model = GaussHRED(corpus, config)

if config.use_gpu:
    model.cuda()

best_epoch = None
if not config.forward_only:
    try:
        best_epoch = train(model, train_data, val_data, test_data, config, evaluator, gen=generate)
    except KeyboardInterrupt:
        print('Training stopped by keyboard.')

config.batch_size = 32
if best_epoch is None:
    model_ids = sorted([int(p.replace('-model', '')) for p in os.listdir(saved_path) if '-model' in p])
    best_epoch = model_ids[-1]

model.load_state_dict(th.load(os.path.join(saved_path, '{}-model'.format(best_epoch))))
logger.info("Load model {}".format(best_epoch))
logger.info("Forward Only Evaluation")
# run the model on the test dataset
validate(model, val_data, config)
validate(model, test_data, config)

with open(os.path.join(saved_path, '{}_test_file.txt'.format(start_time)), 'w') as f:
    generate(model, test_data, config, evaluator, num_batch=None, dest_f=f)

end_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
logger.info('[END]'+ end_time+ '='*30)
                           val_data,
                           test_data,
                           config,
                           evaluator,
                           gen=None)
    except KeyboardInterrupt:
        print('Training stopped by keyboard.')

config.batch_size = 4
if best_epoch is None:
    model_ids = sorted([
        int(p.replace('-model', '')) for p in os.listdir(saved_path)
        if '-model' in p
    ])
    best_epoch = model_ids[-1]

model.load_state_dict(
    th.load(os.path.join(saved_path, '{}-model'.format(best_epoch))))

print("Forward Only Evaluation")
# run the model on the test dataset
validate(model, val_data, config, get_marginals=True)
validate(model, test_data, config, get_marginals=True)

with open(os.path.join(saved_path, '{}_test_file.txt'.format(start_time)),
          'w') as f:
    generate(model, test_data, config, evaluator, num_batch=None, dest_f=f)

end_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
print('[END]', end_time, '=' * 30)