def step_one_test(): data_loader = DataLoader(args) #Checkpoint.CHECKPOINT_DIR_NAME = "0120_0030" Checkpoint.CHECKPOINT_DIR_NAME = args.checkpoint_dir_name checkpoint_path = os.path.join("./experiment", Checkpoint.CHECKPOINT_DIR_NAME, "best") checkpoint = Checkpoint.load(checkpoint_path) seq2seq = checkpoint.model if args.cuda_use: seq2seq = seq2seq.cuda() seq2seq.eval() evaluator = Evaluator(vocab_dict=data_loader.vocab_dict, vocab_list=data_loader.vocab_list, decode_classes_dict=data_loader.decode_classes_dict, decode_classes_list=data_loader.decode_classes_list, loss=NLLLoss(), cuda_use=args.cuda_use) name = args.run_flag if name == 'test_23k': test_temp_acc, test_ans_acc = evaluator.evaluate( model=seq2seq, data_loader=data_loader, data_list=data_loader.math23k_test_list, template_flag=True, batch_size=64, evaluate_type=0, use_rule=False, mode=args.mode, post_flag=args.post_flag, name_save=name) print(test_temp_acc, test_ans_acc)
def gen_best_23_error(): data_loader = DataLoader() Checkpoint.CHECKPOINT_DIR_NAME = args.checkpoint_dir_name checkpoint_path = os.path.join("./experiment", Checkpoint.CHECKPOINT_DIR_NAME, 'best') checkpoint = Checkpoint.load(checkpoint_path) seq2seq = checkpoint.model if args.cuda_use: seq2seq = seq2seq.cuda() seq2seq.eval() emb_model = seq2seq.encoder.embedding emb_np = emb_model.weight.cpu().data.numpy() np.save("./data/rl_train_data/emb.npy", emb_np) evaluator = Evaluator(vocab_dict = data_loader.vocab_dict, vocab_list = data_loader.vocab_list, decode_classes_dict = data_loader.decode_classes_dict, decode_classes_list = data_loader.decode_classes_list, loss = NLLLoss(), cuda_use = args.cuda_use) evaluator.gen_rl_data(model = seq2seq, data_loader = data_loader, data_list = data_loader.math23k_train_list, template_flag = False, batch_size = 16, evaluate_type = 0, use_rule = False, mode = args.mode, filename = args.load_name)
def step_three(): data_loader = DataLoader(args) Checkpoint.CHECKPOINT_DIR_NAME = args.checkpoint_dir_name checkpoint_path = os.path.join("./experiment", Checkpoint.CHECKPOINT_DIR_NAME, "best") checkpoint = Checkpoint.load(checkpoint_path) seq2seq = checkpoint.model if args.cuda_use: seq2seq = seq2seq.cuda() seq2seq.eval() evaluator = Evaluator(vocab_dict=data_loader.vocab_dict, vocab_list=data_loader.vocab_list, decode_classes_dict=data_loader.decode_classes_dict, decode_classes_list=data_loader.decode_classes_list, loss=NLLLoss(), cuda_use=args.cuda_use) test_temp_acc, test_ans_acc = evaluator.evaluate( model=seq2seq, data_loader=data_loader, data_list=data_loader.math57k_data_list, template_flag=False, batch_size=64, evaluate_type=0, use_rule=True, mode=args.mode) print(test_temp_acc, test_ans_acc)
def gen_math57k_error(): data_loader = DataLoader() Checkpoint.CHECKPOINT_DIR_NAME = args.checkpoint_dir_name checkpoint_path = os.path.join("./experiment", Checkpoint.CHECKPOINT_DIR_NAME, args.load_name) checkpoint = Checkpoint.load(checkpoint_path) seq2seq = checkpoint.model if args.cuda_use: seq2seq = seq2seq.cuda() seq2seq.eval() evaluator = Evaluator(vocab_dict = data_loader.vocab_dict, vocab_list = data_loader.vocab_list, decode_classes_dict = data_loader.decode_classes_dict, decode_classes_list = data_loader.decode_classes_list, loss = NLLLoss(), cuda_use = args.cuda_use) evaluator.gen_rl_data(model = seq2seq, data_loader = data_loader, data_list = data_loader.math57k_data_list, template_flag = False, batch_size = 16, evaluate_type = 0, use_rule = True, mode = args.mode, filename = args.load_name)
def test(args, test_dataset="test"): seq2seq, data_loader = create_model(args) resume_checkpoint = Checkpoint.load(model_only=True) seq2seq.load_state_dict(resume_checkpoint.model) if args.use_cuda: seq2seq = seq2seq.cuda() evaluator = Evaluator( class_dict=data_loader.class_dict, class_list=data_loader.class_list, use_cuda=args.use_cuda ) if test_dataset == "test": test_dataset = data_loader.test_list elif test_dataset == "train": test_dataset = data_loader.train_list seq2seq.eval() with torch.no_grad(): test_temp_acc, test_ans_acc = evaluator.evaluate( model=seq2seq, data_loader=data_loader, data_list=test_dataset, template_flag=True, template_len=False, batch_size=1, beam_width=args.beam, test_log=args.test_log, print_probability=True ) logging.info(f"temp_acc: {test_temp_acc}, ans_acc: {test_ans_acc}") return
def train(self, model, data_loader, batch_size, n_epoch, template_flag, \ resume=False, optimizer=None, mode=0, teacher_forcing_ratio=0, post_flag=False): self.evaluator = Evaluator( vocab_dict=self.vocab_dict, vocab_list=self.vocab_list, decode_classes_dict=self.decode_classes_dict, decode_classes_list=self.decode_classes_list, loss=NLLLoss(), cuda_use=self.cuda_use) if resume: checkpoint_path = Checkpoint.get_certain_checkpoint( "./experiment", "best") resume_checkpoint = Checkpoint.load(checkpoint_path) model = resume_checkpoint.model self.optimizer = resume_checkpoint.optimizer resume_optim = self.optimizer.optimizer defaults = resume_optim.param_groups[0] defaults.pop('params', None) self.optimizer.optimizer = resume_optim.__class__( model.parameters(), **defaults) start_epoch = resume_checkpoint.epoch start_step = resume_checkpoint.step self.train_acc_list = resume_checkpoint.train_acc_list self.test_acc_list = resume_checkpoint.test_acc_list self.loss_list = resume_checkpoint.loss_list else: start_epoch = 1 start_step = 0 self.train_acc_list = [] self.test_acc_list = [] self.loss_list = [] model_opt = NoamOpt( 512, 1, 2000, torch.optim.Adam(model.parameters(), lr=0, betas=(0.9, 0.98), eps=1e-9)) if optimizer is None: optimizer = Optimizer(optim.Adam(model.parameters()), max_grad_norm=0) self.optimizer = model_opt self._train_epoches(data_loader=data_loader, model=model, batch_size=batch_size, start_epoch=start_epoch, start_step=start_step, n_epoch=n_epoch, mode=mode, template_flag=template_flag, teacher_forcing_ratio=teacher_forcing_ratio, post_flag=post_flag)
def train(self, model, data_loader, batch_size, n_epoch, resume=False, optim_lr=1e-3, optim_weight_decay=1e-5, scheduler_step_size=60, scheduler_gamma=0.6): start_epoch = 1 start_step = 0 max_acc = 0 self.optimizer = optim.Adam(model.parameters(), lr=optim_lr, weight_decay=optim_weight_decay) self.scheduler = optim.lr_scheduler.StepLR( self.optimizer, step_size=scheduler_step_size, gamma=scheduler_gamma) if resume: resume_checkpoint = Checkpoint.load(model_only=False) model.load_state_dict(resume_checkpoint.model) resume_optimizer = resume_checkpoint.optimizer resume_scheduler = resume_checkpoint.scheduler if resume_optimizer is not None: start_epoch = resume_checkpoint.epoch start_step = resume_checkpoint.step max_acc = resume_checkpoint.max_acc self.optimizer.load_state_dict(resume_optimizer) self.scheduler.load_state_dict(resume_scheduler) self._train_epoches(data_loader=data_loader, model=model, batch_size=batch_size, start_epoch=start_epoch, start_step=start_step, max_acc=max_acc, n_epoch=n_epoch) return
def step_one_test(): args = get_args() print(args) one_quest_test_list = read_data_json("./data/test.json") print(one_quest_test_list) #Checkpoint.CHECKPOINT_DIR_NAME = "0120_0030" Checkpoint.CHECKPOINT_DIR_NAME = args.checkpoint_dir_name checkpoint_path = os.path.join("./experiment", Checkpoint.CHECKPOINT_DIR_NAME, "best") print('-----', args.checkpoint_dir_name, checkpoint_path) checkpoint = Checkpoint.load(checkpoint_path) seq2seq = checkpoint.model if args.cuda_use: seq2seq = seq2seq.cuda() data_loader = DataLoader(args) seq2seq.eval() evaluator = Evaluator(vocab_dict=data_loader.vocab_dict, vocab_list=data_loader.vocab_list, decode_classes_dict=data_loader.decode_classes_dict, decode_classes_list=data_loader.decode_classes_list, loss=NLLLoss(), cuda_use=args.cuda_use) name = args.run_flag test_temp_acc, test_ans_acc = evaluator.evaluate( model=seq2seq, data_loader=data_loader, data_list=one_quest_test_list, template_flag=False, # todo: 这里改成了false batch_size=64, evaluate_type=0, use_rule=False, mode=args.mode, post_flag=args.post_flag, name_save=name) print(test_temp_acc, test_ans_acc)