Predict the effect of decoding settings
Predict how temperature, top-k, top-p, greedy and beam decoding change diversity, repetition and error rates. Explain why the same model can seem creative or deterministic depending on these settings.
Builds on: Top-k and nucleus (top-p) sampling, Greedy, beam search and degeneration, Exposure bias and compounding errors
What the animation says
Same weights, two personalities
Same model, same weights. At temperature zero, it repeats itself, sometimes in loops. At one point two, it's varied, sometimes unhinged. The model didn't change. Each step, it hands over the same scores, called logits. Mat scores two, rug one, floor zero. Potassium, a derailing token, scores minus three. Decoding picks a token with two dials: temperature for sharpness, and a cut for which tokens are allowed. Raise temperature from one to two. What happens to mat's share?
Temperature reshapes the same scores
It shrinks. Temperature divides every logit before turning them into probabilities. At temperature one, mat gets sixty-six percent, and potassium about half a percent. Halve the temperature: gaps double, mat climbs to eighty-seven percent, and potassium nearly vanishes. Double it: gaps halve. Mat falls to forty-nine percent, and potassium rises to four percent. Four percent sounds small. But a story is a hundred steps, not one. At four percent per step, what's the chance of at least one derail in a hundred tokens?
Slips compound; sharpness loops
About ninety-eight percent. A clean run needs every step clean, so the chances multiply. Point nine six, a hundred times, is under two percent clean. At temperature one, thirty-six percent derail. At one half, under half a percent. And one bad token can't be undone. Every later step builds on it. But go too sharp, and the model picks its top choice every time. Long text falls into loops, and its favourite mistake repeats.
The second dial: which tokens are allowed
The second dial doesn't reshape. It cuts. Tokens outside the allowed set get zero. Top-k keeps the k most likely tokens. Top-p keeps the smallest set whose probabilities add up to at least p. Temperature goes first, then the cut. Then the survivors are rescaled to sum to one. Try top-p point seven at temperature one. Mat alone is sixty-six percent, short. Add rug: ninety. Stop. Now temperature two, top-p point nine. Tap the last token that stays in.
Top-p adapts to confidence
Floor. Seventy-eight percent isn't enough, so floor joins at ninety-six, and potassium is cut. Zero derails, three options left. Top-p also changes with the model's confidence. This function tempers, then cuts. On a confident step, the top token holds ninety-seven percent. Top-p keeps just that one, exactly like greedy. On an unsure step, nothing reaches ninety until the last token, so all four stay. Temperature rescales every step the same way. Top-p reacts step by step. Greedy decoding, a long open-ended essay. What goes wrong?
Predict before you run
It loops. Greedy and beam search chase the likeliest path, so long open text repeats. So: low temperature, small k or p, greedy, or beam. Less variety, more repetition, few random slips, and the same top mistake every time. High temperature with no cut: variety, plus tail picks that pile up with length. Truncation keeps the variety and cuts the tail. The model is fixed. Your settings choose how much of its uncertainty you see.
Two dials on a fixed distribution
What actually gets sampled
Temperature vs. a derailing token
Top-p adapts to confidence
import numpy as np
def dist(z, tau=1.0, p=1.0):
q = np.exp((z - z.max()) / tau); q /= q.sum()
order = np.argsort(-q); c = np.cumsum(q[order])
keep = order[: np.searchsorted(c, p) + 1]
out = np.zeros_like(q); out[keep] = q[keep]
return out / out.sum()
confident = np.array([6., 2., 1., 0.])
unsure = np.array([1., 0.9, 0.8, 0.7])
for z in (confident, unsure):
print(np.round(dist(z, tau=1.0, p=0.9), 3))
Predicting a setting's effect
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