Inferensys

Glossary

Temperature Scaling

Temperature scaling is a hyperparameter technique in knowledge distillation where a temperature parameter (T > 1) is applied to the softmax function of the teacher model's logits to produce a smoother, more informative probability distribution for the student to learn from.
Knowledge manager reviewing enterprise knowledge management system on laptop, document library visible, casual office.
MODEL DISTILLATION

What is Temperature Scaling?

Temperature scaling is a critical hyperparameter technique in knowledge distillation that modifies the softmax function to produce a more informative training signal for a student model.

Temperature scaling is a hyperparameter technique in knowledge distillation where a temperature parameter (T > 1) is applied to the softmax function of the teacher model's logits to produce a smoother, more informative probability distribution for the student to learn from. This softened distribution, rich in dark knowledge about inter-class similarities, provides a superior training signal compared to hard one-hot labels, enabling the smaller student model to better mimic the teacher's generalization behavior.

The technique directly optimizes the distillation loss, typically the Kullback-Leibler divergence, between the teacher's and student's softened outputs. During inference, the temperature is set back to 1, restoring standard probability calibration. It is a foundational component in creating efficient models like DistilBERT and TinyBERT, bridging the performance gap between large teacher models and compact, deployable student networks.

MODEL DISTILLATION

Key Characteristics of Temperature Scaling

Temperature scaling is a hyperparameter technique in knowledge distillation where a temperature parameter (T > 1) is applied to the teacher model's softmax function to produce a smoother, more informative probability distribution for the student to learn from.

01

The Temperature Parameter (T)

The temperature parameter (T) is a scalar value applied within the softmax function to control the sharpness of the output probability distribution. The modified softmax function for a logit vector z is: softmax(z_i) = exp(z_i / T) / Σ_j exp(z_j / T). When T = 1, it's the standard softmax. When T > 1, probabilities are 'softened,' making the distribution smoother and less confident. This reveals the dark knowledge—the relative similarities between classes—that the student model learns from.

02

Softening the Teacher's Outputs

The primary function of temperature scaling is to generate soft targets or soft labels. For example, in an image of a '7', a teacher might output logits leading to hard probabilities like [Cat: 0.01, Dog: 0.02, Seven: 0.95, Nine: 0.02]. With T=3, this becomes a softer distribution like [Cat: 0.05, Dog: 0.10, Seven: 0.70, Nine: 0.15]. This indicates to the student that a '7' is somewhat similar to a '9', but not to a 'cat'—information absent in the one-hot label. This richer signal is the core of effective distillation.

03

Impact on the Distillation Loss

Temperature scaling is directly integrated into the distillation loss, typically the Kullback-Leibler (KL) Divergence Loss. The student is trained to minimize the divergence between its own softened outputs (using the same T) and the teacher's softened outputs. The total loss is often a weighted sum: L_total = α * L_CE(hard_labels) + (1-α) * T^2 * L_KL(teacher_soft_targets || student_soft_targets). The T^2 term is used to scale the gradients, as the softened probabilities produce smaller gradients magnitude as T increases.

04

Calibration and Confidence

Beyond distillation, temperature scaling (with T tuned on a validation set) is a premier post-hoc calibration technique. A well-calibrated model's predicted confidence aligns with its accuracy. A model might be accurate but overconfident (e.g., predicting 0.99 probability when it's only correct 80% of the time). Applying T > 1 can smooth its output distribution, reducing confidence on average and improving metrics like Expected Calibration Error (ECE) without retraining the model's weights.

05

Relationship to Other Distillation Methods

Temperature scaling is often used in conjunction with other distillation paradigms:

  • Logit-based Distillation: It is the foundational technique here.
  • Feature-based Distillation (e.g., Attention Transfer): Temperature is not typically applied to intermediate features.
  • Online Distillation: The temperature parameter can be applied to the peer models' outputs.
  • Quantization-Aware Distillation (QAD): Temperature-scaled soft targets can help the student model learn a more robust representation before quantization. It is a complementary, not competing, technique.
06

Practical Tuning and Considerations

Choosing the right temperature is critical. Common practices include:

  • Typical Range: T is usually between 1 and 20, with 3-5 being a common starting point for vision and NLP tasks.
  • Tuning: T is a hyperparameter validated on a downstream task's performance. Too low (≈1) provides little dark knowledge. Too high flattens the distribution excessively, losing necessary discriminative signal.
  • Inference: The temperature is only used during the student's training. During final inference, the student model uses the standard softmax (T=1). The teacher's role, and thus temperature scaling, is complete.
KNOWLEDGE DISTILLATION

Effect of Temperature Parameter (T) on Output

How adjusting the temperature scaling hyperparameter (T) modifies the teacher model's softmax output distribution, which serves as the training target for the student model.

Output CharacteristicLow Temperature (T → 0+)Standard Temperature (T = 1)High Temperature (T > 1)

Probability Distribution Shape

Approaches one-hot encoding

Standard softmax

Softer, more uniform distribution

Peak Probability Value

~1.0

Model-dependent (e.g., 0.9)

Lower (e.g., 0.7)

Information Content (Entropy)

Very Low

Model-dependent

Higher

Inter-Class Similarity Signal (Dark Knowledge)

Minimal

Present

Amplified

Primary Use Case

Standard classification (not for KD)

Baseline teacher output

Optimal for knowledge distillation

Gradient Signal for Student

Sparse, high-variance

Balanced

Denser, smoother

Risk of Overfitting Student

High

Medium

Low

Typical Value Range in KD

2.0 to 10.0

TEMPERATURE SCALING

Frequently Asked Questions

Temperature scaling is a critical hyperparameter in knowledge distillation that modifies the softmax function to create a more informative training signal for a student model.

Temperature scaling is a hyperparameter technique used primarily in knowledge distillation to soften the output probability distribution of a teacher model by dividing its logits by a temperature parameter (T > 1) before applying the softmax function. This process produces soft targets that contain richer inter-class similarity information, known as dark knowledge, which a student model can learn from more effectively than from hard one-hot labels. The technique is foundational because it reveals the relative confidence the teacher has between similar classes (e.g., between different breeds of dog), providing a more nuanced training signal.

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.