Sub topic 5
BLEU part 2: the brevity penalty
This chapter assumes you have read the previous chapter on clipped (modified) n-gram precision. You should also be comfortable with math.exp and with the idea that precision measures how much of the candidate is correct, not how much of the reference is covered.
Why clipped precision is not enough
Clipping stops a candidate from repeating one matching word many times. It does not stop a candidate from being too short. Take this reference and candidate:
- Reference:
the cat is on the mat - Candidate:
the cat
Every candidate unigram and bigram appears in the reference, so clipped precision is 1.0 at every order. A system could score perfectly by emitting only the one phrase it is surest about and dropping the rest of the sentence.
Precision punishes extra wrong words. It never punishes missing words, which is what recall does. BLEU has no recall term. The original authors added the brevity penalty (BP), a multiplier that lowers the score when the output is shorter than the reference. It is a length-based stand-in for recall.
The formula
Let c be the candidate length in tokens and r be the effective reference length. The penalty is:
BP = 1ifc > rBP = exp(1 - r / c)ifc <= r
Some implementations also return 0 when c = 0, to avoid dividing by zero. The final BLEU score is BP times the geometric mean of the clipped precisions, which the next chapter covers in full.
Three properties follow from the formula:
- It is continuous at
c = r. At that pointexp(1 - 1) = 1, so there is no jump as a candidate grows to match the reference. - It only penalizes short outputs. A candidate longer than the reference gets exactly 1.0, not a bonus.
- It decays quickly. The penalty depends on the ratio
r / c, and the exponent grows as the candidate gets shorter relative to the reference.
| Candidate length / reference length | BP |
|---|---|
| 1.00 or more | 1.0000 |
| 0.90 | 0.8948 |
| 0.80 | 0.7788 |
| 0.50 | 0.3679 |
| 0.25 | 0.0498 |
A candidate that is 20% too short loses about 22% of its score. A candidate at half the reference length keeps about 37%.
Long outputs are handled by precision, not BP
A common misreading is that BLEU penalizes outputs that are too long. It does, but through precision. Extra words that do not match the reference add to the denominator of each n-gram precision and lower it. The brevity penalty is the other half of the pair:
- Precision discourages padding and wrong words.
- BP discourages omission.
Together they approximate the balance that F1 gives between precision and recall. BLEU's version is cruder, because BP looks only at lengths and not at which words are missing.
Choosing the effective reference length
When there is a single reference, r is its length. With several references, BLEU uses the reference whose length is closest to the candidate length. If two references are equally close, the usual convention is to pick the shorter one. That convention gives the candidate the lower r and so the milder penalty. NLTK and SacreBLEU both do this.
For example, take a 10-token candidate with reference lengths 8 and 12. Both differ by 2 tokens, so r = 8 and BP = 1.0. If the shorter reference were not preferred, r = 12 would give exp(1 - 12/10) = 0.8187.
Two consequences:
- Adding references of varied lengths can raise BLEU by reducing the penalty, in addition to raising n-gram matches. Scores computed with different numbers of references are not comparable.
- The choice of
ris per sentence at the sentence level and per segment at the corpus level (see the next section).
Sentence level versus corpus level
At the corpus level, BLEU does not average the per-sentence penalties. It sums the candidate lengths over all segments to get c. It sums, for each segment, the closest reference length to get r. It then computes one BP from those two totals. Clipped n-gram counts are also summed across segments before the precisions are computed.
This matters. A system that is a little too short on some sentences and a little too long on others is not punished at the corpus level, because the totals balance. The sentence-level BLEU of a very short sentence can still be near zero.
The code below computes the closest reference length, the per-sentence penalty, and the corpus penalty for two segments.
import math
def brevity_penalty(candidate_len, reference_len):
if candidate_len == 0:
return 0.0
if candidate_len > reference_len:
return 1.0
return math.exp(1 - reference_len / candidate_len)
def closest_ref_len(candidate_len, ref_lens):
# Closest length wins; on a tie, the shorter reference wins.
return min(ref_lens, key=lambda l: (abs(l - candidate_len), l))
# BP for a 6-token reference as the candidate grows
for c in range(1, 8):
print(c, round(brevity_penalty(c, 6), 4))
# Two segments: (candidate length, list of reference lengths)
segments = [(5, [6, 7]), (10, [8, 12])]
sentence_bps = []
total_c = 0
total_r = 0
for c, ref_lens in segments:
r = closest_ref_len(c, ref_lens)
sentence_bps.append(brevity_penalty(c, r))
total_c += c
total_r += r
print("sentence BPs:", [round(bp, 4) for bp in sentence_bps])
print("mean of sentence BPs:", round(sum(sentence_bps) / len(sentence_bps), 4))
print("corpus BP:", round(brevity_penalty(total_c, total_r), 4))The loop prints 0.0067, 0.1353, 0.3679, 0.6065, 0.8187, 1.0 and 1.0 for lengths 1 to 7. The segment calculation prints sentence penalties of 0.8187 and 1.0, a mean of 0.9094, and a corpus BP of 1.0. The first segment is short (5 against 6), but the second is long (10 against 8). The corpus totals are c = 15 and r = 14, so the corpus penalty vanishes.
Seeing BP in SacreBLEU
SacreBLEU reports the penalty and the lengths it used, so you can check whether a low score comes from length or from poor matches.
import sacrebleu
hypotheses = ["the cat sat", "there is a dog in the garden"]
references = [["the cat is on the mat", "a dog is in the garden"]]
bleu = sacrebleu.corpus_bleu(hypotheses, references)
print("BLEU:", bleu.score)
print("BP:", bleu.bp)
print("hypothesis length:", bleu.sys_len)
print("reference length:", bleu.ref_len)
print("length ratio:", bleu.ratio)
print(bleu) # the signature line also shows BP and the length ratioThe printed summary has the form BLEU = score p1/p2/p3/p4 (BP = ... ratio = ... hyp_len = ... ref_len = ...). Read it in this order:
- If
BPis well below 1.0, the system output is shorter than the references. Look for truncation, early end-of-sequence tokens, or a decoding setting that favors short outputs. - If
BP = 1.0, length is not the problem. Look at the individual precisions. - If the length ratio is much greater than 1, BP cannot help you, but precision will be low.
Gotchas
- Length depends on tokenization.
candrcount tokens after the metric's tokenizer runs. Splitting punctuation into separate tokens, or segmenting Chinese characters differently, changes both lengths and so the penalty. This is one reason to use SacreBLEU's fixed tokenization. - Length matching can be gamed. A system that learns to output exactly the reference length, padded with frequent words, gets BP = 1 and small precision losses. BP alone says nothing about content.
- Beam search interacts with BP. Beam search often prefers short hypotheses. Length normalization or length penalties in the decoder are partly a response to this, and tuning them can move BLEU by a point or more without changing translation quality.
- A zero-length candidate scores 0. An empty output has no precision to multiply and no meaningful BP, so implementations return 0.
- Very short references are fragile. With
rof 3 or 4 tokens, one token of difference changes BP by a large factor. Sentence-level BLEU on such data is noisy. Corpus-level scoring is more stable.
Summary of the formula in practice
The brevity penalty is a small piece of BLEU, but it settles an important design question. BLEU is built on precision, which rewards saying only what you are sure of. BP adds the missing pressure to say enough. It does this using lengths only, so it stays cheap and easy to compute, and it shares the same blind spot as everything else in BLEU: it cannot tell whether the right content is present.
Exercise
A system translates a corpus of three segments. The candidate lengths are 8, 9 and 12. The closest reference lengths are 10, 9 and 10. Compute the corpus BP and the average of the three sentence-level BPs. Explain why they differ.
Show answer
Corpus: c = 8 + 9 + 12 = 29 and r = 10 + 9 + 10 = 29. Since c is not greater than r, BP = exp(1 - 29/29) = exp(0) = 1.0. Sentence level: the first segment gives exp(1 - 10/8) = exp(-0.25) = 0.7788. The second gives exp(0) = 1.0. The third is longer than its reference, so it gives 1.0. The average is (0.7788 + 1.0 + 1.0) / 3 = 0.9263. They differ because the corpus BP uses summed lengths, so the surplus of 2 tokens in the third segment cancels the shortfall of 2 tokens in the first. The average of sentence BPs does not allow that cancellation.
Exercise
A candidate has 10 tokens and there are two references with lengths 8 and 12. What is r, and what is BP? What would BP be if the tie were broken toward the longer reference?
Show answer
Both references differ from the candidate by 2 tokens, so the shorter one is chosen: r = 8. Since c = 10 is greater than r, BP = 1.0. If the longer reference (12) were used, c <= r would hold and BP = exp(1 - 12/10) = exp(-0.2) = 0.8187. Choosing the shorter reference on ties gives the milder penalty.
Exercise
Your system's SacreBLEU output shows BP = 1.0 and a length ratio of 1.15, yet the BLEU score is low. Which part of BLEU is responsible, and what does this suggest about the output?
Show answer
The brevity penalty is not responsible, because BP = 1.0. The low score comes from the n-gram precisions. A length ratio of 1.15 means the output is 15% longer than the references, so extra or wrong words are lowering precision at one or more n-gram orders. The output is probably wordy, repetitive or wrong in content, not truncated.