19. Translating: greedy search and beam search
The decoder gives probabilities for the next word; something still has to choose. Greedy search takes the top word every time. Beam search, as the paper did, keeps the four best beginnings at each step and decides at the end.
Beam search on the paper's model for the chosen sentence, one column per step, read from left to right. Each box is a beginning of a translation kept in the beam; a box marked with a square ended with and is finished; a line joins each box to the beginning it extends; under each column, how many candidates were dropped. The last frame ranks the finished translations by their score with the length penalty, and sets greedy search's translation beside them. Change the beam width: 1 is greedy search.
The idea
At every step the decoder (chapter 18) gives a probability to each of the 201 words. The simplest way to translate is greedy search: take the most likely word, append it, repeat. It is fast, but it can paint itself into a corner: a word that looks best now can lead to a poor sentence.
Beam search keeps several beginnings open. With a beam of 4, at every step it extends each of the 4 best beginnings by its likely next words, ranks all the candidates by the probability of the whole beginning, and keeps the best 4 again. A beginning that ends with </s> is finished and set aside. At the end the finished translations are compared.
Comparing them needs care: every word multiplies in another probability below 1, so longer translations always look less likely. The length penalty divides each translation's log-probability by a factor that grows with its length, set by α. The paper used a beam of 4 and α = 0.6.
A worked example: when the two agree, and when they do not
For “Anna has a small dog . She likes it .” with the paper's settings, the search runs 13 steps and finishes 6 translations. The best, “Anna hat einen kleinen Hund . Sie mag ihn .”, has log-probability −0.804 and score −0.447. Greedy search writes “Anna hat einen kleinen Hund . Sie mag ihn .”: the same German.
Our model is so sure of itself that on our held-out sentences greedy search and beam search agree on all but one. That one is “The old women see a car now .”: greedy search writes “Die alten Frauen sehen jetzt ein Auto .” (log-probability −0.593), but beam 4 returns “Die alten Frauen sehen jetzt Auto .” (log-probability −7.746), a worse and less likely translation.
Why? Our search, like the training script it copies, stops as soon as 4 translations have finished. Here 4 short, unlikely ones ended with </s> within 8 steps, before the likely one could, so the best translation was never finished. Pick “The old women see a car now .” in the clip and watch it happen; with a beam of 2 the likely translation wins.
The formula
- \(X,\ Y\)
- the English sentence and a German translation of it
- \(\log P(Y \mid X)\)
- the sum of the log-probabilities of Y's words, each given the words before it: always negative
- \(|Y|\)
- the translation's length in tokens
- \(\operatorname{lp}(Y)\)
- the length penalty; dividing by it favours longer translations a little
- \(\alpha\)
- how strong the penalty is: 0 switches it off; the paper used 0.6
The paper cites the length penalty without writing it out; the formula is that of the reference it cites (Wu et al., 2016), which our engine and training script use.
Why it matters
Every model that writes has to choose words from probabilities, and how it chooses changes the answer. Translation systems use beam search to find the most likely sentence; chat models usually sample instead, with a temperature (chapter 6), because the single most likely answer tends to be dull and repetitive. When an API offers “temperature 0”, it is close to greedy search.
Beam search costs more: a beam of 4 runs the decoder on 4 beginnings at every step, about four times the work of greedy search.
Where it fails
- Wider is not always better. A wider beam finds more likely sentences, but the most likely sentence is not always the best one, and a stopping rule can cut the search short, as the example shows.
- The length penalty is a knob. α was tuned by the paper's authors on development data; a different α ranks the same finished translations differently.
- A confident model needs no beam. When the top word is far ahead at every step, as on our small task, greedy and beam search agree and the beam only adds work.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
The length penalty
What is the length penalty ((5 + |Y|)/6)^α for a translation of |Y| = 7 tokens, with α = 0.6? Three decimals.
From log-probability to score
In the clip's first sentence, the best translation has log-probability −0.804 and 11 tokens with </s>. What is its score with α = 0.6? Three decimals.
A beam of one
What is beam search with a beam width of 1?
Which is more likely?
For “The old women see a car now .”, whose translation has the higher log-probability: greedy search's or beam 4's?
In the paper
Section 6.1: for the base models the authors averaged the last checkpoints, and “used beam search with a beam size of 4 and length penalty α = 0.6”, the settings chosen on the development set. They set the maximum output length to the input length + 50, “but terminate early when possible”.
In the paper: section 6.1