Greedy decoding picks the best next token. Beam search keeps k live hypotheses so a slightly worse token now can still win the sentence later.
Beam search is a bounded breadth-first walk over the tree of possible output sequences.
At every decoding step the model gives a probability over the whole vocabulary. Greedy decoding takes the
argmax and never looks back — one bad early commit poisons the rest of the sentence. Exhaustive search
would consider every sequence, which is V^T and hopeless. Beam search sits between them: expand
all k current hypotheses, score every continuation, then keep only the top k and
throw the rest away.
Because probabilities multiply, we sum log probabilities instead — it keeps the arithmetic stable and turns the objective into a plain additive score.
How many hypotheses survive each step. k=1 collapses to greedy. Translation typically uses 4–10; beyond that, quality plateaus and often degrades.
Each hypothesis carries the running sum of log P. Every candidate extension is scored as parent score + log P(next token), so ranking is a single sort.
k hypotheses × V vocabulary = k·V candidates. Sort, keep k, discard the rest. This prune is the entire reason the search stays tractable — and the reason it is not optimal.
A hypothesis that emits <eos> is moved to a finished set and stops expanding. Stop when
k are finished, then re-rank the finished set with the α length penalty.
<bos>) with score 0.<eos> leaves the beam and joins the finished pool; refill the beam from the next-best candidates.| Step | Candidate | Score (Σ log P) | Fate |
|---|---|---|---|
| t=1 | "the" | -0.22 | keep |
| t=1 | "a" | -0.51 | keep |
| t=1 | "one" | -2.90 | pruned |
| t=2 | "a cat" | -0.74 | keep |
| t=2 | "the cat" | -0.95 | keep |
| t=2 | "the dog" | -1.60 | pruned |
Read the interesting row: at t=1 greedy would have locked in "the".
Beam search kept "a" too — and by t=2 "a cat" is the leading hypothesis. That
recovery is the whole value proposition. Cost: O(k · V · T) scoring work and k parallel
decoder states in memory.
Intuition says a wider beam searches more and must win. Empirically, translation quality peaks around k≈4–10 and then falls — the beam search curse. Wider search finds sequences with genuinely higher model probability, and those turn out to be short, generic, degenerate outputs. The search got better; the objective was flawed. This is also why open-ended text generation usually abandons beam search entirely for sampling (top-k, nucleus): for creative text, the highest-probability sequence is the boring one.