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
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.
Three rules, and a self-feeding loop
Scoring a whole continuation
Greedy misses, beam finds
The likeliest sequence is atypical
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
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.
Bytewise
All courses