bentrevett / pytorch-seq2seq

Tutorials on implementing a few sequence-to-sequence (seq2seq) models with PyTorch and TorchText.
MIT License
5.32k stars 1.33k forks source link

Tut 6: TypeError with calculate_bleu_alt #175

Closed yuvaraj91 closed 2 years ago

yuvaraj91 commented 2 years ago

Using the alternate method for calculating the BLEU score in Tutorial 6, I get this error:


bleu_score = calculate_bleu_alt(test_data, SRC, TRG, model, device)

print(f'BLEU score = {bleu_score*100:.2f}')

TypeError                                 Traceback (most recent call last)
<ipython-input-50-9dbd66c5224c> in <module>()
----> 1 bleu_score = calculate_bleu_alt(test_data, SRC, TRG, model, device)
      2 
      3 print(f'BLEU score = {bleu_score*100:.2f}')

<ipython-input-49-f73b8e937593> in calculate_bleu_alt(iterator, src_field, trg_field, model, device, max_len)
     16                     if i == trg_field.vocab.stoi[trg_field.eos_token] or i == trg_field.vocab.stoi[trg_field.pad_token]:
     17                         break
---> 18                     tmp.append(trg_field.vocab.itos[i])
     19                 _trgs.append([tmp])
     20             trgs += _trgs

TypeError: list indices must be integers or slices, not str
yuvaraj91 commented 2 years ago

Tried using this suggestion but it did not work:

I fixed this issue with replace on calculate_bleu_alt function

return pred_trgs, trgs, bleu_score(pred_trgs, trgs)
like this

return bleu_score(pred_trgs, trgs)

Any tips, @bentrevett ?