pytorch_basic_nmt icon indicating copy to clipboard operation
pytorch_basic_nmt copied to clipboard

Decode error

Open songyang-dev opened this issue 4 years ago • 2 comments

Hello, I'm using your code for a project that involves translating English to UML class diagrams. During the decoding, I have this error.

$ python nmt.py decode model.bin ../../processed/grouped/test.txt decode.yuml
load test source sentences from [../../processed/grouped/test.txt]
load model from model.bin
Decoding:   0%|                                                         | 0/26 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "nmt.py", line 785, in <module>
    main()
  File "nmt.py", line 779, in main
    decode(args)
  File "nmt.py", line 750, in decode
    hypotheses = beam_search(model, test_data_src,
  File "nmt.py", line 722, in beam_search
    example_hyps = model.beam_search(src_sent, beam_size=beam_size, max_decoding_time_step=max_decoding_time_step)
  File "nmt.py", line 353, in beam_search
    new_hyp_sent = hypotheses[prev_hyp_id] + [hyp_word]
TypeError: list indices must be integers or slices, not float
(base) 

songyang-dev avatar Apr 29 '21 03:04 songyang-dev

I confronted the same issue. I guess the reason is that, at Line 340, you changed the "prev_hyp_ids = top_cand_hyp_pos / len(self.vocab.tgt)" to something like "prev_hyp_ids = top_cand_hyp_pos / (1.0*len(self.vocab.tgt))". This leads to the TypeError (TypeError: list indices must be integers or slices, not float) that you mentioned in the issue.

Instead, you may change "prev_hyp_ids = top_cand_hyp_pos / len(self.vocab.tgt)" at Line 340 to "prev_hyp_ids = top_cand_hyp_pos // len(self.vocab.tgt)". This will give a BLEU at 28.18.

BoyuanFeng avatar May 14 '21 18:05 BoyuanFeng

Hello, I didn't change anything in the repo. I found a quick fix and it's to add a int type cast to the line where the error happens, since it's a list index problem.

songyang-dev avatar May 25 '21 03:05 songyang-dev