Knowledge Distillation › Temperature in Distillation
Language AI HandbookAll chapters Knowledge Distillation › Temperature in DistillationOne section · Sections in this chapter3 / 10 ↓
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
The most practically important concept in distillation is temperature scaling, and it is worth understanding in detail because it fundamentally changes what information the student receives. Without temperature scaling, distillation often provides little improvement over hard-label training, because the soft labels are too similar to hard labels to provide additional signal.
The Problem with Sharp Distributions
Language model outputs tend to be sharply peaked. Given a sentence like "The sky is ___", the model assigns very high probability to "blue" and near-zero probability to essentially everything else. When these near-zero probabilities are used as soft labels, they provide almost no additional information compared to hard labels: the distribution is essentially one-hot.
The teacher's knowledge about relationships between less likely tokens is compressed into these tiny probability values. Words that are semantically related to "blue" (like "clear," "gray," or "overcast") receive probabilities that are only slightly higher than completely unrelated words. At floating-point precision, these differences exist, but they are numerically tiny. A student trying to match these near-zero values receives a training signal that is essentially the same as trying to match zeros. The relational structure is there in principle, but it is buried beneath the noise floor of floating-point arithmetic.
This is the fundamental problem that temperature scaling solves. The information is present in the teacher's logits, but it is compressed in a way that makes it invisible to the training objective. We need to uncompress it.
Softmax Temperature
Temperature scaling addresses this by flattening the distribution before using it as a soft label. The teacher's logits are divided by a temperature parameter before the softmax is applied:
where:
- : the teacher's logit (pre-softmax score) for token
- : the temperature parameter ()
- : the temperature-scaled probability for token
When , this is the standard softmax. When , the distribution becomes flatter: smaller probabilities grow relative to the largest ones. When , the distribution becomes sharper, concentrating even more mass on the peak.
To see why temperature matters concretely, consider logits . At , the softmax gives approximately : essentially a one-hot distribution. At , the same logits give roughly : a much softer distribution where the relative magnitudes of all four logits are visible in the probability values. The smaller logits now carry meaningful probability mass and can guide the student's learning.
The mechanics of why this works come from the exponential function. When logits are divided by a large , the differences between them shrink. The exponential function maps small differences to values close to 1, so the ratio between the largest and smallest probabilities decreases. What was a thousand-fold difference at becomes a moderate difference at , moving probabilities that were numerically invisible into ranges where the gradient signal is strong.
During distillation, the same temperature is applied to both the teacher's distribution (to produce soft labels) and the student's distribution (to compute the loss). The student is trained to match the teacher's soft output. After training is complete, the temperature is reset to for inference. The student's weights have been shaped by exposure to the temperature-scaled distributions, but it produces predictions at the standard temperature.
Why Temperature Amplifies Dark Knowledge
Hinton et al. introduced the term "dark knowledge" to describe the information encoded in the small probabilities that standard training ignores. A teacher that assigns probability 0.0001 to "feline" when predicting the word "cat" is revealing a structural relationship between these two words. This relationship is invisible under hard-label training but becomes visible when the distribution is temperature-scaled.
The key insight is that the ratio of probabilities at high temperature reveals structure that absolute probabilities hide. If the teacher assigns logit 10 to "cat," logit 2 to "feline," and logit to "airplane," the ratio encodes the semantic proximity of cat and feline relative to airplane. At low temperature, both probabilities round toward zero and the ratio becomes numerically unreliable. At high temperature, both probabilities are substantial and the ratio is preserved, giving the student a meaningful gradient signal that "feline" and "cat" are semantically related while "airplane" is not.
This is the distinction between distillation and a minor regularization variant. The teacher's logits contain a rich relational structure built up over training. Temperature scaling is the tool that makes this structure accessible to the student.
import numpy as np
def softmax_with_temperature(logits, temperature):
logits = np.array(logits, dtype=float)
scaled = logits / temperature
# Numerical stability: subtract max before exp
scaled -= scaled.max()
exp_vals = np.exp(scaled)
return exp_vals / exp_vals.sum()
# Teacher logits for a 5-token vocabulary: "cat", "feline", "dog", "table", "airplane"
teacher_logits = np.array([8.0, 5.0, 4.5, 1.0, -2.0])
tokens = ["cat", "feline", "dog", "table", "airplane"]
temperatures = [1, 2, 4, 8]
probs_by_temp = {
tau: softmax_with_temperature(teacher_logits, tau) for tau in temperatures
}Temperature effect on teacher distribution Token tau=1 tau=2 tau=4 tau=8 ---------------------------------------------------- cat 0.92512 0.69743 0.46618 0.32935 feline 0.04606 0.15562 0.22021 0.22636 dog 0.02794 0.12119 0.19433 0.21264 table 0.00084 0.02106 0.08101 0.13729 airplane 0.00004 0.00470 0.03827 0.09436
As temperature increases, the near-zero probabilities for "feline," "dog," and even "table" become large enough to compare. At , all five tokens carry substantial probability mass and the student can distinguish between them based on their relative values. The teacher's implicit semantic ranking, from closely related ("feline," "dog") to moderately related ("table") to unrelated ("airplane"), becomes visible in the training signal.


Choosing the Temperature
The right temperature depends on how peaked the teacher's distribution is and how much dark knowledge you want to expose. For tasks with very confident outputs (like clearly labeled image classification), moderate temperatures of to are usually sufficient to make the secondary probabilities visible. For tasks with ambiguous targets (like next-token prediction in language models), higher temperatures up to can reveal richer structure, but there is a point of diminishing returns.
At very high temperatures, the distribution approaches a uniform distribution, and the distinctions between tokens blur. The teacher's guidance becomes noise rather than signal. At very low temperatures, the distribution collapses toward a one-hot vector and the soft labels carry no more information than hard labels. The sweet spot is in between: high enough that secondary probabilities are substantial, low enough that the teacher's ranking of outputs is still preserved.
A practical approach is to treat the temperature as a hyperparameter and tune it on a held-out validation set. In practice, values in the range work well for most classification and language modeling tasks. For well-trained, high-confidence teachers, lean toward lower temperatures; for less confident or more uncertain teachers, lean toward higher temperatures.
Rescaling the Loss
When using temperature scaling during distillation, the gradients of the distillation loss are scaled by compared to the gradients of the standard supervised loss. This happens because the softmax function with temperature produces logit gradients that are times smaller than those of the standard softmax, and this factor appears twice in the computation: once for the teacher's distribution and once for the student's. The net effect is that increasing reduces the magnitude of the distillation gradient by a factor of .
To keep the distillation loss contribution stable relative to the supervised loss when varying , Hinton et al. recommend multiplying the distillation loss term by :
where denotes the distillation loss computed with temperature . The factor compensates for the gradient shrinkage caused by the temperature scaling, making sure that the magnitude of the distillation gradient remains roughly constant as varies. This makes it easier to tune independently of : you can increase the temperature to reveal more dark knowledge without accidentally reducing the effective learning rate on the distillation objective.
This correction is often omitted in implementations without dramatic consequences, especially when is tuned empirically, but it simplifies hyperparameter tuning and is good practice when sweeping over temperature values.