Fix BLEU zero division errors for empty inputs#764
Open
zanvari wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the issue reported in #601, where the BLEU metric raises a
ZeroDivisionErrorwhen either the reference or prediction tokenizes to an empty sequence.The issue can occur when evaluating inputs such as:
chr(12)/ form-feed).In these cases, BLEU computation may reach the brevity penalty calculation with either
reference_length == 0ortranslation_length == 0, leading to a division-by-zero error instead of returning a valid metric output.Fixes #601.
Reproduction
The following examples previously raised
ZeroDivisionError:Errors were raised during the computation of the BLEU brevity penalty.
Changes
Added handling for cases where:
reference_length == 0translation_length == 0Return a valid BLEU result with a score of
0.0instead of raising an exception.While implementing this fix, I also vendored
nmt_bleu.pyinto the BLEU metric directory and replaced the external import with a local import. This removes the external GitHub dependency and incidentally resolves the offline-loading issue discussed in #565.Rationale
When either the reference length or translation length is zero, BLEU is not meaningfully defined. Returning a BLEU score of
0.0is preferable to raising an exception because:Validation
I verified the fix using the examples above.
Empty-tokenized reference
Result:
{ "bleu": 0.0, ... }Empty prediction
Result:
{ "bleu": 0.0, ... }Normal BLEU computation
Result:
{ "bleu": 1.0, ... }Testing
tests/test_load.pysuccessfully.If maintainers prefer, I would be happy to split the offline-loading change related to #565 into a separate PR and keep this PR focused solely on the
ZeroDivisionErrorfix.