All courses

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

▶ Watch this lesson Start the whole course

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.

One distribution, many behaviors

At each step the model produces one logit vector $z_t$ for the last position. The same prompt can still give near-identical outputs on every run, or a lot of variety, depending on a single setting. What exactly does that setting do to the numbers? Can you predict the distribution you will actually sample from, and how uncertain it is, before running anything?

Temperature rescales the gaps

Temperature $\tau$ divides every logit before the softmax. Softmax ignores a constant added to all logits, so only the **gaps** between logits matter. Dividing by $\tau$ multiplies every gap by $1/\tau$. - Small $\tau$: gaps grow, and the top token takes almost all the probability. - Large $\tau$: gaps shrink, and tokens look more alike. The ranking of tokens never changes. Only how strongly it is enforced changes.

The tempered softmax

Write $z$ for the logit vector $z_t$ and $z_i$ for its entry for token $i$. $$p_\tau(i)=\frac{e^{z_i/\tau}}{\sum_{j=0}^{V-1} e^{z_j/\tau}},\quad \tau>0$$ Here $V$ is the vocabulary size. For any two tokens, $p_\tau(i)/p_\tau(j)=e^{(z_i-z_j)/\tau}$. - $\tau=1$: the model's own distribution. - $\tau\to0$: all mass goes to the argmax, which is greedy. Tied maxima split the mass evenly. - $\tau\to\infty$: the distribution becomes uniform. Equivalently, $p_\tau(i)\propto p(i)^{1/\tau}$, renormalized to sum to 1.

Three temperatures on [2, 1, 0]

Logits $z=[2,1,0]$. - $\tau=1$: $e^{z}=[7.39,2.72,1]$, sum 11.11, so $p=[0.665,0.245,0.090]$. Entropy $H=0.83$ nats. - $\tau=0.5$: $z/\tau=[4,2,0]$ gives $[54.6,7.39,1]$, sum 63.0, so $p=[0.867,0.117,0.016]$. $H=0.44$. - $\tau=2$: $z/\tau=[1,0.5,0]$ gives $[2.72,1.65,1]$, sum 5.37, so $p=[0.506,0.307,0.186]$. $H=1.02$. The ceiling is uniform: $\ln 3=1.10$ nats.

Sampling with temperature

Dividing by $\tau$ and then subtracting the max before exponentiating keeps the computation stable. In practice, $\tau=0$ is handled as a plain argmax, because dividing by zero is undefined. Watch the entropy rise with $\tau$.
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

Temperature divides the logits, so every log-probability gap scales by $1/\tau$: $p_\tau\propto p^{1/\tau}$. As $\tau\to0$ sampling becomes greedy (argmax). At $\tau=1$ you get the model's own distribution. Larger $\tau$ flattens it toward uniform and raises the entropy, while the token ranking stays the same.

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.