def run(self): # initialize cuda context devices = self.devices if isinstance(self.devices, list) else [self.devices] self.devices = [torch.device(device) for device in devices] if 'cuda' in self.devices[0].type: torch.cuda.set_device(self.devices[0]) # seed the random number generators for device in self.devices: nn_util.init_random_seed(self.config['seed'], device) agent_name = self.config.get('parser', 'vanilla') self.agent = get_parser_agent_by_name(agent_name).build( self.config, master='learner').to(self.devices[0]).train() use_trainable_sketch_predictor = self.config.get( 'use_trainable_sketch_predictor', False) if use_trainable_sketch_predictor: assert len(self.devices) > 1 if 'cuda' in self.devices[1].type: torch.cuda.set_device(self.devices[1]) self.sketch_predictor = SketchPredictor.build( self.config).train().to(self.devices[1]) self.sketch_predictor_trainer = SketchPredictorTrainer( self.sketch_predictor, self.config['max_train_step'], 0, self.config) self.train()
def run(self): # initialize cuda context self.device = torch.device(self.device) if 'cuda' in self.device.type: torch.cuda.set_device(self.device) # seed the random number generators nn_util.init_random_seed(self.config['seed'], self.device) def get_train_shard_path(i): return os.path.join( self.config['train_shard_dir'], self.config['train_shard_prefix'] + str(i) + '.jsonl') # create agent and set it to evaluation mode if 'cuda' in str(self.device.type): torch.cuda.set_device(self.device) agent_name = self.config.get('parser', 'vanilla') self.agent = get_parser_agent_by_name(agent_name).build(self.config, master=self.actor_id).to(self.device).eval() # initialize sketch predictor use_trainable_sketch_predictor = self.config.get('use_trainable_sketch_predictor', False) if use_trainable_sketch_predictor: self.sketch_predictor = SketchPredictorProxy() self.sketch_predictor.initialize(self) if self.config.get('actor_use_table_bert_proxy', False): # initialize proxy self.agent.encoder.bert_model.initialize(self) # load environments self.load_environments( [ get_train_shard_path(i) for i in range(self.config['shard_start_id'], self.config['shard_end_id']) ], example_ids=self.example_ids ) if self.use_consistency_model: print('Load consistency model', file=sys.stderr) self.consistency_model = ConsistencyModel(QuestionSimilarityModel.load(self.config['question_similarity_model_path']), self.shared_program_cache, self.environments, alpha=float(self.config['consistency_alpha']), log_file=os.path.join(self.config['work_dir'], f'consistency_model_actor_{self.actor_id}.log'), debug=self.actor_id == 0) self.replay_buffer = ReplayBuffer(self.agent, self.shared_program_cache) if self.config['load_saved_programs']: self.replay_buffer.load(self.environments, self.config['saved_program_file']) print(f'[Actor {self.actor_id}] loaded {self.replay_buffer.size} programs to buffer', file=sys.stderr) self.train()
def run(self): # initialize cuda context devices = self.devices if isinstance(self.devices, list) else [self.devices] self.devices = [torch.device(device) for device in devices] if 'cuda' in self.devices[0].type: torch.cuda.set_device(self.devices[0]) # seed the random number generators for device in self.devices: nn_util.init_random_seed(self.config['seed'], device) agent_name = self.config.get('parser', 'vanilla') self.agent = get_parser_agent_by_name(agent_name).build( self.config, master='learner').to(self.devices[0]).train() model_path = os.path.join(self.config['work_dir'], 'model.best.bin') if os.path.exists(model_path): print("Loading from saved model.") self.agent.load(model_path) else: print("No model found at %s." % model_path) sys.stdout.flush() # checkload s= input('Check if model loaded.') use_trainable_sketch_predictor = self.config.get( 'use_trainable_sketch_predictor', False) if use_trainable_sketch_predictor: assert len(self.devices) > 1 if 'cuda' in self.devices[1].type: torch.cuda.set_device(self.devices[1]) self.sketch_predictor = SketchPredictor.build( self.config).train().to(self.devices[1]) self.sketch_predictor_trainer = SketchPredictorTrainer( self.sketch_predictor, self.config['max_train_step'], 0, self.config) self.psi_queue.put(self.current_psi) self.train()
def run(self): # initialize cuda context self.device = torch.device(self.device) if 'cuda' in self.device.type: torch.cuda.set_device(self.device) # seed the random number generators nn_util.init_random_seed(self.config['seed'], self.device) agent_name = self.config.get('parser', 'vanilla') self.agent = get_parser_agent_by_name(agent_name).build( self.config, master='evaluator').to(self.device).eval() self.load_environments() summary_writer = SummaryWriter( os.path.join(self.config['work_dir'], 'tb_log/dev')) dev_scores = [] while True: if self.check_and_load_new_model(): print(f'[Evaluator] evaluate model [{self.model_path}]', file=sys.stderr) t1 = time.time() decode_results = self.agent.decode_examples( self.environments, beam_size=self.config['beam_size'], batch_size=32) eval_results = Evaluation.evaluate_decode_results( self.environments, decode_results) t2 = time.time() print( f'[Evaluator] step={self.get_global_step()}, result={repr(eval_results)}, took {t2 - t1}s', file=sys.stderr) summary_writer.add_scalar('eval/accuracy', eval_results['accuracy'], self.get_global_step()) summary_writer.add_scalar('eval/oracle_accuracy', eval_results['oracle_accuracy'], self.get_global_step()) dev_score = eval_results['accuracy'] if not dev_scores or max(dev_scores) < dev_score: print(f'[Evaluator] save the current best model', file=sys.stderr) with open(os.path.join(self.config['work_dir'], 'dev.log'), 'w') as f: f.write(json.dumps(eval_results)) self.agent.save( os.path.join(self.config['work_dir'], 'model.best.bin')) dev_scores.append(dev_score) sys.stderr.flush() time.sleep(2) # in seconds