Search

Search papers and lessons

Softmax Temperature

Level 3 · 9 min read

Temperature divides the scores before softmax, so a large value flattens the probabilities toward uniform and a small value sharpens them toward the top choice.

On this page

  • Colour = topic mix
  • Neural network basics
  • Discrete math
  • Probability
  • Sign in to see your progress

TL;DR

Softmax turns scores into probabilities, but the sharpness of the result is fixed by the scores themselves. Temperature is one extra number TT that divides every score before softmax runs. A large TT spreads probability across the choices, and a small TT concentrates it on the highest score. The ranking of the choices never changes, only how confident the distribution looks.

Where you'll see it: as the sampling setting "temperature" in language models, and as the softened targets used when a small network learns from a large one.

Intuition

A dial on how decisive the model is

Think of a panel of judges giving scores to three dishes: 2, 1 and 0.1. With the plain softmax, the first dish gets about two-thirds of the vote. Now imagine the judges are asked to be more cautious. They shrink every score toward the middle before tallying. The dish ranking is the same, but the gap between the dishes narrows, so the vote spreads out. Asking them to be more decisive does the opposite and pushes the gaps apart.

Temperature is that dial. Dividing the scores by TT before softmax is the same as shrinking the gaps when T>1T \gt 1 and stretching them when T<1T \lt 1. The model's preferences are untouched. You are only choosing how strongly those preferences show up in the probabilities.

Why would you want this? A trained classifier is often overconfident, and a language model that always picks the single most likely next word writes repetitive, flat text. Temperature gives a single knob to trade between the two, without retraining anything.

flowchart LR
  A[Logits z] --> B[Divide by T]
  B --> C[Softmax]
  C --> D[Probabilities]
  D --> E{T large?}
  E -->|yes| F[Flatter, more random]
  E -->|no| G[Sharper, closer to argmax]

The math

Definition

Definition: Softmax with temperature

For scores z=(z1,…,zK)\mathbf{z} = (z_1, \ldots, z_K) and a temperature T>0T \gt 0, the temperature softmax is the vector whose ii-th entry is

σT(z)i=exp⁡(zi/T)∑j=1Kexp⁡(zj/T).\sigma_T(\mathbf{z})_i = \frac{\exp(z_i / T)}{\sum_{j=1}^{K} \exp(z_j / T)}.

Here ziz_i is the score for class ii, KK is the number of classes, and exp⁡\exp is the exponential function. Setting T=1T = 1 recovers ordinary softmax. The scores ziz_i are often called logits, meaning unnormalised scores before the softmax.

What the limits look like

Two limits explain the whole behaviour.

Large TT. As TT grows, every zi/Tz_i / T approaches 0, so every exponential approaches e0=1e^0 = 1. The output approaches the uniform distribution, with every class at probability 1/K1/K.

Small TT. As TT approaches 0, the largest score dominates. If one score is strictly largest, its probability tends to 1 and every other probability tends to 0. The output becomes a one-hot vector at the argmax, which is the hard choice softmax was meant to soften.

Between these limits the distribution moves smoothly, and the order of the probabilities always matches the order of the scores. Temperature never swaps the winner.

Entropy grows with temperature

The Shannon Entropy of a distribution p\mathbf{p} measures its spread:

H(p)=−∑i=1Kpiln⁡pi,H(\mathbf{p}) = -\sum_{i=1}^{K} p_i \ln p_i,

measured in nats because the natural logarithm is used. A one-hot distribution has entropy 0, and the uniform distribution has the largest entropy, ln⁡K\ln K. Since raising TT moves the output toward uniform, HH increases with TT for these logit-driven distributions. The worked examples below show this with numbers.

Gradients scale with 1/T

Writing pi=σT(z)ip_i = \sigma_T(\mathbf{z})_i, the chain rule gives

∂pi∂zj=1T pi(δij−pj),\frac{\partial p_i}{\partial z_j} = \frac{1}{T}\, p_i (\delta_{ij} - p_j),

where δij\delta_{ij} is 1 when i=ji = j and 0 otherwise. The factor 1/T1/T means that a large temperature shrinks the gradient flowing back to the logits. This matters when temperature is used during training. In distillation, the soft-target gradient shrinks by about 1/T21/T^2 (one factor from the formula above, and another because the softened teacher and student probabilities are closer together), so Hinton et al. multiply that loss by T2T^2 to keep its size comparable across temperatures.

Worked examples

Example
Three scores at two temperatures

Scores z=(2,1,0.1)\mathbf{z} = (2, 1, 0.1). Compare T=1T = 1 (ordinary softmax) with T=2T = 2 and T=0.5T = 0.5.

At T=1T = 1 (from the Softmax lesson): the probabilities are (0.659, 0.242, 0.099)(0.659,\ 0.242,\ 0.099).

At T=2T = 2.

  1. Divide each score by 2: (1, 0.5, 0.05)(1,\ 0.5,\ 0.05).
  2. Exponentiate: e1≈2.718e^{1} \approx 2.718, e0.5≈1.649e^{0.5} \approx 1.649, e0.05≈1.051e^{0.05} \approx 1.051.
  3. Sum: 2.718+1.649+1.051=5.4182.718 + 1.649 + 1.051 = 5.418.
  4. Divide: p1≈0.502p_1 \approx 0.502, p2≈0.304p_2 \approx 0.304, p3≈0.194p_3 \approx 0.194.
  5. Check: 0.502+0.304+0.194=1.0000.502 + 0.304 + 0.194 = 1.000.

At T=0.5T = 0.5.

  1. Divide each score by 0.5, which multiplies it by 2: (4, 2, 0.2)(4,\ 2,\ 0.2).
  2. Exponentiate: e4≈54.598e^{4} \approx 54.598, e2≈7.389e^{2} \approx 7.389, e0.2≈1.221e^{0.2} \approx 1.221.
  3. Sum: 54.598+7.389+1.221=63.20854.598 + 7.389 + 1.221 = 63.208.
  4. Divide: p1≈0.864p_1 \approx 0.864, p2≈0.117p_2 \approx 0.117, p3≈0.019p_3 \approx 0.019.
  5. Check: the three values sum to 1.000.

Interpretation: the ranking is identical in all three rows. Doubling TT pulled the top probability down from 0.659 to 0.502, while halving TT pushed it up to 0.864. The same scores can give a near-coin-flip between the first two or a clear favourite, depending only on the dial.

Example
A two-class classifier and the entropy

A spam filter has logits (3, −3)(3,\ -3) for "spam" and "not spam". With two classes, the softmax depends only on the gap 66 between them, so pspam=1/(1+e−6/T)p_{\text{spam}} = 1 / (1 + e^{-6 / T}).

  1. At T=1T = 1: e−6≈0.00248e^{-6} \approx 0.00248, so pspam≈0.9975p_{\text{spam}} \approx 0.9975.
  2. At T=3T = 3: the gap becomes 6/3=26 / 3 = 2, and e−2≈0.1353e^{-2} \approx 0.1353, so pspam≈0.881p_{\text{spam}} \approx 0.881.
  3. At T=0.5T = 0.5: the gap becomes 1212, and e−12≈0.000006e^{-12} \approx 0.000006, so pspam≈0.999994p_{\text{spam}} \approx 0.999994.

Now the entropy of the three-class distribution from the previous example, in nats:

  1. At T=0.5T = 0.5: H≈0.453H \approx 0.453.
  2. At T=1T = 1: H≈0.847H \approx 0.847.
  3. At T=2T = 2: H≈1.026H \approx 1.026.

The maximum for three classes is ln⁡3≈1.099\ln 3 \approx 1.099, so T=2T = 2 is already close to uniform. Interpretation: the spam model is nearly certain at T=0.5T = 0.5 and still quite sure at T=1T = 1, but raising TT to 3 would make it noticeably less confident about the same message.

Tradeoffs and failure modes

Temperature is cheap and effective, but it is not a fix for everything.

SituationProblemBetter choice
Text generation that is repetitive at T=0T = 0 or near itGreedy choice keeps picking the same high-probability phraseRaise TT modestly, around 0.7 to 1.0, or use top-pp sampling
Text generation that becomes incoherent at high TTLarge TT gives low-probability tokens too much massLower TT, or truncate the tail with top-kk or top-pp
Calibration of an overconfident classifierTemperature is fit on held-out data, so a single TT may not fix every region of the input spaceTemperature scaling fit on a validation set, then check reliability plots
Distilling a large teacher into a small studentHard labels throw away how the teacher ranks the wrong classesTrain the student on softened targets at high TT, scaled by T2T^2
You need the single best answer, reproduciblyAny T>0T \gt 0 with sampling adds randomnessTake the argmax, which is the limit as TT goes to 0

Two pitfalls are common. First, temperature does not change which class wins, so it cannot correct a model that ranks the wrong answer first. Second, a temperature tuned on one dataset may not transfer to a different one, because the scale of the logits depends on the training.

History

The mathematics predates neural networks. The Boltzmann distribution from 19th-century statistical physics has the same shape: energies enter an exponential divided by a temperature, and a normaliser makes the probabilities sum to one. The "temperature" name comes from that physical analogy.

In machine learning, temperature became standard in two places. Language models used it as a sampling control so that users could trade diversity against coherence. In 2015, Hinton, Vinyals and Dean showed in "Distilling the Knowledge in a Neural Network" that training a small model on softened outputs at high temperature transfers more of a large model's knowledge than training on hard labels alone. That paper is why the T2T^2 scaling appears in many training codes. Temperature scaling as a calibration method was popularised later by work on overconfidence in deep networks.

Check yourself

QuestionWhat is the output of softmax with temperature TT when all scores are equal, for example z=(5,5,5)\mathbf{z} = (5, 5, 5)? Does the answer depend on TT?Show answer

All scores are equal, so every exponential is the same number and every probability is 1/31/3. The answer does not depend on TT, because dividing equal scores by any TT still leaves them equal.

QuestionFor z=(1,2)\mathbf{z} = (1, 2) at T=2T = 2, compute the first output of the softmax to three decimal places.Show answer

Divide the scores by 2 to get (0.5, 1)(0.5,\ 1). Then e0.5≈1.649e^{0.5} \approx 1.649 and e1≈2.718e^{1} \approx 2.718, with sum 4.3674.367. The first output is 1.649/4.367≈0.3781.649 / 4.367 \approx 0.378. At T=1T = 1 it would have been 0.2690.269, so the higher temperature moved the first class's probability up toward the uniform value of 0.50.5.

QuestionWhy does the temperature never change which class has the largest probability?Show answer

Dividing every score by the same positive TT preserves their order, and the exponential is strictly increasing, so the largest score still gives the largest exponential. The normaliser is the same for every class, so the largest exponential still gives the largest probability.

QuestionA language model at T=0.2T = 0.2 produces the same sentence every time. A colleague suggests setting T=5T = 5 to make it more varied. Is that a good idea? What trade-off should they expect?Show answer

Raising TT does add variety, because the distribution flattens and sampling picks less likely tokens more often. But at T=5T = 5 the tail of the vocabulary gets a lot of mass, so the text may become incoherent. A moderate value such as 0.8 to 1.0, possibly combined with top-pp truncation, usually gives a better balance.

QuestionDistillation uses a temperature T=4T = 4 and multiplies the soft-target loss by T2T^2. Using the gradient formula in this lesson, explain why the multiplication is needed.Show answer

The gradient of the softened output with respect to the logits carries a factor 1/T1/T, and the soft-target loss gradient shrinks by about 1/T21/T^2 overall. At T=4T = 4 that is roughly 1/161/16 of its usual size, so it would be swamped by the hard-label loss. Multiplying the loss by T2=16T^2 = 16 restores the magnitude, so the two loss terms stay comparable as TT changes.

Sources