Prerequisites worth re-linking if a trap trips you: 3.5.12-Sequence-to-sequence-models, 3.4.8-Temperature-sampling, 4.2.5-BLEU-score. (Beam search operates purely on the decoder's per-step probability distribution, so it does not require attention — attention only improves the probabilities being searched, it is not part of the search itself.)
The traps below hinge on three moving parts — expansion, scoring, and pruning. Look at them first as a picture, then as a flowchart, so the words "beam", "candidate", and "the normalization step" have concrete meaning.
Figure 1 — one step of beam search. The left column is the current beam (k=2 live sequences). Each is expanded by allV vocabulary tokens (middle), producing k×V candidates. We score them, sort, and keep only the top k (right); the faded rows are pruned — gone forever.
Figure 2 — where normalization lives. The cumulative value carried inside each beam is the raw running sum ∑logP. Normalization by Tα is applied only as the sort key at the pruning gate — it never overwrites the stored cumulative score. This figure shows exactly that split.
Figure 3 — the control flow. The same procedure as a flowchart, so you can point to "the normalization step" and "the early-stop test" precisely.
Figure 4 — why the length penalty exponent α matters. The raw sum ∑logP drops with length purely because more negative terms are added. Dividing by T1 removes that slope but can over-reward short high-average fragments; a fractional α sits between. The curves make the trade-off visible.
Beam search with a large enough k is guaranteed to find the globally most probable sequence.
False — unless k≥VT (exhaustive), beam search still prunes. Any true-best sequence whose early token falls outside the current top k is dropped forever (see the faded rows in Figure 1), so it stays an approximate search.
Greedy decoding is just beam search with k=1.
True — with only one beam kept, "expand and keep the top 1" is exactly "pick the single most probable next token", which is the definition of greedy.
Increasing k can never make the returned sequence worse in log-probability.
True for the raw un-normalized score — a bigger beam is a superset of a smaller one's survivors, so the max-scoring survivor can only stay the same or improve. But translation quality (BLEU) can drop, because the extra survivors are often mediocre.
The score of a partial sequence is a probability between 0 and 1.
False — the score is a sum of logs, which is ≤0 and unbounded below. Only exp(score) is the probability in [0,1].
Two sequences of different lengths can be fairly compared by their raw summed log-probability.
False — every extra token adds a negative term (Figure 4's falling raw curve), so longer sequences are systematically penalized; that is exactly why we divide by Tα before comparing.
With α=0 the length penalty term Tα equals 1, so scoring reduces to the raw sum.
True — T0=1 for any T, so dividing by it changes nothing; α=0 means no normalization.
Beam search always runs for exactly max_length steps.
False — it stops early once all live beams have emitted <END> (the "yes" branch in Figure 3); max_length is only a safety cap for sequences that never terminate.
A sequence that has already emitted <END> still keeps getting expanded token-by-token.
False — finished beams are set aside (carried as-is into candidates); expanding past <END> would waste compute and produce meaningless tail tokens.
"We normalize by dividing the score by len(seq), counting <START>."
Error: <START> is a fixed marker with no predicted probability, so it contributes no log-prob term. We divide by len(seq) - 1 (the generated-token count T), else the divisor and the number of summed terms disagree.
"Since argmaxP=argmaxlogP, taking logs changes which sequence wins."
Error: it's the opposite — because log is monotonic increasing, taking logs preserves the ranking. We use logs only for numerical stability (avoiding underflow), not to change the winner.
"Beam search at step t keeps the top k of the k sequences, one best next token each."
Error: it keeps the top k out of k×V candidates (each of the k beams tries allV tokens, as in Figure 1's middle column). Keeping the best token per beam would just be k parallel greedy runs and could never let one beam overtake another.
"With k=100 we explore more, so we always get a higher BLEU than k=5."
Error: quality typically peaks around k=4–10; beyond that, near-duplicate long sequences crowd out better short ones and BLEU can fall. More search = better output.
"Length penalty with α=1 divides by T, so it always prefers longer sentences."
Error: α=1 takes the average log-prob per token, which removes the length bias but does not add a preference for length. Fractional α<1 divides by a smaller number than T, so it cancels less of the length penalty than full averaging — leaving a residual reward for length. This is a tuning heuristic, not a law: the GNMT paper (Wu et al., 2016) reports α≈0.6–0.7 working well for their translation setup, and it must be re-tuned per task.
"We should compute the normalized score at every expansion inside the loop and use it as the running cumulative value."
Error: the cumulative value stored per beam is the un-normalized running sum (Figure 2, left); normalization by Tα is applied only as the sorting key at the pruning gate (Figure 2, right), so early tokens aren't repeatedly re-scaled.
"Complexity is O(VT) because there are VT possible sequences."
Error: the whole point of beam search is that it never enumerates all VT sequences. It touches O(kTV) candidates — the pruning is what makes it tractable.
Why use log-probabilities instead of multiplying raw probabilities?
Products of many small probabilities underflow to 0 in floating point; summing their logs stays in a representable range, and since log is monotonic the ranking is unchanged.
Why does numerical stability ever need the log-sum-exp trick if beam search only adds log-probs?
The addition of log-probs is safe on its own, but the softmax that produces each token's probability sums exponentials over the whole vocabulary; log-sum-exp shifts by the max first so no exi overflows or underflows to 0.
Why does a purely greedy path fail on "Je suis étudiant"?
Greedy is myopic — it locks in the locally best token at step 2 ("student" directly), never seeing that the slightly-less-obvious "a … student" phrasing yields higher total probability. Local optima ≠ global optimum.
Why divide by Tα rather than by T or not at all?
Dividing by T (α=1) over-corrects and lets deceptively-high-average short fragments win; not dividing (α=0) crushes long sequences. A fractional α interpolates to balance the two failure modes (Figure 4).
Why can "The dog ran" survive in a k=3 beam even though "The dog" started worse than "The cat"?
Beam search scores whole partial sequences, not per-step ranks. A high logP(ran∣The dog) can lift the cumulative score of a weaker prefix back into the top k — this is beam search's ability to recover from a bad early choice.
Why not just set k=VT and get the exact answer?
VT is astronomically large (e.g. 50,00020), so exhaustive search is intractable. Beam search deliberately trades exactness for a fixed O(kTV) cost.
Why might a min-length constraint be needed?
Length normalization plus the <END> token can make premature termination score well, so the model emits <END> too early. Forcing at least n generated tokens blocks this collapse into empty or stub outputs.
Why does n-gram blocking help in summarization but matter less in short translation?
Long generations tend to loop, repeating a phrase; blocking repeated n-grams breaks the loop. Short outputs rarely have room to repeat, so the fix is mostly unnecessary there.
What does the beam contain at step 0, before any token is generated?
A single entry: ([<START>], 0.0) — one beam holding just the start marker with cumulative log-prob 0 (since log1=0, "we're certain we started").
What happens when k>V?
You cannot keep more distinct sequences than there are tokens to expand at the first step, so at step 1 you have at most V candidates; effective beam width is capped by the number of available continuations.
If every next-token probability is exactly equal (a uniform distribution over V), how does beam search behave?
All expansions get identical log-prob log(1/V), so the top-k selection is a tie — the algorithm falls back to arbitrary tie-breaking and explores k essentially random paths.
What is the score of a sequence containing a token with predicted probability 0?
log0=−∞, so that whole partial sequence gets score −∞ and is pruned immediately — a zero-probability token is effectively forbidden.
If a beam emits <END> at step 1 (a length-1 output), what is its T for normalization?
T=1 generated token (the <END>), since <START> is not counted; its normalized score is just its single log-prob divided by 1α=1.
What does a negative length penalty exponent α<0 do?
Dividing by Tα with α<0 is the same as multiplying by T∣α∣, which makes longer (already-negative) scores even more negative — it actively punishes length. It is the wrong sign for encouraging complete sentences and is essentially never used.
Is dividing by Tα the only length-normalization scheme?
No — an alternative is the GNMT penalty functionlp(T)=(5+1)α(5+T)α, which smooths the divisor so very short sequences aren't over-rewarded; another is a simple additive per-token reward +β at each step. All aim at the same length-bias problem via different curves.
When all k beams end with <END> at the same step, what does the algorithm return?
It stops and returns the k finished sequences, ranked by their length-normalized scores — the top one is the final decoded output.
How does temperature sampling differ from beam search at a conceptual level?
3.4.8-Temperature-samplingrandomly samples tokens (introducing diversity/creativity), whereas beam search deterministically searches for high-probability sequences; one optimizes surprise, the other optimizes likelihood.