torch.cuda.manual_seed(args.seed) with open(args.checkpoint, 'rb') as f: if args.cuda: model = torch.load(f) else: # to convert model trained on cuda to cpu model model = torch.load(f, map_location=lambda storage, loc: storage) model.eval() if args.cuda: model.cuda() else: model.cpu() dictionary = dictionary_corpus.Dictionary(args.data) vocab_size = len(dictionary) prefix = dictionary_corpus.tokenize(dictionary, args.prefixfile) def _get_predictions_inner(sentences, model, dictionary, seed, device="cpu"): """ Returns torch tensors. See `get_predictions` for Numpy returns. """ ntokens = dictionary.__len__() with torch.no_grad(): for i, sentence in enumerate(sentences): torch.manual_seed(seed) hidden = model.init_hidden(1) input = torch.randint(ntokens, (1, 1), dtype=torch.long).to(device)
print("final average: " + str(np.mean(ratings))) with open(args.results, 'w') as f: f.write(recall_str) with open(args.model, 'rb') as f: if args.cuda: model = torch.load(f) else: import warnings warnings.filterwarnings("ignore") model = torch.load(f, map_location = lambda storage, loc: storage) if args.cuda: model.cuda() model.eval() total_loss = 0 eval_batch_size = 1 hidden = model.init_hidden(eval_batch_size) dictionary = dictionary_corpus.Dictionary("CHILDES") with torch.no_grad(): if args.interactive: interactive(word = "", hidden = hidden, dictionary = dictionary, model = model) elif args.eval != "none": auto_eval(hidden = hidden, dictionary = dictionary, model = model) elif args.gen: generate(hidden = hidden, dictionary = dictionary, model = model) else: parser.print_help()