When the true labels of the transfer set are known, Hinton, Vinyals and Dean train the small model on a weighted average of two cross entropies computed from the same student logits: one against the large model's soft targets at a high temperature T, and one against the true labels at temperature 1. The soft term is multiplied by T2. Compute the objective and its gradient for a batch.
distillation_objective(student_logits, teacher_logits, labels, T, hard_weight) -> dict
student_logits z and teacher_logits v: float arrays of shape (N, C). labels: N integers in 0..C-1. T > 0, hard_weight w∈[0,1].
- With p=softmax(v/T), q(T)=softmax(z/T) and q(1)=softmax(z) row by row:
soft=N1∑n(−∑ipnilogqni(T)),hard=N1∑n−logqn,yn(1)
loss=(1−w)T2soft+whard
- Return a dict with keys
"soft", "hard", "loss" (Python floats) and "grad": the gradient ∂loss/∂z, a float array of shape (N, C).
- The teacher logits are constants (no gradient flows to them). Logits may be in the hundreds; the computation must stay finite.