All courses

Greedy, beam search and degeneration

Contrast greedy decoding, beam search and sampling. Explain why maximizing likelihood produces repetitive, bland loops in open-ended text.

Builds on: Autoregressive generation loop

▶ Watch this lesson Start the whole course

What the animation says

Turning odds into a token

At every step, the model hands you a probability for each possible next token. Something has to turn that list into one actual token. Greedy takes the top token, every time. Fast, and always the same. Beam search keeps a few partial texts with the best total probability, and extends each one. Sampling draws the token at random, weighted by those probabilities. Greedy and beam chase the likeliest text. That sounds ideal. Often it isn't.

Greedy commits early

Here's a tiny tree. The first word is The, at point five, or A, at point four. Greedy takes The, because point five beats point four. After The, probability spreads out. The best next word is cat, at only point three. So greedy's text, The cat, has probability point five times point three: point one five. But after A, dog has point nine. Beam keeps both first words. Which full path wins?

Beam scores whole paths

A dog: point four times point nine is point three six. More than double The cat. Beam compares full paths, so a weaker first word can still win. In practice we add log probabilities instead of multiplying. Each log is zero or below. So every extra token can only lower the score, and beams tend to favour texts that stop early. The price is about one forward pass per kept path, each step, instead of one. Your turn. Beam width two, four extended paths. Tap the two that beam keeps.

Repetition feeds itself

Beam kept the two scores closest to zero. Now, a failure both rules share. Real text often repeats phrases, so the model learned that a phrase already in context is likely to recur. Watch. I think so. The next I think so gets likelier. Then likelier again. A rule that always takes the most probable token takes the repeat, every time. The loop locks in. Which rule could break out of it?

The likeliest is atypical

Sampling can step off the repeat, because it doesn't always take the top token. Blandness has a second cause. Flip a coin, heads sixty percent of the time, a hundred times. The single likeliest sequence is all heads. You will essentially never see it. One sequence with sixty heads is ten million times less likely. But there are so many that together they show up eight percent of the time. Text works the same way. The safest word every time beats any one human-like text, and reads nothing like one.

Remember this

So, greedy and beam search both chase the most probable text. That works well when one correct answer exists, like translation or a short answer. In open-ended writing, the likeliest text is bland, and repetition feeds on itself. That failure is called degeneration. It's why open-ended generation samples instead.

Why not just pick the likeliest text?

The generation loop needs a rule for turning $p_\theta(x_{t+1}\mid x_{\le t})$ into a token. The obvious rule is to take the most probable token. A stronger version searches for the most probable *whole* continuation. For translation this works well. For stories or essays it produces bland text that often falls into loops like *"I think so. I think so. I think so."* Why does chasing probability go wrong?

Three rules, and a self-feeding loop

**Greedy** takes the top token every step. It is fast and deterministic, but it commits early. **Beam search** keeps the $b$ partial texts with the highest total probability so far and extends each one. **Sampling** draws the token at random from $p_\theta$. Why loops? Real text often repeats earlier phrases, so the model learned that a phrase already in the context is likely to recur. Each repeat raises the probability of the next repeat. A probability-maximizer always takes it, so the loop locks in.

Scoring a whole continuation

Score a continuation of the prompt by summing over the generated positions only: $$\log p_\theta(\text{continuation})=\sum_{t\,\in\,\text{generated}}\log p_\theta(x_t\mid x_{<t})$$ Here $x_{<t}$ includes the prompt. **Greedy:** $x_{t+1}=\arg\max_v z_t[v]$. **Beam, width $b$:** extend each of the $b$ kept sequences by all $V$ tokens, then keep the $b$ with the highest summed log-probability. **Sampling:** $x_{t+1}\sim\mathrm{softmax}(z_t)$. Every term is $\le 0$, so each extra token can only lower the score. Beams therefore favor texts that emit the end-of-text token early.

Greedy misses, beam finds

Step 1: *The* 0.5, *A* 0.4. After *The*, probability is spread out and the best next token is *cat* at 0.3. After *A*, *dog* has 0.9. **Greedy:** picks *The* (0.5 > 0.4), then *cat*. So *The cat* $=0.5\times0.3=0.15$ (log $-1.90$). **Beam, $b=2$:** it keeps both first words, then compares whole paths. *A dog* $=0.4\times0.9=0.36$ (log $-1.02$). Beam wins. The cost is about $b$ forward passes per step instead of one.

The likeliest sequence is atypical

Bland text has a second cause. Flip a coin with $P(\text{heads})=0.6$ 100 times. The single most probable sequence is all heads, yet you will essentially never see it. The printout shows the contrast. All heads ($6.5\times10^{-23}$) is about $10^7$ times likelier than any one sequence with 60 heads ($5.9\times10^{-30}$). But there are so many 60-head sequences that together they occur 8% of the time. Likewise, a text that picks the safest word every time beats any one human-like text in probability, yet looks nothing like human writing.
from math import comb
p, n = 0.6, 100
mode = p**n                      # all heads: the most likely single sequence
one_typical = p**60 * (1-p)**40  # one specific sequence with 60 heads
all_60 = comb(n, 60) * one_typical
print(f"{mode:.1e} {one_typical:.1e} {all_60:.3f}")
# 6.5e-23 5.9e-30 0.081

Remember this

Greedy and beam search chase the most probable text. They are good when one correct answer exists, such as translation or short answers. In open-ended writing the most probable text is bland, and repetition feeds on itself. This failure is called degeneration. It is why open-ended generation samples instead.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.