Loss Functions › Temperature Scaling

Michael BrenndoerferMay 2, 20252 min read
Language AI HandbookAll chapters
Part 10 of 67Neural Network Foundations
Chapter 64Loss Functions › Temperature Scaling
Reading section 8 of 17 · Temperature Scaling · 2 minSection 8 of 17 · 2 min

Book contents

Section 8 of 17 · 2 min read

Loss Functions › Temperature ScalingOne section · Sections in this chapter8 / 17 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Temperature scaling modifies a model's logits before computing probabilities, controlling the sharpness of the predicted distribution. A temperature T>0T > 0 is applied as:

pk=ezk/T∑j=1Kezj/Tp_k = \frac{e^{z_k / T}}{\sum_{j=1}^{K} e^{z_j / T}}

where:

When T=1T = 1, the standard softmax is recovered. When T<1T < 1 (low temperature), the distribution becomes sharper: the class with the highest logit gets even more probability mass. When T>1T > 1 (high temperature), the distribution becomes softer, approaching a uniform distribution as T→∞T \to \infty.

Temperature scaling is used in several contexts:

  • Knowledge distillation: A high temperature (e.g., T=5T = 5 or T=10T = 10) is applied to the teacher's logits during training. This produces soft targets that reveal the relative similarity between classes. This provides richer supervision than hard one-hot labels.
  • Calibration: After training, temperature can be tuned on a validation set to make the model better calibrated, without changing its predictions (only confidence scores change).
  • Sampling from language models: During text generation, temperature controls the randomness of token sampling. Low temperature makes the model more deterministic, high temperature makes it more creative.

Post-training temperature scaling is notable for being a uniquely simple and effective calibration technique. Unlike platt scaling or isotonic regression, temperature scaling has only one parameter to tune, avoids overfitting on the calibration set, and does not change the model's accuracy on any task (only its confidence scores). The procedure is: freeze the model, search over values of TT to minimize the NLL on a held-out validation set, and apply the optimal TT at inference time.