Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def featureEvaluate(encoder, decoder, char2i, pairs, use_cuda):
- correct = 0
- total = 0
- i2char = {i: c for c, i in char2i.items()}
- i=0
- print_at = random.sample(range(1, 1000), 5)
- for inp, out in pairs:
- preds = featurePredict(encoder, decoder, inp, use_cuda)
- # Squeeze off the batch_size = 1 dim
- targets = out
- total+=1
- print("Evaluating dev example %i" % i)
- if i in print_at:
- print(''.join([i2char[int(c)] for c in preds]))
- print(''.join([i2char[int(c)] for c in targets]))
- i+=1
- if targets.equal(preds):
- correct += 1
- return "Accuracy: %.2f %% \n" % (correct / total * 100)
- def featurePredict(encoder, decoder, inp, use_cuda):
- enc_out, enc_hidden = encoder(inp)
- decoder_input = Variable(torch.LongTensor([EOS_index]))
- dec_hidden = decoder.init_hidden()
- eos = 1
- preds = []
- while (eos < 2):
- dec_out, dec_hidden = decoder(decoder_input,\
- dec_hidden, enc_out, use_cuda)
- topv, topi = dec_out.data.topk(1)
- # Get the index from the tensor as an integer
- topi_int = topi[0][0][0]
- if topi_int == EOS_index:
- eos += 1
- preds.append(topi_int)
- decoder_input = Variable(topi.view(1))
- return Variable(torch.LongTensor(preds))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement