Beam Search Decoding
Picking the single best-looking word at every step of a generated sequence often leads somewhere worse than keeping several plausible partial sequences alive at once and comparing them at the end.
Prerequisites: Sequence-to-Sequence Encoder-Decoder Models, Teacher Forcing and Exposure Bias
A model that generates a sequence one step at a time — words in a translation, moves in a forecast path — has to choose an output at every step, and each choice narrows what can come after it. Always taking the single highest-probability next step, called greedy decoding, is fast but shortsighted: a slightly lower-probability first step might open the door to a much better overall sequence, while the greedy choice locks you into a mediocre one. The question decoding has to answer is how much of that future to look at before committing.
The analogy: exploring a maze with a flashlight
Greedy decoding is like walking through a maze and, at every junction, taking whichever corridor looks brightest right now, never glancing ahead. You might confidently walk into a dead end that a slightly darker corridor would have avoided. Trying every possible full path guarantees the best route but is far too slow. Beam search is the middle ground: at every junction, keep a small fixed number of your most promising partial paths alive, explore one step further from each, and prune back to that same number of best candidates. You never see the whole maze, but you also never commit to one path before comparing it against a handful of live alternatives.
The mechanics: keeping a beam of candidates
Beam search keeps a beam width of partial sequences at every step. At each step, for every one of the current partial sequences, the model scores every possible next token, producing many candidate continuations; only the candidates with the highest total sequence probability survive to the next step. Sequence probability is the product of per-step probabilities, which is normally handled as a sum of log-probabilities for numerical stability:
In words: the log-probability of a full generated sequence is just the sum of the log-probabilities of each step, given everything generated so far — summing logs instead of multiplying raw probabilities avoids numbers shrinking to zero over long sequences. Greedy decoding is the special case ; exhaustive search is the (usually infeasible) case where equals every possible sequence.
Worked example 1: beam width 2 on a 3-step toy sequence
Suppose at step 1 the model assigns , . Greedy picks A and never looks back. Beam search with keeps both alive. At step 2, continuing from A gives best next-step probability (running product ), while continuing from B gives (running product ). The B-branch, which greedy discarded at step 1, is now ahead. Beam search evaluates step 3 from both and reports whichever has the highest final product — possibly descending from B, something a greedy decoder could never discover.
Worked example 2: why beam width has diminishing, then negative, returns
Say translating a sentence, scores 0.62 on a quality metric, scores 0.71, scores 0.715, and scores 0.70. Quality improves sharply from 1 to 5, barely from 5 to 20, then drops at 100 — a very wide beam starts favoring sequences that are short or generic, simply because raw summed log-probability (always negative) penalizes length, an unrelated side effect of the scoring, not better content. Practical systems fix this with a length-normalization term, and beam widths of 4–10 usually capture most of the benefit.
Beam search's summed log-probability score behaves like this curve: it grows with more (correct) steps but the rate of improvement from adding more search width flattens out — most of the gain comes from the first few candidates you keep alive.
What this means in practice
Beam search is the standard decoding strategy wherever a sequence model must commit to a full discrete output — machine translation, text generation, and any forecasting setup that generates a path token-by-token (e.g. discretized price-move sequences). It is more expensive than greedy decoding by roughly a factor of the beam width, so it trades compute for output quality, and that tradeoff interacts with Bahdanau and Luong Attention, since better per-step attention makes even a narrow beam more reliable.
Beam search keeps several of the most promising partial sequences alive at each step instead of committing to the single best-looking next token, trading extra compute for a better chance of finding the highest-probability full sequence — but wider is not always better once length effects on the scoring kick in.
Practice
- Walk through beam search with and confirm it reduces exactly to greedy decoding.
- If two branches have running log-probabilities and after step 2, which one survives if ?
- Explain in one sentence why summed log-probability without length normalization biases beam search toward shorter outputs.
The common confusion is assuming a wider beam always gives a better sequence. Beam search only ever explores an approximation of the full search space, and widening it can change which biases dominate — particularly the length bias from summing log-probabilities — rather than monotonically improving quality. A wider beam guarantees you consider more candidates, not that the metric you actually care about (translation quality, forecast accuracy) keeps improving; always check quality against beam width on held-out data instead of assuming bigger is better.
Related concepts
Practice in interviews
Further reading
- Sutskever, Vinyals & Le, Sequence to Sequence Learning with Neural Networks (2014)
- Wu et al., Google's Neural Machine Translation System (2016)