Beam Search

deep-learning
sequence-models
nlp
machine-translation
beam-search
length-normalization
error-analysis
decoding
The approximate search that finds the most likely translation, the log and length-normalization refinements, and how to tell search errors from model errors.
Published

Aug 27, 2026

Sequence to Sequence Models left off with a problem. The model gives \(P(y \mid x)\) for any candidate translation, the sentence you want is the one that maximizes it, greedy search does not find it, and the space of candidates is too large to enumerate.

Beam search is the algorithm that resolves this, and it is the one most production translation systems use. The same problem appears in speech recognition, where you want the best transcript of an audio clip rather than a random one, and beam search answers it there too.

Beam Search Algorithm

Beam search has one parameter, the beam width \(B\). Where greedy search keeps the single best option at each step, beam search keeps the best \(B\). The running example uses \(B = 3\) and the sentence Jane visite l'Afrique en septembre, with capitalization ignored to keep things simple.

First Step

Run the French sentence through the encoder, then run one step of the decoder. Its softmax produces \(P(y^{\langle 1 \rangle} \mid x)\) over all 10,000 words in the vocabulary. Greedy search would take the single most likely word. Beam search takes the top three and stores them.

Say those three are in, Jane and September. With a beam width of 10 it would have kept ten instead.

Second Step

For each of those three first words, consider every possible second word.

Take in. Hard-wire \(\hat{y}^{\langle 1 \rangle}\) to in, feed it back into the decoder, and the network fragment now gives \(P(y^{\langle 2 \rangle} \mid x, \text{`in'})\). That is the probability of the second word given the French sentence and given that the first word was in.

What matters is not the most likely second word on its own but the most likely pair. By the chain rule,

\[P(y^{\langle 1 \rangle}, y^{\langle 2 \rangle} \mid x) = P(y^{\langle 1 \rangle} \mid x) \; P(y^{\langle 2 \rangle} \mid x, y^{\langle 1 \rangle})\]

The first factor was saved in step one and the second comes from this fragment, so multiplying them gives the probability of the pair. Do the same with Jane hard-wired, and again with September.

Three first words times 10,000 second words is 30,000 candidate pairs. Evaluate all of them and keep the best three.

Suppose those turn out to be in September, Jane is and Jane visits. Notice what just happened. September has been rejected as a first word entirely, so only two first words survive, while the beam still holds three pairs.

NoteThree copies of the network, not thirty thousand

Because \(B = 3\), every step instantiates three copies of the decoder to evaluate the partial fragments. Each copy produces a softmax over the whole vocabulary in one pass, so three copies cover all 30,000 options. You never instantiate 30,000 networks.

Third Step and Onward

Same again. For in September, hard-wire the first two words and evaluate \(P(y^{\langle 3 \rangle} \mid x, \text{`in September'})\) across the vocabulary. Repeat for Jane is and for Jane visits, take the best three triples, and continue.

On a small screen, scroll horizontally to follow all three steps.

A tree growing left to right over three columns. The first column holds in, Jane and September. The second holds in September, Jane is and Jane visits, with orange arrows showing that two of them came from Jane and none from September. The third holds in September Jane, Jane is visiting and Jane visits Africa. Five faint stubs fan out of every node in the first two columns, standing in for the thousands of discarded alternatives.

Three steps of beam search with a beam width of 3. Every surviving prefix is extended by all 10,000 words in the vocabulary, of which five representative discards are drawn as faint stubs at each expanded node, and only the best three extensions survive. September wins a place in the beam after step one and then loses it, because none of its continuations makes the top three.

On a small screen, scroll horizontally to inspect all three fragments.

Three stacked rows. Each row has a green encoder chain reading x superscript 1 through x superscript T sub x, feeding a purple decoder of three cells. In the first row the first two decoder cells are labeled in and September, in the second Jane and is, in the third Jane and visits. Each row's third cell emits y-hat superscript 3, annotated with the conditional probability of the third word given the input and that prefix.

The third step as three copies of the network. Each fragment runs the same encoder over the French sentence, then wires the two words of its surviving prefix into the decoder, so its final softmax gives the probability of a third word given the input and that prefix. Three copies cover all 30,000 candidate triples, because each one produces a distribution over the whole vocabulary in a single pass.

Eventually beam search reaches the end-of-sentence token, and the hope is that it settles on Jane visits Africa in September.

Notice that a beam width of 1 makes this exactly greedy search. Keeping several possibilities instead, three or ten or more, usually finds a much better sentence.

Review Questions

1. At the second step, beam search evaluates 30,000 candidate pairs but instantiates only three copies of the network. Explain both numbers.

Answer

The 30,000 is the beam width times the vocabulary size, three surviving first words each extended by 10,000 possible second words. The three is the number of distinct decoder states, one per surviving prefix. Each copy is run once with its prefix hard-wired, and its softmax output already gives the probability of every one of the 10,000 second words at once. So the fan-out is in the softmax rather than in the number of networks, and one forward pass per prefix covers 10,000 candidates.


1. After step two the beam holds in September, Jane is and Jane visits. How many distinct first words survive, and why is that not a contradiction?

Answer

Two, in and Jane. September was in the beam after step one and no continuation of it made the top three, so it is gone. There is no contradiction because the beam width bounds the number of hypotheses carried forward, not the number of distinct first words. Two of the three surviving hypotheses happen to share a first word. This is the mechanism that lets beam search recover from a bad greedy first choice, and also the mechanism that can lose the right answer if its prefix looks unpromising early.

Back to top