def load_model(model_file, cuda): # Load model model = torch.load(model_file, map_location=lambda storage, loc: storage) if cuda: model.cuda() model.rnn.flatten_parameters() # Send extra argument with model parameters to forward function model.rnn.forward = lambda input, hidden: lstm.forward(model.rnn, input, hidden) model_original = copy.deepcopy(model.state_dict()) model.load_state_dict(model_original) return model
] gold = pandas.read_csv(args.input + '.gold', sep='\t', header=None, names=['verb_pos', 'correct', 'wrong', 'nattr']) # Load model print('Loading models...') import lstm print('\nmodel: ' + args.model + '\n') model = torch.load( args.model, map_location=lambda storage, loc: storage) # requires GPU model model.rnn.flatten_parameters() # hack the forward function to send an extra argument containing the model parameters model.rnn.forward = lambda input, hidden: lstm.forward(model.rnn, input, hidden ) model_orig_state = copy.deepcopy(model.state_dict()) log_p_targets_correct = np.zeros((len(sentences), 1)) log_p_targets_wrong = np.zeros((len(sentences), 1)) model.load_state_dict(model_orig_state) stime = time.time() output_fn = args.output + '.abl' if args.lang == 'en': init_sentence = " ".join([ "In service , the aircraft was operated by a crew of five and could accommodate either 30 paratroopers , 32 <unk> and 28 sitting casualties , or 50 fully equipped troops . <eos>", "He even speculated that technical classes might some day be held \" for the better training of workmen in their several crafts and industries . <eos>", "After the War of the Holy League in 1537 against the Ottoman Empire , a truce between Venice and the Ottomans was created in 1539 . <eos>",