def HK2011Pairwise(twoStage=False): # 1st Pass # Feature extraction ext = extractor.Extractor() ext.HK2011Baseline(prr.examples, prr.labels) # Learning lrn = learner.Learner() lrn.initSVM(0.1) lrn.fitSVM(ext.trainExamples, ext.trainLabels) # Prediction predictions1 = lrn.predictSVM(ext.testExamples) # Evaluation accuracy = lrn.computeAccuracy(ext.testLabels, predictions1) F1 = lrn.computeF1(ext.testLabels, predictions1) report = lrn.evaluatePairwise(ext.testLabels, predictions1) # Reporting stage = "HK2011 1st Pass" output.reportPairwiseLearning(stage, prr, accuracy, F1, report) output.savePredictions("output/" + stage + ".txt", prr.examples[constants.TEST], ext.testExamples, predictions1, ext.testLabels) # 2nd Pass if twoStage: # Feature extraction ext.appendBinaryLanguageFeatures(prr.examples, prr.labels, constants.TEST, prr.testLanguages) # Learning lrn = learner.Learner() lrn.initSVM(0.0001) lrn.fitSVM(ext.testExamples, predictions1) # Prediction predictions2 = lrn.predictSVM(ext.testExamples) # Evaluation accuracy = lrn.computeAccuracy(ext.testLabels, predictions2) F1 = lrn.computeF1(ext.testLabels, predictions2) report = lrn.evaluatePairwise(ext.testLabels, predictions2) # Reporting stage = "HK2011 2nd Pass" output.reportPairwiseLearning(stage, prr, accuracy, F1, report) output.savePredictions("output/" + stage + ".txt", prr.examples[constants.TEST], ext.testExamples, predictions2, ext.testLabels) # Significance print constants.SIGNIFICANCE.format( lrn.computeMcNemarSignificance(ext.testLabels, predictions1, predictions2)) return ext, lrn
def pairwiseDeduction(measure): # Feature extraction ext = extractor.Extractor() if measure == constants.IDENTICAL_WORDS: ext.identicalWordsBaseline(prr.examples, prr.labels) elif measure == constants.IDENTICAL_PREFIX: ext.identicalPrefixBaseline(prr.examples, prr.labels) elif measure == constants.IDENTICAL_LETTER: ext.identicalFirstLetterBaseline(prr.examples, prr.labels) predictions = ext.testExamples.reshape((ext.testExamples.shape[0], )) # Evaluation lrn = learner.Learner() accuracy = lrn.computeAccuracy(ext.testLabels, predictions) F1 = lrn.computeF1(ext.testLabels, predictions) report = lrn.evaluatePairwise(ext.testLabels, predictions) # Reporting output.reportPairwiseDeduction(constants.DEDUCERS[measure], prr, accuracy, F1, report) output.savePredictions( "output/Pairwise " + constants.DEDUCERS[measure] + ".txt", prr.examples[constants.TEST], ext.testExamples, predictions, ext.testLabels) return predictions
def experiment(estimator, train_df, valid_df, bg_paths, batch_size, sample_size, version_path=None, csv_log_path=None): label_num = len(config.POSSIBLE_LABELS) train_generator = generator.batch_generator(train_df, batch_size, label_num, bg_paths, sampling_size=sample_size) valid_generator = generator.batch_generator(valid_df, batch_size, label_num, bg_paths, mode='valid', sampling_size=sample_size) valid_steps = int(np.ceil(valid_df.shape[0] / batch_size)) steps_per_epoch = int(np.ceil(sample_size * label_num / batch_size)) learn = learner.Learner(estimator, version_path, csv_log_path) result = learn.learn(train_generator, valid_generator, valid_steps, steps_per_epoch=steps_per_epoch) return result
def main(argv): try: options, _ = getopt.getopt(argv, "hlp:", ["help", "load=", "print="]) except getopt.GetoptError: sys.exit() load = False for option, value in options: if option in ("-h", "--help"): print("initialize a new experiment: run interface.py with no args") print( "continue experiments based on the archive: interface.py -l[--load] \"YYYY-MM-DD_hh-mm\"" ) return elif option in ("-l", "--load"): load = True datetime = value return elif option in ("-p", "--print"): archive_filename = './archives/archive_' + value + '.txt' print_archive(archive_filename) return interface = Interface() learn = learner.Learner(interface) if load: learn.load(datetime) else: learn.init() learn.train() learn.print_archive() learn.plot_best_costs_list() learn.close()
def evaluate_agent(ag_obj): env = environment.Environment(buoys, steps_between_actions, vessel_id, rudder_id, thruster_id, scenario, goal, goal_heading_e_ccw, goal_vel_lon, True) env.set_up() agent = learner.Learner(load_saved_regression=ag_obj, action_space_name='large_action_space') env.set_single_start_pos_mode([8000, 4600, -103.5, 3, 0, 0]) # env.set_single_start_pos_mode([6600, 4200, -102, 3, 0, 0]) env.new_episode() final_flag = 0 with open('debug.txt', 'w') as outfile: for step in range(evaluation_steps): state = env.get_state() print(state, file=outfile) action = agent.select_action(state) print(action, file=outfile) state_prime, reward = env.step(action[0], action[1]) print(state_prime, file=outfile) print(reward, file=outfile) print('\n', file=outfile) final_flag = env.is_final() print("***Evaluation step " + str(step + 1) + " Completed") if final_flag != 0: break
def train_from_single_episode(episodes, pickle_vars, ep_number): env = environment.Environment(buoys, steps_between_actions, vessel_id, rudder_id, thruster_id, scenario, goal, goal_heading_e_ccw, goal_vel_lon, False) replace_reward = reward.RewardMapper(plot_flag=False) replace_reward.set_boundary_points(buoys) replace_reward.set_goal(goal, goal_heading_e_ccw, goal_vel_lon) batch_learner = learner.Learner( file_to_save=learner_file, action_space_name=pickle_vars['action_space'], r_m_=replace_reward) episode = episodes[ep_number] with open('debug_ep.txt', 'w') as outfile: for transition in episode['transitions_list']: print(transition[0], file=outfile) print(list(transition[1]), file=outfile) print(transition[2], file=outfile) print(transition[3], file=outfile) print('\n', file=outfile) batch_learner.add_to_batch(episode['transitions_list'], episode['final_flag']) batch_learner.set_up_agent() for it in range(max_fit_iterations): if it % 10 == 0: batch_learner.fqi_step(1, debug=True) else: batch_learner.fqi_step(1, debug=False)
def groupDeduction(measure): # Feature extraction ext = extractor.Extractor() if measure == constants.IDENTICAL_WORDS: predictedLabels, predictedSets = ext.identicalWordsGroupBaseline( prr.testMeanings, prr.testLanguages, rdr.wordforms) elif measure == constants.IDENTICAL_PREFIX: predictedLabels, predictedSets = ext.identicalPrefixGroupBaseline( prr.testMeanings, prr.testLanguages, rdr.wordforms) elif measure == constants.IDENTICAL_LETTER: predictedLabels, predictedSets = ext.identicalFirstLetterGroupBaseline( prr.testMeanings, prr.testLanguages, rdr.wordforms) trueLabels = ext.extractGroupLabels(rdr.cognateSets, rdr.wordforms, prr.testMeanings, prr.testLanguages) # Evaluation lrn = learner.Learner() V1scores = { meaningIndex: lrn.computeV1(trueLabels[meaningIndex], predictedLabels[meaningIndex]) for meaningIndex in prr.testMeanings } # Reporting output.reportGroup(constants.DEDUCERS[measure], V1scores, rdr.meanings) output.saveGroup("output/Group " + constants.DEDUCERS[measure] + ".txt", predictedSets)
def crossover(motherDNN, fatherDNN): print("[crossover] Generating child") motherWeights = motherDNN.getWeights() fatherWeights = fatherDNN.getWeights() childWeights = combineWeights(motherWeights, fatherWeights) child = mlearner.Learner() child.Init() child.setWeights(childWeights) return child
def train_from_samples(sample_files): replace_reward = reward.RewardMapper(plot_flag=False, r_mode_='cte') replace_reward.set_boundary_points(buoys) replace_reward.set_goal(goal, goal_heading_e_ccw, goal_vel_lon) batch_learner = learner.Learner(r_m_=replace_reward) for file in sample_files: batch_learner.load_sample_file(file) batch_learner.set_up_agent() batch_learner.fqi_step(50)
def __init__(self): self.win = tk.Tk() self.button_dict = {} self.actions_list = ['*', '/', '+', '-', '.', '='] self.EQUAL_SIGN = '=' self.DOT_SIGN = '.' self.recent_action = "n" self.mySVM = learner.Learner() self.my_cnn = cnn.cnn()
def doLearnAll(self, num_of_times = 100): count = 0 while len(self.nodes)-2 < max(self.layer_range)+1: count += 1 learner = L.Learner(self.input, self.ground_truth) learner.doLearn(self.nodes, num_of_times) self.tick(learner) self.progress(count, learner) self.addLearnerToTopLists(learner) self.nodes = self.increaseNodesOrLayers(self.nodes)
def learn(ext, C): # Learning lrn = learner.Learner() lrn.initLogisticRegression(C) lrn.fitLogisticRegression(ext.trainExamples, ext.trainLabels) # Prediction predictions = lrn.predictLogisticRegression(ext.testExamples) return lrn, predictions
def treeFeatureSelection(): # Feature extraction ext = extractor.Extractor() ext.appendWordSimilarityFeatures(prr.examples, prr.labels, ext.allMeasures) # Feature selection lrn = learner.Learner() lrn.initForest(250, 0) lrn.fitForest(ext.trainExamples, ext.trainLabels) importances = lrn.getForestImportances() # Reporting for i, feature in enumerate(ext.allMeasures): print "{0}: {1:.4f}".format(feature, importances[i])
def train_from_batch(episodes, pickle_vars): replace_reward = reward.RewardMapper(plot_flag=False, r_mode_='exp_border_target_rot_angle') replace_reward.set_boundary_points(buoys) replace_reward.set_goal(goal, goal_heading_e_ccw, goal_vel_lon) batch_learner = learner.Learner(file_to_save=learner_file, action_space_name=pickle_vars['action_space'], r_m_=replace_reward) batch_size = 0 for episode in episodes: remaining = max_tuples_per_batch - len(episode['transitions_list']) - batch_size if remaining >= 0: batch_learner.add_to_batch(episode['transitions_list'], episode['final_flag']) batch_size += len(episode['transitions_list']) else: batch_learner.add_to_batch(episode['transitions_list'][0:abs(remaining)], 0) break batch_learner.set_up_agent() batch_learner.fqi_step(max_fit_iterations)
def main(): direcs = ['OUV9DC3LZ08N6R6DJYQF'] de = DataExtractor(direcs, 137) trajs, X, y = de.extract() est = knet.Network([64, 64], learning_rate=.01, epochs=50) lnr = learner.Learner(est) lnr.add_data(X, y) lnr.train() print "Control at init state: " + str(lnr.intended_action(X[0])) lnr.est.save_weights('tmp/weights.txt', 'tmp/stats.txt') lnr.est.load_weights('tmp/weights.txt', 'tmp/stats.txt') print "Control at init stat after loading: " + str( lnr.intended_action(X[0]))
def basic_model(self, individuals, signals, meanings, k, terminal, monitor=None): exec('terminal_condition = revelations.terminate_%s' % terminal) keyword_args = { 'seed': revelations.initialize(individuals, signals, meanings), 'reproduce': revelations.parental_learning(k), 'wilt': terminal_condition, } if monitor is not None: exec('monitor = epoch_contexts.get_%s' % monitor) keyword_args['epoch_context'] = monitor self._run(learner.Learner(**keyword_args))
def setUp(self): def _wilt_function(iterations): wilt_state = {'iterations': iterations} def _wilt(*unused_args, **unused_kwargs): wilt_state['iterations'] -= 1 return wilt_state['iterations'] < 0 return _wilt @contextlib.contextmanager def _mock_context(unused_state): yield self._seed_mock = mock.create_autospec(lambda: None) self._wilt = _wilt_function(5) self._reproduce_mock = mock.create_autospec(lambda unused_state: None) self._learner = learner.Learner(seed=self._seed_mock, wilt=self._wilt, reproduce=self._reproduce_mock)
def main(self, screen, age): """ Main loop for game Redraws scores and next tetromino each time the loop is passed through """ clock = pygame.time.Clock() self.matris = Matris() screen.blit(construct_nightmare(screen.get_size()), (0, 0)) matris_border = Surface( (MATRIX_WIDTH * BLOCKSIZE + BORDERWIDTH * 2, VISIBLE_MATRIX_HEIGHT * BLOCKSIZE + BORDERWIDTH * 2)) matris_border.fill(BORDERCOLOR) screen.blit(matris_border, (MATRIS_OFFSET, MATRIS_OFFSET)) timewait = 0 print("session:" + str(age)) if age % 100 == 0: timewait = 0.5 else: timewait = 0.0001 learn = learner.Learner(self.matris, self, timewait)
def main(): global student if real_time: global time_interval global student print( "Enter 'A','B','C','D' to answer a question\nEnter 'start' to start :" ) ip = input("Enter input :") if ip == "start": tl.start() # Start a new job while True: curr_prob = student.next_question() if curr_prob: curr_prob.display() question_start_time = time.time() ip = input("Enter answer :") question_end_time = time.time() question_time = question_end_time - question_start_time # f.write("Question answered\n") student.answer(curr_prob, ip, question_time) else: print("Test completed") tl.stop() # Stop the job for i in range(student.no_of_problems): student.show_question_graph(i, save=True, display=False) student.show_decay_rate_graph(i, save=True, display=False) # student.show_time_graph(i , save=True , display=False) # student.show_no_of_review_events_graph(i , save=True , display=False) student.show_overall_score_graph() break # print("Scores") # print(student.__dict__) print(list(student.scores)) # print(student.display_review_queue()) else: # Time is tracked here. question_time = 0 global global_time #Initialise test test_ = test.Test() test_.construct_test(folder=problem_folder) #Initialise learner session student = learner.Learner(test_) print("-----------INITIAL-------------") student.display_state() #main time loop print( "Press 'n' to advance one time step\nPress 'A','B','C','D' to answer a question\nPress anyother button to start:" ) while True: start_loop() ip = input("Input :") if ip == "n": print("Advancing one time step ") global_time += 1 question_time += 1 student.increment_time() elif ip == "A" or ip == "B" or ip == "C" or ip == "D": student.answer(curr_prob, ip, question_time) question_time += 1 student.increment_time() curr_prob = student.next_question() if curr_prob: curr_prob.display() question_time = 0 else: print("Test completed!!") break elif ip == "curr": curr_prob.display() continue elif ip == "show": ip_ = input("Enter entity to display ") if ip_ == "score": student.show_overall_score_graph() else: student.show_question_graph(int(ip_)) elif ip == "start": curr_prob = student.next_question() student.increment_time() if curr_prob: curr_prob.display() question_time = 0 else: print("Test completed!!") break elif ip == "exit": break print("SCORE HISTORY") # student.show_score_history() print(student.score_history[-1]) # print("CURRENT QUESTION TIME ",question_time) # print("GLOBAL TIME ",global_time) end_loop() student.show_overall_score_graph() for i in range(student.no_of_problems): student.show_question_graph(i)
def main(): # parse arguments parser = args.parse_args() # set up logger log_path = os.path.join(parser.save_dir, "logs") if not os.path.exists(log_path): os.mkdir(log_path) log_fname = os.path.join( log_path, "{}_log_{}.log".format(parser.exp_name, dt.now().strftime("%Y%m%d_%H%M"))) log.basicConfig(filename=log_fname, format='%(asctime)s: %(name)s || %(message)s', level=log.INFO) # ============================================================================= # start # ============================================================================= log.info("=" * 40 + " Start Program " + "=" * 40) # ============================================================================= # misc stuff # ============================================================================= # Set devise to CPU if available device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") log.info("Device is {}".format(device)) # set random seeds random.seed(parser.seed) np.random.seed(parser.seed) torch.manual_seed(parser.seed) if torch.cuda.is_available(): torch.cuda.manual_seed_all(parser.seed) # set data directory log.info("Data Directory is {}.".format(parser.data_dir)) # ============================================================================= # import data # ============================================================================= task_names = parser.data_name.split(',') content_headers = parser.content.split(',') tokenizer = transformers.AutoTokenizer.from_pretrained( parser.model, do_lower_case=parser.do_lower_case) data_handler = myio.IO( data_dir=parser.data_dir, model_name=parser.model, task_names=task_names, tokenizer=tokenizer, max_length=parser.input_length, content=content_headers, review_key=parser.review_key, label_name=parser.label_name, val_split=parser.val_split, test_split=parser.test_split, batch_size=parser.batch_size, shuffle=not parser.no_shuffle, cache=not parser.no_cache, ) data_handler.read_task() # ============================================================================= # define model # ============================================================================= log.info("=" * 40 + " Defining Model " + "=" * 40) config = transformers.AutoConfig.from_pretrained(parser.model) classifier = model.Model( model=parser.model, config=config, n_others=parser.n_others, n_hidden=parser.n_class_hidden, n_flag=parser.n_labels, load=parser.preload_emb, load_name=parser.preload_emb_name, ) # ============================================================================= # define trainer # ============================================================================= log.info("Save Directory is {}.".format(parser.save_dir)) log.info("=" * 40 + " Defining Trainer " + "=" * 40) # create trainer object trainer = learner.Learner( model=classifier, device=device, myio=data_handler, max_epochs=parser.max_epochs, save_path=parser.save_dir, lr=parser.lr, weight_decay=parser.weight_decay, pct_start=parser.pct_start, anneal_strategy=parser.anneal_strategy, cycle_momentum=parser.cycle_momentum, log_int=parser.log_int, buffer_break=not parser.no_early_stop, break_int=parser.patience, accumulate_int=parser.grad_accum, max_grad_norm=parser.max_grad_norm, n_others=parser.n_others, batch_size=parser.batch_size, check_int=parser.check_int, save=parser.save, test=parser.test, ) # train model best = trainer.learn( model_name=parser.model, task_name=task_names[0], early_check=parser.early_check, debug=parser.debug, ) best['experiment'] = parser.exp_name #write results to "results.jsonl" if not os.path.exists(parser.save_dir): os.mkdir(parser.save_dir) results_name = os.path.join(parser.save_dir, "results.jsonl") with open(results_name, 'a') as f: f.write(json.dumps(best) + "\n") log.info("=" * 40 + " Program Complete " + "=" * 40) log.info("=" * 40 + " Results written to {} ".format(results_name) + "=" * 40)
def evaluate_agent(ag_obj): agent = learner.Learner(load_saved_regression=ag_obj, action_space_name='complete_angle', nn_=True) env = environment.Environment(buoys, 20, vessel_id, rudder_id, thruster_id, scenario, goal, goal_heading_e_ccw, goal_vel_lon, False, _increment=0.5) env.set_up() starting_points = [ [11000, 5250, -101, 3, 0, 0], [11000, 5300, -104, 3, 0, 0], [11000, 5280, -103.5, 3, 0, 0], [11000, 5320, -103.5, 3, 0, 0], [11000, 5320, -103.5, 3, 0, 0]] ret_tuples = list() # env.set_single_start_pos_mode([11000, 5380.10098, -103, 3, 0, 0]) # env.set_single_start_pos_mode([8000, 4600, -103.5, 3, 0, 0]) # env.set_single_start_pos_mode([12000, 5500, -90, 3, 0, 0]) # env.set_single_start_pos_mode([6600, 4200, -102, 3, 0, 0]) # env.starts_from_file_mode('starting_points_global_coord') # env.move_to_next_start() results = list() num_steps = list() steps_inside_goal_region = list() for start_pos in starting_points: final_flag = 0 transitions_list = list() total_steps = 0 env.set_single_start_pos_mode(start_pos) env.move_to_next_start() steps_inside = 0 for step in range(evaluation_steps): state = env.get_state(state_mode='simple_state') state_r = env.convert_to_simple_state(state) action = agent.select_action(state_r) state_prime, reward = env.step(action[0], 0.6) transition = (state, (action[0], action[1]), state_prime, reward) if abs(state_r[2]) < 50: steps_inside+=1 final_flag = env.is_final() print("***Evaluation step " + str(step + 1) + " Completed") transitions_list.append(transition) ret_tuples = ret_tuples + transitions_list total_steps = step if final_flag != 0: break results.append(final_flag) num_steps.append(total_steps) steps_inside_goal_region.append(steps_inside) with open('trajectory_'+agent.learner.__class__.__name__+'it'+str(total_steps)+'end'+str(final_flag), 'wb') as outfile: pickle.dump(transitions_list, outfile) with open('trajectory_'+agent.learner.__class__.__name__+'it'+str(total_steps)+'end'+str(final_flag)+'.csv', 'wt') as out: csv_out = csv.writer(out) csv_out.writerow(['x', 'y', 'heading', 'rudder_lvl']) for tr in transitions_list: pos = (tr[0][0], tr[0][1], tr[0][2], tr[1][0]) csv_out.writerow(pos) with open('results'+agent.learner.__class__.__name__+datetime.datetime.now().strftime('%Y%m%d%H%M%S'), 'wb') as outfile: pickle.dump(num_steps, outfile) pickle.dump(results, outfile) pickle.dump(steps_inside_goal_region, outfile) return ret_tuples
print("Training file: " + options.conll_train) if options.conll_dev != "N/A": print("Development file: " + options.conll_dev) highestScore = 0.0 eId = 0 if os.path.isfile(os.path.join(options.output, options.params)) and \ os.path.isfile(os.path.join(options.output, os.path.basename(options.model))): print 'Found a previous saved model => Loading this model' with open(os.path.join(options.output, options.params), 'r') as paramsfp: c2i, stored_opt = pickle.load(paramsfp) stored_opt.external_embedding = None parser = learner.Learner(c2i, stored_opt) parser.load(os.path.join(options.output, os.path.basename(options.model))) parser.trainer.restart() if options.conll_dev != "N/A": devPredSents = parser.predict(options.conll_dev) count = 0 for idSent, devSent in enumerate(devPredSents): conll_devSent = [entry for entry in devSent if isinstance(entry, utils.ConllEntry)] for entry in conll_devSent: if entry.id <= 0: continue count += 1
# new_state = utils.convert_to_simple_state(new_tup[0], geom_helper) # new_state_p = utils.convert_to_simple_state(new_tup[2], geom_helper) # new_tuple = (new_state, new_tup[1], new_state_p, new_tup[3], new_tup[4]) # simple_state_tuples.append(new_tuple) # # mirror_list = list() # for new_tup in simple_state_tuples: # mirror_state = (new_tup[0][0], -new_tup[0][1], -new_tup[0][2], -new_tup[0][3]) # mirror_action = (-new_tup[1][0], new_tup[1][1]) # mirror_state_p = (new_tup[2][0], -new_tup[2][1], -new_tup[2][2], -new_tup[2][3]) # mirror_tuple = (mirror_state, mirror_action, mirror_state_p, new_tup[3], new_tup[4]) # mirror_list.append(mirror_tuple) # simple_state_tuples += mirror_list batch_learner = learner.Learner( nn_=True, load_saved_regression= 'agents/agent_20180727160449Sequential_r____disc_0.8it20.h5') bundle_name = 'agents/agent_20180727160449Sequential_r____disc_0.8_batch' with open(bundle_name, 'rb') as file: simple_state_tuples = pickle.load(file) batch_learner.add_tuples(simple_state_tuples) batch_learner.set_up_agent() batch_learner.fqi_step(0) for i in range(500): additional_tuples = experiment.run_episodes(batch_learner, reward_mapping) os.chdir('..') # additional_tuples = get_strictly_simmetric_set(points[0], points[1], additional_tuples) # additional_tuples = [tpl for tpl in additional_tuples if tpl[0][3] < 0 and tpl[0][0] > 6000]
if __name__ == "__main__": """ Simple standalone test routine for Actor class """ import json import learner with open('parameters.json', 'r') as f: params = json.load(f) param_set_id = db_initializer.initialize(params) mp_manager = mp.Manager() status_dict = mp_manager.dict() shared_state = mp_manager.dict() shared_mem = mp_manager.Queue() params['actor']['wait_shared_memory_clear'] = False status_dict['quit'] = False status_dict['Q_state_dict_stored'] = False status_dict['request_quit'] = False l = learner.Learner(params, param_set_id, status_dict, shared_state, shared_mem) actor = Actor(params, param_set_id, 0, status_dict, shared_state, shared_mem) actor.run()
threading.Thread.__init__(self) self.time_interval = time_interval self.student = student def run(self): print("Thread executing") while True: t = Timer(self.time_interval, self.update) t.start() t.join() print(threading.active_count()) def update(self): self.student.decay_scores(time_step=self.time_interval) self.student.score_history.append(list(self.student.scores)) self.student.check_scores() self.student.update_review_queue() self.student.update_probability() self.student.display_state() if __name__ == "__main__": test_ = test.Test() test_.construct_test(folder=problem_folder) student = learner.Learner(test_) student.scores = np.array([10, 10, 10, 10, 10, 10, 10, 10, 10]) student.decay_rates = np.array( [0.1, 0.2, 0.3, 0.4, 0.2, 0.6, 0.1, 0.9, 0.1]) update_thread = UpdateThread(1, student) update_thread.start()
# tuples_with_reflection.append(reflect_tuple) # print('Number of tuples after reflection:', len(tuples_with_reflection)) # with open(bundle_name+'_filter_reflected_sim', # 'wb') as outfile: # pickle.dump(tuples_with_reflection, outfile) # org_tuples = list() # with open('samples/samples_bundle_two_angles', 'rb') as file: # org_tuples = pickle.load(file) # new_angle_tup = [tup for tup in tuples if tup[0][4] > -0.2] # selected_tuples = random.sample(org_tuples, 10000) # plot_sequence(selected_tuples) batch_learner = learner.Learner( r_m_=replace_reward, nn_=True, load_saved_regression= 'agents/agent_20180524135238Sequential_r_linear_with_rudder_punish_disc_0.8it5.h5' ) new_list = batch_learner.replace_reward(reduct_batch) simple_state_tuples = list() for tuple in new_list: new_state = convert_state_space(tuple[0], replace_reward) new_state_p = convert_state_space(tuple[2], replace_reward) new_tuple = (new_state, tuple[1], new_state_p, tuple[3], tuple[4]) simple_state_tuples.append(new_tuple) final = [tpl for tpl in simple_state_tuples if tpl[0][0] > 0] batch_learner.add_tuples(final)
def collect_trajectories(): with tf.device('/cpu:0'): reward_mapping.set_goal(goal, goal_heading_e_ccw, goal_vel_lon) viewer = Viewer() viewer.plot_boundary(buoys) viewer.plot_goal(goal, 1000) agents = ['agents/agent_20180727160449Sequential_r____disc_0.8it20.h5'] starting_points = [[11000, 5320, -105.5, 3, 0, 0], [11000, 5320, -104.5, 3, 0, 0], [11000, 5320, -105.5, 3, 0, 0], [11000, 5300, -103.5, 3, 0, 0], [11000, 5300, -102.5, 3, 0, 0], [11000, 5300, -101.5, 3, 0, 0]] for agent_obj in agents: viewer agent = learner.Learner(load_saved_regression=agent_obj, nn_=True) ret_tuples = list() results = list() num_steps = list() env = environment.Environment(rw_mapper=reward_mapping) env.set_up() for start_pos in starting_points: final_flag = 0 transitions_list = list() compact_state_list = list() total_steps = 0 env.set_single_start_pos_mode(start_pos) env.move_to_next_start() steps_inside = 0 for step in range(evaluation_steps): state = env.get_state() print('Value for yaw_p :', state[5]) viewer.plot_position(state[0], state[1], state[2]) state_r = utils.convert_to_simple_state(state, geom_helper) compact_state_list.append(state_r) print('Value for yaw_p :', state_r[3]) print('Value for vlon:', state_r[0]) action = agent.select_action(state_r) state_prime, reward = env.step(action[0], action[1]) transition = (state, (action[0], action[1]), state_prime, reward) if abs(state_r[2]) < 50: steps_inside += 1 final_flag = env.is_final() print("***Evaluation step " + str(step + 1) + " Completed") transitions_list.append(transition) ret_tuples += transitions_list total_steps = step if final_flag != 0: break results.append(final_flag) num_steps.append(total_steps) with open( agent_obj + '_' + str(start_pos[1]) + '_' + str(start_pos[2]) + str(final_flag) + '.csv', 'wt') as out: csv_out = csv.writer(out) csv_out.writerow( ['x', 'y', 'heading', 'rudder_lvl', 'balance']) for tr, compact_state in zip(transitions_list, compact_state_list): pos = (tr[0][0], tr[0][1], tr[0][2], tr[1][0], compact_state[2]) csv_out.writerow(pos)
parser.add_argument('--log-interval', type=int, default=100, help='reporting interval') args = parser.parse_args() # TODO: set seed corpus = data.Corpus(args.data) ntokens = len(corpus.dictionary) learner = learner.Learner(module=model.RNNModel, batch_size=1, use_cuda=args.cuda, module__rnn_type='LSTM', module__ntoken=ntokens, module__ninp=200, module__nhid=200, module__nlayers=2) learner.initialize() learner.load_params(args.checkpoint) words = [corpus.dictionary.idx2word[n] for n in range(10)] print(words) p = learner.predict_proba( np.array([[ corpus.dictionary.word2idx['fish'], corpus.dictionary.word2idx['sees'], corpus.dictionary.word2idx['man'],
"--model-type", type="int", dest="model_type", default=0 ) # 0 none -1 simple char rnn - 2 simple char bilstm - 3 simple prevec (options, args) = parser.parse_args() print("Training file: " + options.conll_train) if options.conll_dev != "N/A": print("Development file: " + options.conll_dev) highestScore = 0.0 eId = 0 print 'Extracting vocabulary' c2i, w2i, features = utils.vocab(options.conll_train) parser = learner.Learner(c2i, w2i, features, options) highestScore = 0.0 eId = 0 for epoch in xrange(options.epochs): print '\n-----------------\nStarting epoch', epoch + 1 if epoch % 10 == 0: if epoch == 0: parser.trainer.restart(learning_rate=0.001) elif epoch == 10: parser.trainer.restart(learning_rate=0.0005) else: parser.trainer.restart(learning_rate=0.00025) parser.train(options.conll_train)
def main(): # parse arguments parser = args.args # get working directory wd = os.getcwd() # set up logger log_fname = os.path.join(wd, "logs", "log_{}.log".format( dt.now().strftime("%Y%m%d_%H%M"))) root = log.getLogger() while len(root.handlers): root.removeHandler(root.handlers[0]) log.basicConfig(filename=log_fname, format='%(asctime)s: %(name)s || %(message)s', level=log.INFO) root.addHandler(log.StreamHandler()) # ============================================================================= # start # ============================================================================= log.info("="*40 + " Start Program " + "="*40) # ============================================================================= # misc stuff # ============================================================================= label_order = {'rating':0, 'flagged':1} # Set devise to CPU if available device = torch.device("cuda" if torch.cuda.is_available() else "cpu") log.info("Device is {}".format(device)) # set random seeds random.seed(parser.seed) np.random.seed(parser.seed) torch.manual_seed(parser.seed) if device == "cuda": torch.cuda.manual_seed(parser.seed) torch.cuda.manual_seed_all(parser.seed) torch.backends.cudnn.benchmark = False torch.backends.cudnn.deterministic = True # set data directory if os.path.isdir(parser.data_dir): data_path = parser.data_dir else: data_path = os.path.join(wd, parser.data_dir) log.info("Data Directory is {}.".format(data_path)) # ============================================================================= # import data # ============================================================================= task_names = [parser.data_name] tokenizer = transformers.AutoTokenizer.from_pretrained(parser.model) label_names = parser.label_names.split(',') data_handler = myio.IO(data_dir = data_path, task_names = task_names, tokenizer = tokenizer, max_length = parser.input_length, val_split = parser.val_split, test_split = parser.test_split, batch_size = parser.batch_size, label_names = label_names ) data_handler.read_task() # ============================================================================= # define model # ============================================================================= log.info("="*40 + " Defining Model " + "="*40) number_labels = [int(n) for n in parser.label_numbers.split(',')] config = transformers.AutoConfig.from_pretrained(parser.model) classifier = model.Model(config=config, nrating = number_labels[label_order.get('rating')], nflag = number_labels[label_order.get('flagged')] ) # ============================================================================= # define trainer # ============================================================================= weights = [float(w) for w in parser.label_weights.split(',')] train_data = data_handler.tasks.get(parser.data_name).get('train') val_data = data_handler.tasks.get(parser.data_name).get('dev') test_data = data_handler.tasks.get(parser.data_name).get('test') if os.path.isdir(parser.save_dir): save_path = parser.save_dir else: save_path = os.path.join(wd, parser.save_dir) log.info("Save Directory is {}.".format(save_path)) log.info("="*40 + " Defining Trainer " + "="*40) # create trainer object trainer = learner.Learner(model = classifier, device = device, train_data = train_data, val_data = val_data, test_data = test_data, rating_w = weights[label_order.get('rating')], flag_w = weights[label_order.get('flagged')], max_epochs = parser.max_epochs, save_path = save_path, lr = parser.lr, buffer_break = (parser.early_stop == 'True'), break_int = parser.patience ) # train model best_path, best_emb_path = trainer.learn(model_name = parser.model, verbose = True, early_check = parser.early_stop ) log.info("="*40 + " Program Complete " + "="*40) log.info("Best Total Weights in {}".format(best_path)) log.info("Best Embedding Weights in {}".format(best_emb_path)) # release logs for handler in log.getLogger().handlers: handler.close() # exit python sys.exit(0)