Inferensys

Glossary

Temperature Scaling

Temperature scaling is a hyperparameter technique in knowledge distillation that applies a temperature (T) to the softmax function to soften a teacher model's output probability distribution, making it more informative for training a student model.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
KNOWLEDGE DISTILLATION

What is Temperature Scaling?

Temperature Scaling is a core technique in logits distillation that modifies the softmax function to create a more informative probability distribution for training a student model.

Temperature Scaling is a technique in knowledge distillation where a temperature parameter (T) is applied to the softmax function to soften a teacher model's output probability distribution. This controlled softening reveals the model's 'dark knowledge'—the relative similarities between different classes—which provides a richer training signal than hard, one-hot labels. The softened outputs, called soft targets, are used to train a smaller student model via a distillation loss function like Kullback-Leibler divergence.

The temperature parameter (T > 1) smooths the logits, increasing the entropy of the output distribution. A higher temperature produces a more uniform distribution, emphasizing inter-class relationships, while a temperature of 1 recovers the standard softmax. This technique is fundamental to the teacher-student framework, enabling effective model compression. It is often used in conjunction with quantization-aware distillation to prepare models for efficient on-device inference.

KNOWLEDGE DISTILLATION

Key Characteristics of Temperature Scaling

Temperature Scaling is a core technique in logits distillation that modifies the softmax function to control the 'softness' of a teacher model's output distribution, making its knowledge more transferable to a student model.

01

The Temperature Parameter (T)

The temperature parameter (T) is a scalar value applied within the softmax function to control the entropy of the output probability distribution. The modified softmax for logit (z_i) is: ( \sigma(z_i, T) = \frac{e^{z_i / T}}{\sum_j e^{z_j / T}} ).

  • T = 1: Standard softmax, producing the original distribution.
  • T > 1: 'Softens' the distribution, increasing probabilities for non-argmax classes and revealing dark knowledge about inter-class similarities.
  • T < 1: 'Hardens' the distribution, making it more peaky and confident, approaching a one-hot label.
02

Softening the Teacher's Distribution

The primary function of temperature scaling (with T > 1) is to soften the teacher's output probabilities. This transforms hard, one-hot-like labels into a richer supervisory signal.

  • Example: For a cat/dog/horse image, a teacher's raw logits might yield probabilities [0.9, 0.09, 0.01]. With T=2, this could soften to [0.7, 0.2, 0.1].
  • This softened distribution encodes relational information (e.g., 'cat' is more similar to 'dog' than to 'horse'), which is the dark knowledge crucial for effective distillation.
  • The student learns from these nuanced probabilities, leading to better generalization than learning from hard labels alone.
03

Role in the Distillation Loss Function

Temperature scaling is integral to the standard distillation loss, which combines a cross-entropy loss with the Kullback-Leibler (KL) Divergence between the teacher and student's softened outputs.

  • The total loss is often: ( L = (1 - \alpha) L_{CE}(y, \sigma(z_s)) + \alpha T^2 L_{KL}(\sigma(z_t/T), \sigma(z_s/T)) ).
  • The ( T^2 ) factor compensates for the gradient scaling introduced by the temperature.
  • The hyperparameter ( \alpha ) balances learning from true labels versus learning from the teacher's softened distribution.
04

Calibration and Confidence Smoothing

Beyond distillation, temperature scaling is a primary method for model calibration. A well-calibrated model's predicted confidence aligns with its actual accuracy.

  • A single scalar T is learned on a validation set to scale the logits post-training.
  • This simple linear operation can significantly reduce overconfidence (where softmax probabilities are excessively peaky) without changing the model's argmax predictions.
  • It is a key component in Platt scaling for neural networks, providing a lightweight way to improve probability estimates for downstream decision-making.
05

Interaction with the Student's Training

During distillation, the same temperature T is applied to both teacher and student logits for the KL divergence term. This ensures a consistent, smooth target for the student to match.

  • High T (e.g., 10): Creates a very uniform, high-entropy target. This can be beneficial in early training to prevent the student from overfitting to the teacher's potentially noisy argmax.
  • Optimal T: Is task and dataset-dependent. A common heuristic is to start with a higher T (e.g., 4-10) and potentially anneal it during training, or to tune it as a hyperparameter.
  • At inference, the student uses standard softmax (T=1). The temperature is purely a training-time mechanism.
06

Limitations and Practical Considerations

While powerful, temperature scaling has specific constraints and trade-offs.

  • Single-Scalar Limitation: It applies a uniform transformation to all logits, which may not be optimal for all samples or classes. More advanced methods like vector scaling or matrix scaling exist but add complexity.
  • Hyperparameter Tuning: The optimal value of T must be searched for, typically via validation performance. An incorrect T can provide less informative targets than hard labels.
  • Not a Panacea: It primarily aids in transferring dark knowledge via output logits. For complex student architectures, it is often combined with feature mimicking or attention transfer from intermediate layers for greater knowledge fidelity.
LOGITS DISTILLATION

Temperature Scaling vs. Standard Softmax

A comparison of the standard softmax function and its temperature-scaled variant, highlighting their distinct roles in classification and knowledge distillation.

Feature / PropertyStandard Softmax (T=1)Temperature-Scaled Softmax (T>1)

Primary Purpose

Final classification layer for inference

Generate softened training targets for knowledge distillation

Temperature Parameter (T)

Fixed at 1 (implicit)

Tunable hyperparameter, typically >1

Output Distribution

Sharp, high-confidence probabilities

Softer, more uniform probabilities

Information Content

High for the predicted class, low for others

High 'dark knowledge' revealing inter-class similarities

Effect on Logits

Applies standard exponential normalization

Divides logits by T before exponentiation: σ(z/T)

Gradient Signal for Student

Strong only for the target class

Provides richer gradients across all classes

Use in Final Inference

✅ Used directly for prediction

❌ Not used; student uses standard softmax (T=1) at inference

Role in Teacher-Student Framework

❌ Not used for generating training signals

✅ Core mechanism for producing informative soft targets

TEMPERATURE SCALING

Frequently Asked Questions

Temperature Scaling is a core technique in logits-based knowledge distillation. These questions address its fundamental mechanics, applications, and relationship to other model optimization methods.

Temperature Scaling is a technique used primarily in knowledge distillation where a temperature parameter (T) is applied to the softmax function to soften a model's output probability distribution, making it more informative for training a smaller student model.

In standard classification, the softmax function converts raw output logits into a probability distribution. The formula is softmax(z_i) = exp(z_i) / Σ_j exp(z_j). Temperature Scaling modifies this to softmax(z_i / T). When T = 1, it's the standard softmax. When T > 1, the distribution becomes 'softer'—probabilities are smoothed out, reducing the confidence of the peak class and revealing the model's relative confidence across all classes. This softened distribution contains dark knowledge, such as similarities between classes (e.g., that a 'cat' is more similar to a 'lynx' than to a 'truck'), which provides a richer training signal than a hard, one-hot label.

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.