Inferensys

Glossary

Temperature Scaling

A hyperparameter applied to the softmax function during distillation that controls the softness of the output probability distribution, revealing the 'dark knowledge' of the teacher's internal representations.
Knowledge engineer constructing knowledge base on laptop, document hierarchy visible, casual office setup.
SOFTMAX CALIBRATION

What is Temperature Scaling?

A hyperparameter applied to the softmax function that controls the entropy of the output probability distribution, revealing the 'dark knowledge' of a model's internal representations.

Temperature scaling is a calibration technique that divides the logits of a neural network by a scalar parameter T before applying the softmax function. A high temperature (T > 1) softens the probability distribution, making class probabilities more uniform and exposing the relative similarities between incorrect classes—the dark knowledge—while T = 1 recovers the standard softmax.

In knowledge distillation, a teacher model's softened outputs serve as soft targets for training a student model, providing richer supervisory signals than hard labels. The temperature is critical during training but set to 1 at inference. This mechanism is also used for model calibration, reducing overconfidence by aligning predicted probabilities with empirical accuracy.

SOFTMAX CALIBRATION

Key Characteristics of Temperature Scaling

Temperature scaling is a hyperparameter applied to the softmax function that controls the entropy of the output probability distribution, revealing the 'dark knowledge' of a model's internal representations.

01

Softmax with Temperature Parameter

Temperature scaling modifies the standard softmax function by dividing logits by a scalar T before exponentiation. When T = 1, the function behaves as standard softmax. As T → ∞, the distribution approaches uniform, making all classes equally probable. As T → 0, the distribution becomes a Dirac delta at the argmax, outputting hard labels. The formula is: softmax(z_i / T) = exp(z_i / T) / Σ_j exp(z_j / T). This single scalar parameter is applied identically across all classes, preserving the rank order of the original logits while adjusting the confidence of the prediction.

02

Dark Knowledge Extraction

The term dark knowledge refers to the information encoded in the relative probabilities of incorrect classes within a teacher model's softmax output. At high temperatures, the softmax surface reveals inter-class similarities that are obscured at T=1. For example, a model classifying a handwritten '3' might assign a probability of 10⁻⁶ to '8' and 10⁻⁹ to 'dog' at T=1. With temperature scaling, these differences are magnified, showing that '3' is semantically closer to '8' than to 'dog'. This structural knowledge about the data manifold is transferred to the student during distillation.

03

Distillation Loss Weighting

During knowledge distillation, the student model is trained with a composite loss function that balances two objectives. The distillation loss uses the Kullback-Leibler divergence between the student's soft targets and the teacher's soft targets, both computed with the same high temperature T. The student loss is the standard cross-entropy between the student's predictions at T=1 and the ground-truth hard labels. A weighting parameter α (typically 0.1 to 0.5) controls the relative contribution of these two losses: L_total = α * L_hard + (1 - α) * T² * L_soft. The T² scaling factor is applied to keep gradient magnitudes consistent.

04

Post-Training Calibration

Beyond distillation, temperature scaling serves as a post-hoc calibration method for modern neural networks. Unlike Platt scaling or isotonic regression, temperature scaling does not change the model's accuracy or the rank ordering of predictions—it only adjusts confidence estimates. A single scalar T is optimized on a held-out validation set to minimize the negative log-likelihood or expected calibration error (ECE). This technique is particularly effective for calibrating large language models and deep ensembles, where overconfident predictions are a well-documented problem. The optimal T is found via gradient descent on the calibration metric.

05

Entropy and Gradient Flow

Temperature scaling directly controls the entropy of the softmax distribution, which in turn affects the gradient flow during backpropagation. At low temperatures, gradients are sparse—only the top class receives a meaningful signal. At high temperatures, the gradient is distributed more evenly across classes, providing richer training signals for the student. However, excessively high temperatures can drown the signal in noise. Empirical studies show optimal distillation temperatures typically range from T=2 to T=20, depending on the number of classes and the complexity of the teacher model. The choice of T represents a bias-variance tradeoff in the transferred knowledge.

06

Logit Matching Equivalence

An important theoretical property of temperature scaling is its relationship to logit matching. When the distillation loss uses KL divergence with a high temperature T and the soft targets are derived from the teacher's logits, the gradient of the distillation loss with respect to the student's logits approaches a simple squared error between the student and teacher logits as T → ∞. Specifically, the gradient becomes proportional to (z_student - z_teacher) / T. This reveals that high-temperature distillation is asymptotically equivalent to directly matching the logit vectors of the teacher, providing a simpler interpretation of what the student is learning.

TEMPERATURE SCALING

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the temperature hyperparameter and its role in softening probability distributions during knowledge distillation.

Temperature scaling is a hyperparameter applied to the softmax function that controls the entropy of the output probability distribution. By dividing the logits by a temperature parameter ( T ), the model's confidence is modulated: a higher ( T > 1 ) produces a softer, more uniform distribution, while ( T < 1 ) sharpens it toward a one-hot vector. Mathematically, the scaled softmax is defined as:

code
softmax(z_i / T) = exp(z_i / T) / Σ_j exp(z_j / T)

This mechanism is the cornerstone of knowledge distillation, where it reveals the dark knowledge encoded in the relative probabilities of incorrect classes. Unlike calibration-focused temperature scaling, distillation uses high temperatures during both teacher inference and student training to expose the inter-class similarity structure learned by the teacher.

POST-HOC CALIBRATION COMPARISON

Temperature Scaling vs. Other Calibration Methods

A technical comparison of temperature scaling against alternative probability calibration techniques for neural network outputs.

FeatureTemperature ScalingPlatt ScalingIsotonic RegressionHistogram Binning

Parametric form

Single scalar T > 0

Logistic regression on logits

Non-parametric step function

Piecewise constant bins

Number of parameters

1

2 (A, B)

N/A (data-driven)

M bins

Preserves rank order

Optimization objective

NLL on validation set

NLL on validation set

Isotonic NLL minimization

Bin-wise accuracy matching

Prevents overfitting on small sets

Applicable to multi-class

Typical ECE reduction

70-90%

50-70%

60-85%

30-50%

Computational cost

O(1) post-training

O(N) convex optimization

O(N log N) PAVA algorithm

O(N) bin assignment

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.