Contrast beam search with greedy decoding in sequence models. Greedy picks highest probability each step. Beam keeps top k sequences. Why does beam avoid local maxima but not guarantee global optimal?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This is a great question about decoding strategies in sequence models. Let me think through this carefully.
Greedy Decoding
Greedy decoding works as follows:
At each time step t, select the token with the highest probability P(y_t | y_1...y_{t-1})
Continue until EOS token or max length
Problem: Local maxima
A token might have high probability at time t, but the overall sequence it leads to might have lower total probability
Example: If at t=1, token "the" has p=0.9 and token "a" has p=0.08, greedy picks "the", but maybe "a" leads to a better overall sequence
Beam Search
Beam search maintains k hypotheses (top-k sequences):
At each step, for each of the k sequences, generate all possible next tokens
Score each resulting sequence
Keep only the top k sequences by cumulative score (usually log probability to avoid underflow)
Continue until all k sequences are complete
Why does beam avoid local maxima?
By keeping k candidates, it's not locked into one choice early on
Even if greedy would pick sequence A at step 1, beam keeps A but also explores B, C, etc.
The early "mistake" in greedy gets corrected by looking ahead
However, beam search doesn't guarantee GLOBAL optimal. Why?
Finite beam width: With k sequences, we're still pruning. The globally optimal sequence could be outside the top-k at some step. Once pruned, it can never be recovered.
Pruning is irreversible: Beam search makes hard decisions. Once a sequence falls out of the top-k, it's discarded forever. The true optimal could have taken a path that looked locally suboptimal.
Example:
Step 1: Beam keeps sequences starting with [A, B, C]
Step 2: Expanding A,B,C and pruning to top-3 keeps [A→X, A→Y, B→Z]
But the path through C might eventually accumulate higher probability than any of these top-3, yet C is already gone and can't be recovered
Partial sequence scoring: We're comparing incomplete sequences using cumulative log probability. A sequence that appears weak partway through might recover with strong tokens later, but it gets pruned before that happens.
In beam search, we score sequences as: Score = log P(y_1...y_t) = Σ log P(y_i | y_1...y_{i-1}). We keep the k highest-scoring sequences at each step.
The fundamental problem is that a sequence with lower cumulative probability at step t could end up with higher probability at the final step T if it continues optimally. Beam search can't anticipate this because it only looks at current scores, not future potential.
With k=1, we get greedy search. With k=∞, we'd explore everything exhaustively. Finite k sits between these extremes but sacrifices optimality. The issue is irreversible pruning—once a sequence falls outside the top-k, it's discarded permanently and can never be reconsidered, even if it would have led to the globally best solution. This hard cutoff means we might eliminate paths that would eventually accumulate higher probability than the sequences we kept. The core issue is that beam search evaluates sequences based on their probability up to step t, but this doesn't guarantee the best final sequence. A path with lower intermediate probability could have much stronger conditional probabilities for future steps.
For instance, one sequence might accumulate 0.9 × 0.9 × 0.9 = 0.729 through step 3, then drop to 0.1 at step 4 for a total of 0.0729. Meanwhile, a pruned alternative starting with slightly lower probabilities (0.8 × 0.8 × 0.8 = 0.512) could maintain 0.8 at the final step, yielding 0.4096—substantially better overall. By discarding the second path early based on intermediate scores, beam search misses this superior outcome.
Even with k=2, the algorithm can get trapped. After selecting the top candidates at each step based on cumulative log probability, it may eliminate sequences that would eventually outperform the kept ones. The greedy ranking at intermediate steps doesn't reflect which paths lead to optimal final sequences.
For instance, Seq3 might have the lowest individual token probability initially, causing it to be dropped early. But if subsequent tokens from Seq3 have much higher probabilities than alternatives, the final sequence could be superior to what beam search actually produces. By pruning Seq3 before discovering these favorable continuations, beam search misses the globally optimal path.
Beam search mitigates local maxima by maintaining k competing hypotheses rather than committing to a single greedy choice, allowing it to recover from early mistakes. However, it can't guarantee finding the global optimum because hard pruning permanently eliminates sequences that might lead to better solutions later—the truly optimal path could fall outside the top-k at any intermediate step, making it impossible to recover without exploring exponentially more possibilities. The fundamental problem is that greedy decoding optimizes locally at each step. At position t, it picks the token that maximizes P(y_t | y_1,...,y_{t-1}), but this choice might lock you into a path where future tokens have much lower probabilities. The globally optimal sequence requires maximizing the sum of all log probabilities across the entire sequence, not just the current token. Beam search attempts to mitigate this by keeping multiple candidates, but it still makes irreversible commitments at each step—once a sequence falls out of the top k, it's gone forever, even if it could have led to a better overall solution. d explores those branches forward, which helps avoid getting trapped in locally optimal paths.
Beam search still has limitations though. Since it only tracks the top-k sequences at each step rather than exploring all possibilities, seemingly weak sequences early on might actually lead to better overall probabilities later. The exponential space of possible sequences means we're fundamentally constrained in what we can search.
The key distinction is that in sequence generation, local maxima refer to sequences where individual token swaps won't help, yet other sequences reachable through different initial choices could have higher probability. Greedy selection can become stuck in these suboptimal regions because committing to early choices forecloses better paths that only become apparent with different starting decisions.
Beam search mitigates this by maintaining multiple candidates, but with finite k it still can't guarantee finding the globally optimal sequence. The tradeoff is computational—when k approaches the full search space size, beam search approaches brute force and would find the true optimum, but that's infeasible. With practical k values, we accept some loss in optimality for tractability.
The scoring mechanism uses cumulative log probabilities rather than raw probabilities to avoid numerical underflow when multiplying many small values between 0 and 1. Converting to log space transforms products into sums, making the computation stable. Since we're working with discrete choices rather than continuous optimization, calling it a "local maximum" isn't quite precise—it's more that greedy decoding commits to suboptimal early decisions that foreclose better paths forward. Beam search sidesteps this trap by maintaining multiple candidate sequences simultaneously, allowing it to explore different branches rather than locking into a single greedy path.
That said, beam search doesn't truly eliminate this risk either. With a beam width of 1, it collapses back into greedy decoding, and even with larger widths, it still prunes possibilities and could theoretically miss globally optimal sequences.
The core tension is that beam search trades off between exploration and computation: it keeps k alternatives to reduce the chance of early commitment to poor paths, but finite k means some promising branches still get discarded. This explains why it outperforms greedy decoding while still falling short of guaranteeing the best possible solution.
Consider a concrete scenario where this limitation surfaces. Suppose at the first step, token "a" has the highest probability (0.4) and "b" has the second-highest (0.3), so with k=2 these are retained while lower-probability options like "c" are eliminated. Later, "c" might have led to much stronger continuations that ultimately produce a better final sequence than either path starting with "a" or "b", but we never discover this because "c" was pruned irreversibly in step 1. The algorithm simply cannot recover from this early pruning decision.
This reveals the fundamental trade-off: beam search explores multiple hypotheses simultaneously, making it less prone to getting trapped than greedy decoding, yet it still discards candidates that could have led to globally optimal solutions. The finite beam width means some promising branches are cut off before their true value becomes apparent. The key distinction is in how each method scores sequences. Greedy decoding selects the highest probability token at each step without reconsidering earlier choices, while beam search maintains the k most promising partial sequences based on their cumulative log probability. This difference matters because greedy can get trapped by early decisions—picking a high-probability token that leads down a lower-probability path overall, whereas beam search's alternatives provide escape routes. However, beam search still isn't guaranteed to find the optimal sequence since pruning low-scoring candidates early might discard paths that eventually become highest-probability. The tradeoff is fundamentally about computation versus optimality: beam search explores more possibilities but remains bounded by k. Greedy decoding commits to the highest-probability token at each step with no opportunity to recover from early mistakes, running in O(T·V) time. Beam search maintains k competing sequences in parallel, expanding each by all V vocabulary options and retaining the top-k by cumulative log probability—a more expensive O(T·k·V) approach that explores multiple paths simultaneously rather than locking into a single trajectory. The top-k hypothesis set functions as a form of parallel exploration, allowing different branches to demonstrate their potential rather than being discarded immediately.
With sufficient beam width, this parallel approach captures enough alternatives to sidestep the commitment problem that traps greedy decoding.
However, beam search still can't guarantee finding the globally optimal sequence because it performs irreversible pruning at each step. Even with a large k value, the algorithm permanently discards hypotheses that fall outside the top-k, and sometimes the optimal path requires exploring a token that initially scores poorly. For instance, with k=2 and five vocabulary options, a token scoring third-best at step 1 might lead to the highest-probability sequence overall, but it gets eliminated before the algorithm can discover this.
The issue compounds because intermediate scores mislead the algorithm—a sequence with mediocre cumulative probability can still lead to exceptional final scores if the conditional probabilities of future tokens are strong, yet it gets pruned based on its current standing. Additionally, the algorithm only explores a tiny fraction of the exponential space of possible sequences, so even with multiple parallel paths, it misses vast regions where better solutions might exist. Beam search sits between greedy and exhaustive search—it explores enough candidates that early pruning rarely discards the optimal path, yet keeps runtime manageable by limiting k. In practice, k=5-10 often works well for language generation because low-probability tokens at any step tend to lead nowhere anyway, though this isn't guaranteed. The tradeoff is fundamental: no finite-budget algorithm can promise optimal decoding, so beam search has become the standard compromise between quality and speed. I'm satisfied the explanation covers the key points—beam search explores multiple paths in parallel, which prevents early greedy mistakes, but the finite beam width means some optimal sequences get irreversibly pruned. The examples illustrate this tradeoff concretely.
Let me verify the complexity analysis holds up. Greedy operates in O(T·V) by maintaining a single hypothesis and scoring V tokens at each of T steps. Beam search scales to O(T·k·V) since it tracks k hypotheses and evaluates each against V possible next tokens per timestep. This confirms why beam is more expensive but more thorough than greedy. I should note that dynamic programming can find optimal sequences in some domains like speech recognition, but with large vocabularies in language modeling it becomes intractable—beam search serves as the practical middle ground for these cases. The table captures the core tradeoff: greedy is fast but commits early, beam search balances speed with better results through limited exploration, and exhaustive search is theoretically optimal but computationally infeasible. I'll standardize notation to yt for clarity and make sure the language around pruning irreversibility is precise before finalizing.
Greedy vs Beam Search: Comparing Decoding Strategies
Quick Mechanism Comparison
Aspect
Greedy
Beam Search
Hypotheses per step
1
k
Selection rule
yt=argmaxP(yt
Why Beam Avoids Greedy's "Local Maxima" Problem
Greedy gets locked into bad early decisions through irrevocable commitment:
code
Step 1: Greedy picks "the" (P=0.91)
Alternative "a" (P=0.08)
Later steps:
"the" → mediocre continuations → final P = 0.65
"a" → excellent continuations → final P = 0.87
But greedy can't recover—it committed at step 1.
Beam's solution: Maintain parallel hypotheses
code
Step 1: Keep both [the, a]
Step 2: Each generates k·V candidates → keep top-k
If "a" path proves superior, it gets promoted
Multiple branches compete, early mistakes can be outweighed
By keeping k paths alive, beam search doesn't lock into one suboptimal early choice. Different branches get chances to prove themselves over multiple steps—you essentially run k greedy searches in parallel.
Why Beam Search Doesn't Guarantee Global Optimum
The core issue: finite k means permanent, irreversible pruning.
Problem 1: Optimal Sequences Pruned Away Early
code
k=2, vocabulary size = 5000
Step 1:
Token "a": log P = -0.5 ← keep
Token "b": log P = -0.6 ← keep
Token "c": log P = -0.8 ← DISCARD (outside top-2)
Token "d": log P = -3.2 ← DISCARD
...
Step 20 (end):
Sequence starting with "a": final score = -15.2
Sequence starting with "b": final score = -15.0
Sequence starting with "c": final score = -12.1 ← BEST, but lost forever!
Once "c" is pruned, recovery is impossible—even though it would have been globally optimal.
Problem 2: Misleading Partial Scores
A sequence can look bad mid-way but recover dramatically:
code
Sequence A at step t=10:
Cumulative score: -8.2 (kept in top-k, looks "okay")
P(remaining | A) = average endings → final: -12.5
Sequence B at step t=10:
Cumulative score: -8.5 (pruned, looked slightly worse)
P(remaining | B) = exceptional endings → final: -11.1 (would be BEST!)
But B is discarded before we see its excellent continuations.
Problem 3: Exponential Space Only Partially Explored
Possible sequences: O(V^T) — exponential growth
Beam explores: O(k·T) — linear in k
Gap: Huge
code
T=100, V=50,000:
Exhaustive: 50,000^100 ≈ 10^581 (impossible)
Beam k=10: 100 × 10 × 50,000 = 50M operations (tractable)
But we sample 50M out of 10^581 possibilities—a rounding error.
The Fundamental Tradeoff
code
Beam width k:
↓ k=1 (greedy)
Fastest but most myopic
↓ k=5 (typical NLP)
Fast + good empirical results
Still suboptimal but rare failures
↑ k=∞ (exhaustive)
Optimal but O(T·V^T)—unusable
The hard truth: With deterministic decoding and finite compute, you cannot guarantee global optimality. Beam search is the practical standard precisely because it:
✅ Nearly always beats greedy's commitment trap
✅ Runs in reasonable time
✅ Provides knob to tune (k) by application needs
❌ Cannot guarantee global optimum (and no polynomial algorithm can)
In practice, k=5-10 often suffices for language tasks; k=100+ for highly constrained problems (like structured prediction).