def main(): global opt train_from = opt.train_from if opt.train_from: train_from = True checkpoint = torch.load(opt.train_from) _ep = opt.epochs opt = checkpoint['opt'] opt.epochs = _ep print("Loading dictrionary from ", opt.dict) vocab_dict = torch.load(opt.dict) print("Loading train data from ", opt.traindata) train_data = torch.load(opt.traindata) print("Loading valid data from ", opt.validdata) valid_data = torch.load(opt.validdata) train_dataset = reader.Dataset(train_data, opt.batch_size, True) valid_dataset = reader.Dataset(valid_data, opt.batch_size, True, volatile=True) print(' * vocabulary size = %d' % (vocab_dict.size())) print(' * number of training samples. %d' % len(train_data['answers'])) print(' * maximum batch size. %d' % opt.batch_size) print('Building model...') model = reader.AoAReader(vocab_dict, dropout_rate=opt.dropout, embed_dim=opt.embed_size, hidden_dim=opt.gru_size) # no way on CPU model.cuda() if train_from: print('Loading model from checkpoint at %s' % opt.train_from) chk_model = checkpoint['model'] model.load_state_dict(chk_model) opt.start_epoch = checkpoint['epoch'] + 1 optimizer = torch.optim.Adam(model.parameters(), lr=opt.learning_rate, weight_decay=opt.weight_decay) if train_from: optimizer.load_state_dict(checkpoint['optimizer']) nParams = sum([p.nelement() for p in model.parameters()]) print('* number of parameters: %d' % nParams) trainModel(model, train_dataset, valid_dataset, optimizer)
def main(): print("Loading dict", testopt.dict) vocab_dict = torch.load(testopt.dict) print("Loading test data") test_data = torch.load(testopt.testdata) print("Loading model from ", testopt.model) ckp = torch.load(testopt.model) opt = ckp['opt'] model_state = ckp['model'] if opt.gpu: torch.cuda.set_device(opt.gpu) test_dataset = reader.Dataset(test_data, opt.batch_size, True, volatile=True) print(' * vocabulary size = %d' % (vocab_dict.size())) print(' * number of test samples. %d' % len(test_data['candidates'])) print(' * maximum batch size. %d' % opt.batch_size) print('Building model...') model = reader.AoAReader(vocab_dict, dropout_rate=opt.dropout, embed_dim=opt.embed_size, hidden_dim=opt.gru_size) # no way on CPU model.cuda() # load state model.load_state_dict(model_state) print('Evaluate on test data') answers = evalulate(model, test_dataset, vocab_dict) with open(testopt.out, 'w') as out: print('\n'.join(answers), file=out)