Temperature
Sample from softmax(z/τ) and predict the result: τ→0 approaches greedy, τ=1 is the model's own distribution, and larger τ flattens it and raises entropy. Compute the effect on a toy logit vector.
Builds on: Autoregressive generation loop, Surprisal and entropy
What the animation says
One knob on the logits
Every step, the model hands you one vector of logits: a score for each token in the vocabulary. Same prompt, same logits. Yet one setting decides whether runs come out nearly identical or wildly varied. That setting is temperature, called tau. Before the softmax, every logit is divided by it. Take three logits: two, one, zero. A tau of one half means dividing each one by one half. Watch the gap between the top two tokens. After the division, what happens to it?
Only the gaps matter
Divided by one half, two, one, zero becomes four, two, zero. Every gap doubled. And gaps are all softmax sees. Add ten to every logit, and the probabilities don't move. So the ratio of two tokens' probabilities depends only on their gap: e to the gap divided by tau. Small tau stretches the gaps, and the top token grabs almost everything. Large tau shrinks them, and tokens look alike. The order never changes. Temperature only sets how strongly the ranking is enforced.
Three temperatures, worked
Let's run the numbers. At tau one, exponentiate two, one, zero: about seven point four, two point seven, and one. They sum to eleven point one, so the probabilities are about sixty-seven, twenty-four, and nine percent. At tau one half, the logits become four, two, zero. Exponentials: about fifty-five, seven point four, and one. Now the top token holds eighty-seven percent. Sharper, but the same order. At tau two, the logits become one, one half, zero. Roughly what share does the top token get?
From greedy to uniform
At tau two: about fifty-one, thirty-one, and nineteen percent. Entropy tracks the spread: point four four, then point eight three, then one point oh two nats. Push tau toward zero and all the mass lands on the top token. That's greedy; tied tops split it evenly. Push it toward infinity and you get uniform: about one point one nats, the ceiling. Same thing another way: raise each probability to one over tau, then renormalize. One of these charts can't come from two, one, zero at any temperature. Tap it.
The sampler in code
Here's the sampler. This line divides the logits by tau. Next, subtract the max before exponentiating. Same probabilities, since only gaps matter, but no overflow. Then normalize and draw a token. Tau of exactly zero would divide by zero, so real code treats it as plain argmax. Run it over five temperatures on two, one, zero. Watch the entropy column climb from nearly zero toward the ceiling. Remember: temperature scales every gap by one over tau. Greedy near zero, the model itself at one, uniform at infinity. Ranking fixed.
Temperature rescales the gaps
The tempered softmax
Three temperatures on [2, 1, 0]
Sampling with temperature
import numpy as np
def sample(z, tau, rng):
s = z / tau
p = np.exp(s - s.max()); p /= p.sum()
return rng.choice(len(z), p=p), p
rng = np.random.default_rng(0)
z = np.array([2.0, 1.0, 0.0])
for tau in [0.1, 0.5, 1.0, 2.0, 10.0]:
tok, p = sample(z, tau, rng)
H = -(p * np.log(p + 1e-12)).sum()
print(tau, tok, p.round(3), round(H, 3))
Remember
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