def learn_weights(self, mode='greedy'): """ Runs iterations of generating samples with current weights and training new weight vectors based on the collected samples. - mode can be ['greedy','rl_half','rl_regression','rl_lspi'] """ print("Beginning to learn regression weights") self.tt.tic('learn_weights:all') # Need to have weights set here to collect samples, so let's set # to manual_1 to get a reasonable execution trace. self.weights = self.load_weights(weights_mode='manual_1', force=True) if comm_rank == 0: print("Initial weights:") np.set_printoptions(precision=2, suppress=True, linewidth=160) print self.get_reshaped_weights() # Loop until max_iterations or the error is below threshold error = threshold = 0.001 max_iterations = 12 # early iterations should explore more than later iterations # so do an exponential fall-off, halving every few iterations epsilons = 0.6 * np.exp2(-np.arange(0, max_iterations + 1) / 2.) # Collect samples (parallelized) num_samples = 350 # actually this refers to images dets, clses, all_samples = self.run_on_dataset(False, num_samples, epsilon=epsilons[0]) for i in range(0, max_iterations): # do regression with cross-validated parameters (parallelized) weights = None if comm_rank == 0: weights, error = self.regress(all_samples, mode) self.weights = weights try: # write image of the weights img_filename = opjoin( config.get_dp_weights_images_dirname(self), 'iter_%d.png' % i) self.write_weights_image(img_filename) # write image of the average feature all_features = self.construct_X_from_samples(all_samples) avg_feature = np.mean(all_features, 0).reshape(len(self.actions), BeliefState.num_features) img_filename = opjoin( config.get_dp_features_images_dirname(self), 'iter_%d.png' % i) self.write_feature_image(avg_feature, img_filename) except: print("Couldn't plot, no big deal.") print( """ After iteration %d (epsilon %.2f), we've trained on %d samples and the weights and error are:""" % (i, epsilons[i], len(all_samples))) np.set_printoptions(precision=2, suppress=True, linewidth=160) print self.get_reshaped_weights() print error # after the first iteration, check if the error is small if i > 0 and error <= threshold: break print( "Now collecting more samples with the updated weights...") safebarrier(comm) weights = comm.bcast(weights, root=0) self.weights = weights new_dets, new_clses, new_samples = self.run_on_dataset( False, num_samples, epsilon=epsilons[i + 1]) if comm_rank == 0: # We either collect only unique samples or all samples only_unique_samples = False if only_unique_samples: self.tt.tic('learn_weights:unique_samples') for sample in new_samples: if not (sample in all_samples): all_samples.append(sample) print( "Only adding unique samples to all_samples took %.3f s" % self.tt.qtoc('learn_weights:unique_samples')) else: all_samples += new_samples if comm_rank == 0: print("Done training regression weights! Took %.3f s total" % self.tt.qtoc('learn_weights:all')) # Save the weights filename = config.get_dp_weights_filename(self) np.savetxt(filename, self.weights, fmt='%.6f') safebarrier(comm) weights = comm.bcast(weights, root=0) self.weights = weights return weights
def learn_weights(self,mode='greedy'): """ Runs iterations of generating samples with current weights and training new weight vectors based on the collected samples. - mode can be ['greedy','rl_half','rl_regression','rl_lspi'] """ print("Beginning to learn regression weights") self.tt.tic('learn_weights:all') # Need to have weights set here to collect samples, so let's set # to manual_1 to get a reasonable execution trace. self.weights = self.load_weights(weights_mode='manual_1',force=True) if comm_rank==0: print("Initial weights:") np.set_printoptions(precision=2,suppress=True,linewidth=160) print self.get_reshaped_weights() # Loop until max_iterations or the error is below threshold error = threshold = 0.001 max_iterations = 12 # early iterations should explore more than later iterations # so do an exponential fall-off, halving every few iterations epsilons = 0.6*np.exp2(-np.arange(0,max_iterations+1)/2.) # Collect samples (parallelized) num_samples = 350 # actually this refers to images dets,clses,all_samples = self.run_on_dataset(False,num_samples,epsilon=epsilons[0]) for i in range(0,max_iterations): # do regression with cross-validated parameters (parallelized) weights = None if comm_rank==0: weights, error = self.regress(all_samples, mode) self.weights = weights try: # write image of the weights img_filename = opjoin( config.get_dp_weights_images_dirname(self),'iter_%d.png'%i) self.write_weights_image(img_filename) # write image of the average feature all_features = self.construct_X_from_samples(all_samples) avg_feature = np.mean(all_features,0).reshape( len(self.actions),BeliefState.num_features) img_filename = opjoin( config.get_dp_features_images_dirname(self),'iter_%d.png'%i) self.write_feature_image(avg_feature, img_filename) except: print("Couldn't plot, no big deal.") print(""" After iteration %d (epsilon %.2f), we've trained on %d samples and the weights and error are:"""%(i,epsilons[i],len(all_samples))) np.set_printoptions(precision=2,suppress=True,linewidth=160) print self.get_reshaped_weights() print error # after the first iteration, check if the error is small if i>0 and error<=threshold: break print("Now collecting more samples with the updated weights...") safebarrier(comm) weights = comm.bcast(weights,root=0) self.weights = weights new_dets,new_clses,new_samples = self.run_on_dataset(False,num_samples,epsilon=epsilons[i+1]) if comm_rank==0: # We either collect only unique samples or all samples only_unique_samples = False if only_unique_samples: self.tt.tic('learn_weights:unique_samples') for sample in new_samples: if not (sample in all_samples): all_samples.append(sample) print("Only adding unique samples to all_samples took %.3f s"%self.tt.qtoc('learn_weights:unique_samples')) else: all_samples += new_samples if comm_rank==0: print("Done training regression weights! Took %.3f s total"% self.tt.qtoc('learn_weights:all')) # Save the weights filename = config.get_dp_weights_filename(self) np.savetxt(filename, self.weights, fmt='%.6f') safebarrier(comm) weights = comm.bcast(weights,root=0) self.weights = weights return weights
def load_weights(self, weights_mode=None, force=False): """ Set self.weights to weights loaded from file or constructed from scratch according to weights_mode and self.weights_dataset_name. """ if not weights_mode: weights_mode = self.weights_mode filename = config.get_dp_weights_filename(self) if not force and opexists(filename): self.weights = np.loadtxt(filename) return self.weights # Construct the weight vector by concatenating per-action vectors # The features are [P(C) P(not C) H(C) 1] weights = np.zeros((len(self.actions), BeliefState.num_features)) num_classes = len(self.dataset.classes) # OPTION 1: the manual weights correspond to the class of the action if weights_mode == 'manual_1': # Set weight of 1 on the P(C) feature weights[:, 0] = 1 # If gist mode, also set a 1 to 1-t/T, which starts at as 1. if self.actions[0].name == 'gist': weights[0, -1] = 1 weights = weights.flatten() elif weights_mode == 'manual_1_fastinf': # If gist mode, also set a 1 to 1-t/T, which starts at as 1. start = 0 if self.actions[0].name == 'gist': start = 1 weights[0, -1] = 1 # Set weight of 1 on the P(C_i|O) features weights[start:start + num_classes, 1:1 + num_classes] = np.eye(num_classes) weights = weights.flatten() elif weights_mode in ['manual_2', 'manual_3']: # Figure out the statistics if they aren't there if 'naive_ap|present' not in self.actions[0].obj.config or \ 'actual_ap|present' not in self.actions[0].obj.config or \ 'actual_ap|absent' not in self.actions[0].obj.config: det_configs = self.output_det_statistics() self.test_actions = self.init_actions() self.actions = self.test_actions # OPTION 2: the manual weights are [naive_ap|present 0 0 0] if weights_mode == 'manual_2': for action in self.actions: print action.obj.config weights[:, 0] = [ action.obj.config['naive_ap|present'] for action in self.actions ] # OPTION 3: the manual weights are [actual_ap|present actual_ap|absent 0 0] elif weights_mode == 'manual_3': weights[:, 0] = [ action.obj.config['actual_ap|present'] for action in self.actions ] # TODO: get rid of this # weights[:,1] = [action.obj.config['actual_ap|absent'] for action in self.actions] else: None # impossible weights = weights.flatten() elif weights_mode in ['greedy', 'rl_half', 'rl_regression', 'rl_lspi']: weights = self.learn_weights(weights_mode) print("DONE LEARNING WEIGHTS PLZ RUN ON TEST GODDAMNIT!!!!") sys.exit() else: raise ValueError("unsupported weights_mode %s" % weights_mode) self.weights = weights return weights
def load_weights(self,weights_mode=None,force=False): """ Set self.weights to weights loaded from file or constructed from scratch according to weights_mode and self.weights_dataset_name. """ if not weights_mode: weights_mode = self.weights_mode filename = config.get_dp_weights_filename(self) if not force and opexists(filename): self.weights = np.loadtxt(filename) return self.weights # Construct the weight vector by concatenating per-action vectors # The features are [P(C) P(not C) H(C) 1] weights = np.zeros((len(self.actions), BeliefState.num_features)) num_classes = len(self.dataset.classes) # OPTION 1: the manual weights correspond to the class of the action if weights_mode == 'manual_1': # Set weight of 1 on the P(C) feature weights[:,0] = 1 # If gist mode, also set a 1 to 1-t/T, which starts at as 1. if self.actions[0].name=='gist': weights[0,-1] = 1 weights = weights.flatten() elif weights_mode == 'manual_1_fastinf': # If gist mode, also set a 1 to 1-t/T, which starts at as 1. start = 0 if self.actions[0].name=='gist': start = 1 weights[0,-1] = 1 # Set weight of 1 on the P(C_i|O) features weights[start:start+num_classes,1:1+num_classes] = np.eye(num_classes) weights = weights.flatten() elif weights_mode in ['manual_2','manual_3']: # Figure out the statistics if they aren't there if 'naive_ap|present' not in self.actions[0].obj.config or \ 'actual_ap|present' not in self.actions[0].obj.config or \ 'actual_ap|absent' not in self.actions[0].obj.config: det_configs = self.output_det_statistics() self.test_actions = self.init_actions() self.actions = self.test_actions # OPTION 2: the manual weights are [naive_ap|present 0 0 0] if weights_mode == 'manual_2': for action in self.actions: print action.obj.config weights[:,0] = [action.obj.config['naive_ap|present'] for action in self.actions] # OPTION 3: the manual weights are [actual_ap|present actual_ap|absent 0 0] elif weights_mode == 'manual_3': weights[:,0] = [action.obj.config['actual_ap|present'] for action in self.actions] # TODO: get rid of this # weights[:,1] = [action.obj.config['actual_ap|absent'] for action in self.actions] else: None # impossible weights = weights.flatten() elif weights_mode in ['greedy','rl_half','rl_regression','rl_lspi']: weights = self.learn_weights(weights_mode) print("DONE LEARNING WEIGHTS PLZ RUN ON TEST GODDAMNIT!!!!") sys.exit() else: raise ValueError("unsupported weights_mode %s"%weights_mode) self.weights = weights return weights