What is the difference between greedy decoding and beam search for generating a sequence? Why is neither guaranteed to find the most probable sequence?

Sequence models and transformers, Decoding
Level L1 Beginner. Length Short. Nature Th Theoretical.

What's being asked

Whether you understand that both are approximate searches over an exponentially large space.

Key idea

Greedy keeps the single best token at each step. Beam search keeps the B best partial sequences (by total log-probability) at each step. The search space has ∣V∣T sequences, so both are approximate.

Steps

  1. The goal is to find arg⁡max⁡y∑tlog⁡P(yt∣y<t,x), but there are ∣V∣T candidate sequences for vocabulary V and length T.
  2. Greedy picks arg⁡max⁡wP(w∣y<t) at every step. It is cheap, but an early choice that looks best may lead to a poor continuation.
  3. Beam search with beam width B keeps B partial sequences, extends each by every token, and keeps the B best of the B∣V∣ candidates.
  4. Beam search with B=1 is greedy. Larger B costs about B times more and usually finds higher-probability sequences.
  5. Neither is exact: a sequence whose prefix falls out of the beam can never come back.

Common mistakes

How long your answer should be

4–5 sentences.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: Why does a maximum-likelihood n-gram language model fail on unseen n-grams, and what does smoothing do about it?
Next in Sequence models and transformers: How do the training objectives of BERT (masked language modelling) and GPT (causal language modelling) differ, and what does each allow the model to attend to?