Basis Lab

Answers · The mathematics underneath

What does temperature scaling actually change in a model's probabilities?

In shortIt divides every score by one positive number before the softmax, so the probabilities become sharper or flatter while the ranking and top prediction stay the same.

Temperature scaling divides every score, or logit, by one positive number before the softmax. The result is a sharper or flatter probability distribution with exactly the same ranking and the same top class.

pi(T)=exp⁡(zi/T)∑jexp⁡(zj/T)p_i(T) = \frac{\exp(z_i/T)}{\sum_j \exp(z_j/T)}

What the constant does to two scores

Take a two-class model with scores 2 and 1. At T=1T=1 the leading probability is e2/(e2+e)=e/(e+1)=2.7183/3.7183=0.731e^2/(e^2+e)=e/(e+1)=2.7183/3.7183=0.731. At T=2T=2 the scores become 1 and 0.5, so the leading probability is e/(e+e0.5)=2.7183/(2.7183+1.6487)=2.7183/4.3670=0.622e/(e+e^{0.5})=2.7183/(2.7183+1.6487)=2.7183/4.3670=0.622. At T=0.5T=0.5 the scores become 4 and 2, so the leading probability is e4/(e4+e2)=e2/(e2+1)=7.3891/8.3891=0.881e^4/(e^4+e^2)=e^2/(e^2+1)=7.3891/8.3891=0.881. The gap between the scores was 1; dividing by 2 makes it 0.5, and dividing by 0.5 makes it 2. The leading class never changes, but the confidence attached to it does.

FIG 1
Tscaled leading scorescaled runner-up scoreleading probability
210.50.622
1210.731
0.5420.881

Why the ranking cannot move

The natural guess is that temperature changes what the model believes about the classes. It does not change the order. For any positive TT, if zi>zjz_i>z_j then zi/T>zj/Tz_i/T>z_j/T, and softmax is increasing in the score, so pi>pjp_i>p_j remains. The ratio of two probabilities makes the invariance explicit: pi(T)/pj(T)=exp⁡((zi−zj)/T)p_i(T)/p_j(T)=\exp((z_i-z_j)/T), which is greater than one exactly when zi>zjz_i>z_j. A temperature above one shrinks every gap and flattens the distribution. A temperature below one stretches every gap and sharpens it. At T=1T=1 the model is left exactly as trained. Zero is not allowed because it divides by zero; negative values would reverse the order and are not what temperature scaling means.

How the one number is chosen

The constant is fitted after training, with the network frozen. On held-out data, choose TT to minimise cross entropy of the scaled probabilities. Since the ranking is fixed, no prediction flips and accuracy is unchanged. What changes is the loss and the confidence attached to each answer. A single parameter can remove a large part of a smooth overconfidence because it rescales all gaps together, rather than repairing classes one at a time. It is cheap because the expensive part, the scores themselves, has already been computed.

Where it stops being a repair

It stops being a repair when the miscalibration is not one global scale. If a model is overconfident by different amounts in different classes, or in different regions of input space, one TT cannot correct all of them. It will improve the average and leave structure behind. Fitting TT on data unlike the deployment data gives a constant that no longer matches, so the held-out set has to be representative. It also cannot make a wrong model right: if the top class is wrong, temperature scaling keeps it wrong. And a temperature below one sharpens the distribution, which is appropriate only if the model was underconfident to begin with.

What temperature scaling actually changes is the entropy of the reported distribution. It does not change the model's internal scores, the order of the classes, or the top answer. It changes how much probability mass the model puts behind its winner and how much it spreads over the rest. That is why it can repair calibration without touching accuracy, and why it cannot recover information the model never had. If the deployment distribution differs from the fitting set, the chosen constant is no longer the right constant.

Worth keeping

  • Temperature scaling divides every logit by the same positive constant before the softmax, so the ranking of classes is unchanged.
  • A temperature above one flattens the distribution; a temperature below one sharpens it.
  • One global temperature fitted on held-out data can repair a smooth overconfidence, but it cannot fix miscalibration that varies by class or input.

Where this goes next

This question is one step of The Mathematics of Cross Entropy, a course that works through its subject from the beginning. The page that teaches this part of it is Confident and Wrong, and it reads the whole way through whether or not you have an account.

What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.

Read that pageThe whole course

Other questions people ask about this

  1. Why can't cross entropy loss go below the entropy of the labels?Cross entropy cannot fall below the uncertainty already in the labels because it is that uncertainty plus a non-negative distance from the model to the labels.
  2. What does a perplexity of 10 actually mean?A perplexity of 10 means the model's average loss is about 2.30 nats per prediction, equivalent to a uniform choice among ten equally likely options, not a 10 percent accuracy.
  3. Why does subtracting the max from the logits not change softmax?Subtracting the largest score is an exact rewriting of softmax, not a change to the distribution: the same factor cancels above and below.
  4. Why does cross entropy loss only use the probability of the correct class?Because a definite label puts all the target weight on the correct class and none on the others, so every other term is multiplied by zero.
  5. What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
  6. How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.

Read alongside