Neural Machine Translation › Decoding: Turning Probabilities Into Translations
Language AI HandbookAll chapters Training gives you . Decoding is the search problem:
With a maximum length and a finite vocabulary, you can enumerate all candidate strings exactly, but their number grows exponentially with the length limit. Beam search reduces how many prefixes are expanded and can miss the optimal completed sequence.
Hold model weights fixed when comparing decoders. Measure both the outputs and their costs on the same inputs.
Greedy decoding
Greedy decoding picks the highest-probability next token at each step. One locally best choice can lead to a lower-scoring complete sequence than another first choice. Use greedy when you want a one-hypothesis baseline and measure its quality and latency.
If a wider beam changes the output, compare the two sequences under the same scoring rule. Inspect whether the score increase also improves the evaluation metric.
Beam search
Maintain partial hypotheses. At each step, expand every hypothesis by every vocabulary item, score by cumulative log-probability, and keep the top overall. We covered the algorithm in Beam Search; the translation-specific details are:
- Beam size. Expanding every vocabulary item for each of active prefixes considers up to next-token candidates at a step. The latency also depends on how you batch and cache model states. Tune width using held-out translation quality and latency.
- Length normalization. Completed outputs of different lengths can rank in either order under raw likelihood. On a development set, check whether short outputs are favored. Wu et al. (2016) used this length penalty:
The normalization equals one at length one. For positive it increases with length, with linear growth only when . Tune using the decoder and development set you will evaluate.
- Coverage penalty. Wu et al. also add a term for low cumulative coverage, with nonnegative :
The inner sum is the total attention assigned to source position . The cap at one prevents any additional reward for that position once it is covered. For , a position with zero cumulative attention contributes negative infinity before any numerical flooring. When , omit the coverage term.
- Stopping. Keep completed hypotheses separate and use a stopping rule suited to the objective. Reaching the first EOS can leave better candidates unexplored, especially if final scores include length or coverage terms. Use a maximum length as an explicit fallback.
The beam search curse
Koehn and Knowles (2017) reported conditions where widening the beam decreased BLEU. A search algorithm can improve model score while worsening a separate evaluation metric. Use the same scoring rule for all returned outputs when tracking model scores across a sweep.
There are two distinct problems:
- Model error. If the model assigns a higher score to an unsuitable output, finding that output more reliably does not improve translation. Stahlberg and Byrne (2019) found that for many sentences in their studied Transformer the highest-scoring output under raw likelihood was empty.
- Search error. Under a fixed scoring rule, an approximate decoder makes a search error if it returns a sequence with a lower score than one it missed. Compare with exhaustive search on a tiny fixture if you want to isolate search error from score quality.
On a development set, compare widths and length settings and record latency. Reranking is another option: generate an -best list and score its members with a different criterion. A reference-based metric is useful offline when a reference exists. A deployment reranker needs a score it can compute from the available inputs.
Sampling
Temperature, top-, and top- change the distribution used to sample output. If the use calls for one translation, compare sampling with a deterministic baseline. Sampling is also a way to generate multiple candidates, including synthetic sources in the back-translation experiments cited above.
Constrained decoding
Write down any functional requirement that must hold even when the model gives a higher score to an output that violates it.
- Terminology constraints. A glossary may require “cloud” to be translated as “nuage” in a specified context. Post and Vilar (2018) group candidates by the number of constraint tokens they satisfy and allocate a fixed beam budget across groups. Dinu et al. (2019) instead train the model to apply supplied terms.
- Placeholders and markup. When the format requires
{name}or<b>verbatim, check the generated string explicitly. You can restrict the decoder to eligible candidates, then check that the placeholder or markup survived. The Copy Mechanism chapter covers a related learned approach. - Length control. A subtitle may have a maximum number of characters or time on screen. Put that budget in the scoring or acceptance rule and test it on generated subtitles.
- Formatting. When dates or units must follow a locale-specific convention, validate the output against the requested format and apply an appropriate formatter if needed.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!