OpenNMT / OpenNMT-py

Open Source Neural Machine Translation and (Large) Language Models in PyTorch
https://opennmt.net/
MIT License
6.76k stars 2.25k forks source link

Error when translating with scores (--with_scores) option enabled #2583

Closed ncicio closed 4 months ago

ncicio commented 6 months ago

When attempting to translate with the --with_scores option enabled, an error is encountered in the OpenNMT translation module. The specific error message is:

AttributeError: 'float' object has no attribute 'item'

This error arises from the translator.py file at line 437, within a list comprehension where it attempts to access the item() attribute of a float object in trans.pred_scores.

Expected Behavior: The translation process with scores should proceed without encountering any errors, providing the translated text along with corresponding scores.

Actual Behavior: The error mentioned above is encountered, halting the translation process and preventing the retrieval of translation scores.

Additional Context:

This issue occurs specifically when translating with scores enabled; without scores, the translation process functions as expected. The error seems to stem from an attempt to access the item() attribute of a float object, which is invalid. Possible Solution: Inspect the code within the translator.py file at line 437 to identify the source of the error. Ensure that the trans.pred_scores attribute is properly handled when the --with_scores option is enabled.

Environment:

Python version: 3.9 OpenNMT version: 3.5.1 Operating System: Ubuntu

vince62s commented 6 months ago

you need to remove the .item() can you send a PR?