lenatriestounderstand

Chapter 43 of 57

Temperature in Distillation, and Why the Loss Is Multiplied by T²

Created Sep 19, 2026 Updated Oct 25, 2026

In the classic distillation loss you will usually see a temperature and, in front of the soft-target term, a factor of T2T^2:

L = α · CE(y, q₁) + (1 − α) · T² · L_soft,     L_soft = KL(p_T ‖ q_T)

Only the second term runs at temperature T: the hard-label term compares the student's unsoftened q₁ with the label.

Both parts get misread. The temperature here softens two distributions at once, not one, and the T2T^2 is not a second knob: it compensates for the way changing TT changes the size of the gradient. TT is a real hyperparameter; T2T^2 is a rescaling convention attached to it, not a term with a life of its own.

The same operation as the sampling temperature, in a different job. Dividing logits by T before the softmax is exactly what happens in LLM decoding; the arithmetic does not know which field it is in. What changes is the use. In decoding, one distribution is reshaped and a token is sampled from it. In distillation nothing is sampled: two distributions are softened, the teacher's and the student's, and the loss compares them. Raising T makes the teacher's ranking of the wrong classes legible — the dark knowledge — and asks the student to reproduce it.

Where the T2T^2 comes from. Two factors of 1/T1/T appear in the soft term, and they have different origins. Differentiating LsoftL_{\text{soft}} with respect to a student logit gives one of them directly:

∂Lsoft∂zi=1T(qi−pi)\frac{\partial L_{\text{soft}}}{\partial z_i} = \frac{1}{T}\left(q_i - p_i\right)

The second is not in the derivative but in the quantity it contains. At a high temperature both distributions flatten, so the probability differences themselves shrink roughly as 1/T1/T: with NN classes and logits centred to mean zero, qi−pi≈(zi−vi)/(NT)q_i - p_i \approx (z_i - v_i) / (N T), where vv are the teacher's logits. Together:

∂Lsoft∂zi≈zi−viNT2\frac{\partial L_{\text{soft}}}{\partial z_i} \approx \frac{z_i - v_i}{N T^2}

So the soft-target gradient scales roughly as 1/T21/T^2. Uncorrected, raising the temperature would quietly turn the distillation term down, and every temperature sweep would be a learning-rate sweep in disguise. Multiplying by T2T^2 compensates for that shrinkage and keeps the two terms' gradients comparable as TT moves — approximately, not exactly, because the 1/T21/T^2 result is a high-temperature approximation and the distributions change shape non-linearly below it.

On the teacher's real logits the raw gradient falls by two orders of magnitude between T=1T = 1 and T=20T = 20; scaled by T2T^2 it holds to within 1.4× from T=4T = 4 upward, and still moves 3.4× across the whole range.

That also says what the two ends of the range are. As TT grows, both distributions flatten and the raw KL\mathrm{KL} goes to zero; what stays finite is T2 KLT^2\,\mathrm{KL}, which approaches a squared error between the teacher's and the student's centred logits. So high-temperature distillation is logit regression, T=1T = 1 is cross-entropy against the teacher's probabilities, and for T≥1T \geq 1 the temperature interpolates between them — below 1 it sharpens instead, and the soft targets lose the thing they were for.

And the loss is cross-entropy either way. KL splits into cross-entropy minus the teacher's own entropy:

KL(p ‖ q) = −H(p) + CE(p, q)

The teacher is frozen, so H(p) is a constant: the two objectives have identical gradients. Nothing here is distillation-specific — it is the maximum-likelihood cross-entropy you already use, pointed at a soft target instead of a one-hot one.

And in a measured sweep the knob is mild. On a ten-class image problem (the widget's second view) T=4T = 4 was worth 2.7 percentage points over T=1T = 1, and T=8T = 8 gave a quarter of that back: the movement is the step from "almost one-hot" to "actually softened", not a sharp optimum. Whether that holds with a hundred thousand classes is a different measurement.

So: one temperature, applied to both sides, with a T² that exists to make temperature sweeps mean what they appear to mean. The measured sweep, and what happens when the label term α is mixed back in, are in Knowledge Distillation: What a Teacher Actually Teaches.