Beam Search › Length Normalization
Language AI HandbookAll chapters Beam Search › Length NormalizationOne section · Sections in this chapter5 / 17 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Raw log-probability scores are biased against longer sequences. Since every additional token adds a negative log-probability term, longer sequences accumulate more negative contributions and systematically score lower than shorter ones. A model using raw scores will prefer short outputs, sometimes generating incomplete or truncated translations.
To see why this matters, imagine two candidate translations of a German sentence: a five-word English translation and a ten-word English translation. If each token has an average log-probability of -0.7 (a reasonable value for a good translation), the five-word translation scores -3.5 and the ten-word translation scores -7.0. The model will always prefer the five-word translation, even if the ten-word one is linguistically better. The model effectively penalizes every word added to the output.
The length penalty addresses this by normalizing the score by sequence length. One common formulation, introduced by Wu et al. in Google's Neural Machine Translation system, uses:
where the length penalty is:
where:
- : the length of the generated sequence (number of tokens)
- : a hyperparameter controlling how strongly length is penalized, typically set to 0.6 or 0.7
- The constant 5 is a smoothing term that prevents the penalty from being too aggressive for very short sequences
When , the penalty is 1.0 for all lengths, and there is no length normalization (equivalent to raw log-probability). When , the penalty is approximately linear in length, giving nearly equal footing to sequences of different lengths. Values in between, such as 0.6, provide a moderate preference for longer outputs without going all the way to strict length normalization.
Why not simply divide by directly? The constant 5 in the numerator and denominator prevents very short sequences from being penalized too heavily and provides a smoother transition. Without it, the difference between length 1 and length 2 would be enormous (a factor of 2), while the difference between length 50 and length 51 would be negligible.
The choice of has practical consequences. A value of was calibrated empirically on production MT systems. For summarization, where you generally want outputs shorter than the source, a lower (or even ) may be more appropriate. For caption generation, where captions should have a natural length, around 0.5 to 0.8 tends to work well. Tuning on a validation set is standard practice.
Some systems also add a coverage penalty alongside the length penalty. The coverage penalty rewards sequences where attention has covered all source tokens, addressing the tendency of seq2seq models to ignore parts of the input. The combined score balances fluency (via the model's probability), adequate length, and adequate coverage.