spro / practical-pytorch

Go to https://github.com/pytorch/tutorials - this repo is deprecated and no longer maintained
MIT License
4.51k stars 1.1k forks source link

Output returning tensor instead of word index in batched_seq2seq #103

Closed alpoktem closed 6 years ago

alpoktem commented 6 years ago

Hi,

First of all thanks a lot for the great repository. I'm having a problem in the evaluation script in the batched seq2seq tutorial. This line results in the variable ni to have a tensor output instead of a word index:

# Choose top word from output
topv, topi = decoder_output.data.topk(1)
ni = topi[0][0]

This is the error I get when I call evaulate function:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-83-b00ef544095f> in <module>()
      1 #evaluate_and_show_attention("my")
----> 2 output_words, attentions = evaluate("hey")
      3 #input_lang.word2index["hey"]

<ipython-input-82-bdd5eb3ce23d> in evaluate(input_seq, max_length)
     40             break
     41         else:
---> 42             decoded_words.append(output_lang.index2word[ni])
     43 
     44         # Next input is chosen word

KeyError: tensor(1582)
alpoktem commented 6 years ago

Changed ni = topi[0][0] to ni = topi.item() and it's solved.