← Explainer Library

Interactive Explainer

Knowledge Distillation, Live

A tiny student trained in your browser absorbs a smooth pretrained teacher through softmax temperature. Toggle three loss recipes, slide the temperature, and watch the student's decision boundary snap to the teacher's much faster than hard labels alone allow.

Prelude

Why a small model can learn from a big model better than from the data

You have a big, accurate teacher. You want a small, fast student that runs on a phone, a sensor node, an edge GPU. The naive recipe: train the student from scratch on the same data. The better recipe: train the student to imitate the teacher's softened outputs. The student often beats the data-only baseline by a lot — with the same parameters, on the same dataset.

The trick is information that's invisible in hard labels. A cat picture has a hard label "cat", but a teacher's softmax says "cat 0.93, lynx 0.04, tiger 0.02, dog 0.01". That ordering carries dark knowledge — the teacher's opinion about how classes relate. The student picks it up for free.

Hinton's slogan. "Soft targets reveal more about the structure of the data than hard labels." Once you've seen this article's animation, the phrase will mean something.
Step 1

Soften with temperature

Standard softmax converts logits $z_c$ into probabilities $p_c$. Knowledge distillation introduces a temperature $T$:

$T=1$ is the usual softmax. Larger $T$ flattens the distribution, exposing more of the relative ordering. As $T \to \infty$ everything becomes uniform; as $T \to 0$ it becomes one-hot. The whole game is to pick a $T$ where the non-winning classes still carry useful structure.

Same teacher logits at four temperatures. Bars are softmax probabilities; numbers are the same logits scaled by 1/T.
Step 2

The two losses, side by side

The student gets two supervision signals:

The $T^2$ multiplier in the KD term keeps gradients on the same scale at any $T$ (Hinton et al., 2015). $\alpha$ trades off how much the student listens to data vs. teacher.

Step 3

Watch the student learn

The teacher below is a fixed Gaussian-mixture classifier on a 2-D plane: smooth, accurate, but 'big' in the sense that we assume we don't get to ship it. The student is a single linear layer (just $2 \times K$ weights) — barely more than logistic regression. Train it three ways and compare.

step 0 student acc teacher acc
Teacher decision regions
Student decision regions
Loss curve
What to watch for. With Hard CE only, the student's boundary is jagged and slow — it has only the hard label per point. With Pure KD at $T=4$, the student inherits the teacher's smooth confidence everywhere, including in regions with no training points. Hybrid usually wins: data anchors the labels, the teacher fills in the rest. Try setting $T=1$ to kill the dark knowledge and see distillation collapse to ordinary cross-entropy.
Step 4

Where does the gain come from?

Three angles on the same phenomenon:

Per-class gradient magnitude on a single example, hard target vs soft target at $T=4$. Same total signal, redistributed.
Step 5

The variants you'll meet in papers

Step 6

Five things to remember

Tip

Pick T per task, not by intuition. Sweep $T \in \{2, 4, 8, 16\}$ on val. Big-vocabulary models (LLMs) often want $T \le 2$; small-class-count vision tasks like CIFAR-10 work at $T=4$ to $8$.

Tip

α matters. Pure KD ($\alpha=0$) inherits the teacher's mistakes. Adding back even $\alpha=0.1$ of hard CE keeps the student grounded in the actual labels.

Tip

Train the teacher with label smoothing off. Smoothed teachers throw away the dark knowledge that makes KD work (Müller et al., 2019). Counterintuitive but real.

Tip

KD is data augmentation in disguise. You can distil on unlabelled data — just use the teacher's soft labels as targets. Often this single trick beats more complex semi-supervised pipelines.

Tip

Distil before quantising / pruning. KD compresses information; quantisation discards bits. Doing them in the wrong order leaves accuracy on the floor.

Final takeaway. Distillation is the cheapest, simplest, most reliable way to shrink a model — and the only way to put a big model's opinions about uncertainty into a small one. Anywhere you ship a small model, the teacher should be in the loop.