Skip to content

Fix swapped hypothesis and reference in TER - #3479

Open
Kayvan-Zahiri wants to merge 2 commits into
Lightning-AI:masterfrom
Kayvan-Zahiri:fix-ter-argument-order
Open

Fix swapped hypothesis and reference in TER#3479
Kayvan-Zahiri wants to merge 2 commits into
Lightning-AI:masterfrom
Kayvan-Zahiri:fix-ter-argument-order

Conversation

@Kayvan-Zahiri

Copy link
Copy Markdown

What does this PR do?

Fixes #3478

_compute_sentence_statistics called _translation_edit_rate(tgt_words, pred_words), but that function is declared (pred_words, target_words) and treats argument two as the reference: it builds _LevenshteinEditDistance(target_words) (parameter name reference_tokens) and shifts argument one against it. So TER was computed in the wrong direction.

Levenshtein distance is symmetric, so most scores were unaffected and the existing tests passed. Two things are not symmetric:

  • The shift search. hello hello the a dog against jumps dog lazy the scored 1.0; sacrebleu gives 1.25.
  • The empty guard. if len(target_words) == 0: return tensor(0.0) was testing the prediction, so a system that transcribed nothing scored 0.0, the best possible TER.

The fix is the argument order alone.

Verification

sacrebleu is the reference implementation named in the metric's docstring, and _reference_sacrebleu_ter in the existing tests already uses it as ground truth.

case before after sacrebleu
empty hypothesis vs 3-word reference 0.000 1.000 1.000
asymmetric shift case 1.000 1.250 1.250
simple swap 0.500 0.500 0.500

unittests/text/test_ter.py: 26 passed, 6 skipped. All four pre-existing empty-input tests pass unchanged. Both new tests fail on master with assert tensor(0.) == tensor(1.) and assert tensor(1.) == tensor(1.2500).

Not included, deliberately

An empty reference ([[""]]) returns tensor(0.) where sacrebleu returns 1.0, and _compute_ter_score_from_statistics's docstring says "1 if reference_length == 0". That comes from the num_edits > 0 conditions in that function rather than the argument order; correcting the order just moves which input reaches it. I left it out to keep this a one-line correctness fix, and can send it separately if you want it changed.

I have not added a CHANGELOG entry yet since the number was not known; happy to push one.

_compute_sentence_statistics called _translation_edit_rate(tgt_words,
pred_words), but that function treats its second argument as the reference:
it builds _LevenshteinEditDistance(reference_tokens=...) from it and shifts
the first argument's words against it.

Levenshtein distance is symmetric so most scores were unaffected, but TER's
shift search is not, and the empty-input guard fired on the prediction, so an
empty hypothesis scored 0.0, a perfect TER.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TER: hypothesis and reference are passed in swapped order, so an empty hypothesis scores a perfect 0.0

1 participant