Practice in Lab: Distill a detached teacher with temperature scaling →
Distill a detached teacher with temperature scaling
Soft teacher distributions contain relative class information that hard labels discard. Temperature changes the distribution and gradient scale, so the declared objective includes the squared temperature factor. The teacher is a fixed target: accidentally training it along with the student changes the optimization problem. KL divergence also has an argument-order convention that differs from the mathematical notation. This Lab checks actual student gradients, detached teacher ownership, mixture endpoints and agreement with an independently computed categorical divergence.
import torch
logits = torch.tensor([[4., 0.]])
print((logits/2).softmax(-1))Review the design
State what the teacher knows, which data generated targets and how temperature was selected. Distillation does not make unavailable or private training evidence disappear.
Your task
Compute (1-alpha)*cross_entropy(student,labels) + alpha*T*T*KL(softmax(teacher.detach()/T) || softmax(student/T)). Use KL batchmean, not an elementwise mean. Inputs are finite aligned [N,C] tensors, integer labels [N], C>=2, T>0 and alpha in [0,1]. Return a differentiable scalar. Teacher logits must receive no gradient.
The explanation opens here once every check passes.