PigeonColumba compressa

Lesson 5 of 10

The distillation loss and its gradient

Hinton's distillation loss compares the student's and teacher's softened distributions:

L = T^2 * mean_i KL(p_t || p_s), with p_t = softmax(z_t / T), p_s = softmax(z_s / T)

and KL(p || q) = sum_c p_c (log p_c - log q_c).

Why T^2? The gradient of the softened loss shrinks like 1/T^2 as T grows; multiplying back keeps its size comparable to the cross-entropy term when the two are mixed.

The gradient with respect to the student's logits works out to

dL/dz_s = T * (p_s - p_t) / N

Derive it yourself first: it's the cross-entropy gradient from lesson 1 with a soft target, with the chain rule through z / T.

Your code

Where this is used