PythiloLesson 3 of 9 · progress kept in this browser

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))
This example uses torch. Run it in the Pythilo app, where these features are available.
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.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.