args.cuda = False elif args.cuda: torch.cuda.manual_seed(args.seed) model_file_list = [ 'saved_models/01', 'saved_models/02', 'saved_models/03', 'saved_models/04', 'saved_models/00' ] prob_list = [] for j in range(len(model_file_list)): model_file = model_file_list[j] + '/' + args.model print("Loading model from {}".format(model_file)) opt = torch_utils.load_config(model_file) # 加载超参数 trainer = GCNTrainer(opt) # 定义模型 trainer.load(model_file) # 加载最好模型 # load vocab vocab_file = model_file_list[j] + '/vocab.pkl' vocab = Vocab(vocab_file, load=True) assert opt[ 'vocab_size'] == vocab.size, "Vocab size must match that in the saved model." # load data data_file = opt['data_dir'] + '/{}.json'.format(args.dataset) print("Loading data from {} with batch size {}...".format( data_file, opt['batch_size'])) batch = DataLoader(data_file, opt['batch_size'], opt, vocab, evaluation=True)
header="# epoch\ttrain_loss\tdev_loss\tdev_score\tbest_dev_score") # print model info helper.print_config(opt) # model if not opt['load']: trainer = GCNTrainer(opt, emb_matrix=emb_matrix) else: # load pretrained model model_file = opt['model_file'] print("Loading model from {}".format(model_file)) model_opt = torch_utils.load_config(model_file) model_opt['optim'] = opt['optim'] trainer = GCNTrainer(model_opt) trainer.load(model_file) id2label = dict([(v, k) for k, v in label2id.items()]) dev_score_history = [] current_lr = opt['lr'] global_step = 0 global_start_time = time.time() format_str = '{}: step {}/{} (epoch {}/{}), loss = {:.6f} ({:.3f} sec/batch), lr: {:.6f}' max_steps = len(train_batch) * opt['num_epoch'] # start training for epoch in range(1, opt['num_epoch'] + 1): train_loss = 0 for i, batch in enumerate(train_batch): start_time = time.time()
header="# epoch\ttrain_loss\tdev_loss\tdev_score\tbest_dev_score") # print model info helper.print_config(opt) # model if not opt['load']: trainer = GCNTrainer(opt, emb_matrix=emb_matrix) else: # load pretrained model model_file = opt['model_file'] print("Loading model from {}".format(model_file)) model_opt = torch_utils.load_config(model_file) model_opt['optim'] = opt['optim'] trainer = GCNTrainer(model_opt) trainer.load(model_file) id2label = dict([(v, k) for k, v in label2id.items()]) dev_score_history = [] current_lr = opt['lr'] lr_change = True global_step = 0 global_start_time = time.time() format_str = '{}: step {}/{} (epoch {}/{}), loss = {:.6f} ({:.3f} sec/batch), lr: {:.6f}' max_steps = len(train_batch) * opt['num_epoch'] writer = SummaryWriter() # start training for epoch in range(1, opt['num_epoch'] + 1): train_loss = 0