TL;DR
Softmax turns scores into probabilities, but the sharpness of the result is fixed by the scores themselves. Temperature is one extra number that divides every score before softmax runs. A large spreads probability across the choices, and a small 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 before softmax is the same as shrinking the gaps when and stretching them when . 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
For scores and a temperature , the temperature softmax is the vector whose -th entry is
Here is the score for class , is the number of classes, and is the exponential function. Setting recovers ordinary softmax. The scores are often called logits, meaning unnormalised scores before the softmax.
What the limits look like
Two limits explain the whole behaviour.
Large . As grows, every approaches 0, so every exponential approaches . The output approaches the uniform distribution, with every class at probability .
Small . As 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 measures its spread:
measured in nats because the natural logarithm is used. A one-hot distribution has entropy 0, and the uniform distribution has the largest entropy, . Since raising moves the output toward uniform, increases with for these logit-driven distributions. The worked examples below show this with numbers.
Gradients scale with 1/T
Writing , the chain rule gives
where is 1 when and 0 otherwise. The factor 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 (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 to keep its size comparable across temperatures.
Worked examples
Scores . Compare (ordinary softmax) with and .
At (from the Softmax lesson): the probabilities are .
At .
- Divide each score by 2: .
- Exponentiate: , , .
- Sum: .
- Divide: , , .
- Check: .
At .
- Divide each score by 0.5, which multiplies it by 2: .
- Exponentiate: , , .
- Sum: .
- Divide: , , .
- Check: the three values sum to 1.000.
Interpretation: the ranking is identical in all three rows. Doubling pulled the top probability down from 0.659 to 0.502, while halving 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.
A spam filter has logits for "spam" and "not spam". With two classes, the softmax depends only on the gap between them, so .
- At : , so .
- At : the gap becomes , and , so .
- At : the gap becomes , and , so .
Now the entropy of the three-class distribution from the previous example, in nats:
- At : .
- At : .
- At : .
The maximum for three classes is , so is already close to uniform. Interpretation: the spam model is nearly certain at and still quite sure at , but raising 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.
| Situation | Problem | Better choice |
|---|---|---|
| Text generation that is repetitive at or near it | Greedy choice keeps picking the same high-probability phrase | Raise modestly, around 0.7 to 1.0, or use top- sampling |
| Text generation that becomes incoherent at high | Large gives low-probability tokens too much mass | Lower , or truncate the tail with top- or top- |
| Calibration of an overconfident classifier | Temperature is fit on held-out data, so a single may not fix every region of the input space | Temperature scaling fit on a validation set, then check reliability plots |
| Distilling a large teacher into a small student | Hard labels throw away how the teacher ranks the wrong classes | Train the student on softened targets at high , scaled by |
| You need the single best answer, reproducibly | Any with sampling adds randomness | Take the argmax, which is the limit as 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 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 when all scores are equal, for example ? Does the answer depend on ?Show answer
All scores are equal, so every exponential is the same number and every probability is . The answer does not depend on , because dividing equal scores by any still leaves them equal.
QuestionFor at , compute the first output of the softmax to three decimal places.Show answer
Divide the scores by 2 to get . Then and , with sum . The first output is . At it would have been , so the higher temperature moved the first class's probability up toward the uniform value of .
QuestionWhy does the temperature never change which class has the largest probability?Show answer
Dividing every score by the same positive 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 produces the same sentence every time. A colleague suggests setting to make it more varied. Is that a good idea? What trade-off should they expect?Show answer
Raising does add variety, because the distribution flattens and sampling picks less likely tokens more often. But at 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- truncation, usually gives a better balance.
QuestionDistillation uses a temperature and multiplies the soft-target loss by . 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 , and the soft-target loss gradient shrinks by about overall. At that is roughly of its usual size, so it would be swamped by the hard-label loss. Multiplying the loss by restores the magnitude, so the two loss terms stay comparable as changes.