Distillation Playground

Settings

Runs in your browser, no tracking. Source

Teacher

CE on every training label

Test accuracy
Train accuracy

training loss, log scale

From scratch

CE on the labelled points only

Test accuracy
Agrees with teacher

training loss, log scale

Hard labels

α CE(teacher’s argmax) + (1−α) CE

Test accuracy
Agrees with teacher

training loss, log scale

Distilled

α T² KL(teacherT ‖ studentT) + (1−α) CE

Test accuracy
Agrees with teacher

training loss, log scale

Inside a student

Tiles: each neuron’s output over the plane. Lines: weights, thicker when larger, positive negative.

At a point

Charts

How it works

What is knowledge distillation?

A big model (the teacher) is trained first. A smaller student is then trained to imitate the teacher's full output distribution instead of, or as well as, the one-hot labels. Buciluǎ et al. (2006) compressed large ensembles this way by labelling a large synthetic transfer set with the ensemble. Ba & Caruana (2014) trained shallow nets to regress a deep net's logits. Hinton et al. (2015) gave the now-standard recipe: soften both distributions with a temperature T and mix in the ordinary label loss:

L = α · T² · KL( softmax(v/T) ‖ softmax(z/T) ) + (1 − α) · CE( y, softmax(z) )

where v are the teacher's logits and z the student's. Here all three students have the same size and the same initial weights; only their targets differ. The KL term is averaged over every training point (labelled or not) and the CE term over the labelled ones.

Three students: what does each one isolate?

From scratch sees only the labelled points. Hard labels also sees every unlabelled point, labelled with the teacher's top class: α · CE(argmax v) + (1 − α) · CE(y). That is the distillation loss with one-hot targets at T = 1, so T does not change it. Distilled gets the teacher's full probabilities at temperature T.

The gap between scratch and hard labels is what the teacher's answers on the unlabelled points are worth. The gap between hard labels and distilled is what the soft targets add beyond those answers: how sure the teacher is, and which wrong classes it finds plausible. On these toy problems the answers do most of the work for accuracy; the soft targets show up mostly in the test loss, and in the "at a point" bars near a boundary.

Why do soft targets carry information?

A one-hot label says only which class. The teacher's probabilities also say how sure it is and which wrong classes are plausible: a point near a boundary gets something like 0.6 / 0.4, a point deep inside a class gets 0.99 / 0.01. Hinton et al. call these small probabilities dark knowledge. They describe the shape of the teacher's function around each point, so one soft target carries more bits than one label. And they can be computed on unlabelled points, so the transfer set can be far larger than the labelled set.

At T = 1 a confident teacher's targets are nearly one-hot, so the small probabilities are invisible. Raising T divides the logits before the softmax, which lifts those small probabilities into view. Too high, and every class looks equally likely.

Why multiply by T²?

The gradient of the soft term with respect to a student logit is

∂/∂zi [ T² · KL ] = T · ( qi − pi ),   q = softmax(z/T), p = softmax(v/T).

For large T, qi − pi ≈ (zi − vi) / (k·T) (for zero-mean logits), so without the T² factor the soft gradient would shrink like 1/T² and changing T would silently change the balance set by α. With it, the gradient stays roughly (zi − vi)/k: in the high-temperature limit distillation becomes logit matching, the objective of Ba & Caruana.

Keyboard and controls

Space play or pause, S one epoch, R reset. Focus a plane and use the arrow keys to step through the training points. Every setting is stored in the address, so Copy link shares the exact run.

References

Source on GitHub · MIT licence