Neural Machine Translation › Decoding: Turning Probabilities Into Translations

Michael BrenndoerferNovember 26, 20265 min read
Language AI HandbookAll chapters
Part 27 of 67Machine Translation and Speech Translation
Chapter 191Neural Machine Translation › Decoding: Turning Probabilities Into Translations
Reading section 8 of 12 · Decoding: Turning Probabilities Into Translations · 5 minSection 8 of 12 · 5 min

Section 8 of 12 · 5 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

Training gives you Pθ(y∣x)P_\theta(y \mid x). Decoding is the search problem:

y^=arg⁡max⁡y∈Y∗Pθ(y∣x)\hat{y} = \arg\max_{y \in \mathcal{Y}^*} P_\theta(y \mid x)

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.

Maintain kk partial hypotheses. At each step, expand every hypothesis by every vocabulary item, score by cumulative log-probability, and keep the top kk overall. We covered the algorithm in Beam Search; the translation-specific details are:

  • Beam size. Expanding every vocabulary item for each of kk active prefixes considers up to kVkV 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:
lp(y)=(5+∣y∣6)α,score(y)=log⁡Pθ(y∣x)lp(y)\text{lp}(y) = \left(\frac{5 + |y|}{6}\right)^{\alpha}, \qquad \text{score}(y) = \frac{\log P_\theta(y \mid x)}{\text{lp}(y)}

The normalization equals one at length one. For positive α\alpha it increases with length, with linear growth only when α=1\alpha=1. Tune α\alpha using the decoder and development set you will evaluate.

  • Coverage penalty. Wu et al. also add a term for low cumulative coverage, with nonnegative β\beta:
cp(x,y)=β∑i=1mlog⁡ ⁣(min⁡ ⁣(∑t=1nαt,i, 1.0))\text{cp}(x, y) = \beta \sum_{i=1}^{m} \log\!\left(\min\!\left(\sum_{t=1}^{n} \alpha_{t,i},\ 1.0\right)\right)

The inner sum is the total attention assigned to source position ii. The cap at one prevents any additional reward for that position once it is covered. For β>0\beta>0, a position with zero cumulative attention contributes negative infinity before any numerical flooring. When β=0\beta=0, 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 nn-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-kk, and top-pp 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!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy