Beam Search: Look a Few Steps Ahead Before Choosing
Last lesson left a trap: greedy always grabs the biggest option in front of it, so one misstep can ruin the whole path. The fix is almost cutely simple—don’t lock in yet; keep several candidate paths walking forward together, then compare total scores at the end. That’s Beam Search. The word lattice below puts three strategies on the same stage.
The start is fixed as “This spot's”, then 4 steps right, 3 candidate words each step. Click the three buttons in order, and watch three things: greedy (red) grabs probability-0.5 “food” on step one, but the path narrows after; Beam=2 (blue) keeps two paths alive and greys out the pruned ones; Beam=3 even keeps step-one’s 0.2 “decor”—whose cumulative score wins? And how far apart are the compute counters at the bottom right?
😤 Greedy · Cumulative
🔦 Beam=2 · Cumulative
🔦🔦 Beam=3 · Cumulative
The whole idea of Beam Search
At each step you don’t pick one path—you keep the top-k highest-scoring paths (k = beam width) and walk them all forward; at the finish you compare cumulative totals and hand in the winner. When k=1 it collapses to greedy; when k is infinite it’s exhaustive search of every path—Beam Search is the slider between greedy and exhaustive search.
Width = the compute-for-quality knob
Each +1 on k means a whole extra row of candidates every step. Translation systems often use k=4~10: beyond that, quality gains shrink while the bill rises linearly. The engineering question was never “can we be more optimal?”—it’s “is this bit of optimality worth the compute?” The BFS/DFS fight in the maze lesson is the same choice in different clothes.
🌍 The classic decoder for machine translation and speech recognition. When translating a sentence, picking “The” vs “A” for the first word can sway how fluent the whole line feels—greedy often produces sentences where every word is “right” but the glue feels off. Beam Search keeps several openings alive, then picks the highest whole-sentence probability; it was the default in the neural-MT era. Speech recognition is the same: keep near-homophone candidates first, and let later context split “facts” from “fax.”
🧠 Same intuition as “reasoning models think first, then answer.” The Absolute Beginner Part covered deep-thinking models: before answering they generate a long thinking trace, try a few lines of thought, self-reject, then pick the best reply. That philosophy lines up with Beam Search—explore a few more paths before you “put pen to paper,” trading extra compute for a better final answer. The difference is reasoning models explore in natural language and are far more flexible—but the “compute for quality” ledger is the same one as those three counters on the lattice.
What this lesson wants to share
- One greedy misstep cascades: step-one’s 0.5 “food” looked tasty; the whole path only scored 0.072
- Beam Search = keep several candidate paths, compare totals at the end: k=1 is greedy, k=∞ is exhaustive search
- Wider is more accurate—and more expensive: 0.072 → 0.098 → 0.101, compute 12 → 21 → 30
- Classic decoder for translation and speech recognition; reasoning models’ “think first, then answer” is the modern version of the same philosophy
- The real engineering question: not “can we be more optimal?” but “is this bit of optimality worth the compute?”