Foundation reference

Knowledge Distillation: Learning from Teachers

Knowledge distillation trains a smaller student model to match a larger teacher’s outputs. Sanh et al. (2019) reported that DistilBERT was 40% smaller and 60% faster than BERT while retaining 97% of its performance on their language-understanding evaluations.

A newer notebook covers this concept. This older page keeps its summary, math and sources.

Concept 62 of 100EfficiencyStudy phase 6
Key equation
L=(1−α) LCE(pS,y)+α τ2 KL(pT,τ ∥ pS,τ)\mathcal{L} = (1-\alpha)\, \mathcal{L}_{\text{CE}}(p^{S}, y) + \alpha\, \tau^2\, \mathrm{KL}\big(p^{T,\tau} \,\|\, p^{S,\tau}\big)
Reading map and next steps

Study guide

Where to start

Concept 62 of 100Knowledge DistillationEfficiency · Study phase 6: Modern efficiency & inference
Question

A hard label says only "cat"; a teacher's soft output also says the image looks a little like a dog and not at all like a car. How much of what the teacher has learned can a student recover from those small probabilities?

How to study this pagePredict · Check · Generalize · Continue
PredictSay what you expect.

Before you use the demo in the newer notebook, say what you expect to change.

CheckCheck against the math and the papers.

Compare what the notebook demo shows with the key equation here and with the papers cited below.

GeneralizeName what stays the same.

Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.

ContinueContinue in the newer notebook.

The newer notebook covers the same concept and has an interactive demo.

Why It Matters for Modern Models

  • Knowledge distillation trains a smaller student model to match a larger teacher’s outputs. Sanh et al. (2019) reported that DistilBERT was 40% smaller and 60% faster than BERT while retaining 97% of its performance on their language-understanding evaluations.
  • A teacher's soft outputs carry information that hard labels lack; when the teacher is confident, much of what it has learned lies in the ratios of the very small probabilities it gives to wrong classes (Hinton et al., 2015).
  • It is used to train small language models: Gemma 2's 2- and 9-billion-parameter models were trained to match a larger teacher model's next-token probabilities instead of only the observed next token (Gemma Team, 2024), and sequence-level distillation trains a student on the teacher's generated outputs (Kim & Rush, 2016).

Questions to Think About

  • Why does raising the temperature reveal the teacher's small probabilities, and why does matching soft targets at very high temperature become the same as matching the logits (Hinton et al., 2015)?
  • Why can a student with a different architecture, which could never copy the teacher's weights, still learn from the teacher's outputs?

Interactive Demo

The interactive demo for this concept is in the newer notebook.

Open the notebook demo

Core Math

If you prefer intuition first, try the demo in the newer notebook, then come back for the math.

Key Equation
L=(1−α) LCE(pS,y)+α τ2 KL(pT,τ ∥ pS,τ)\mathcal{L} = (1-\alpha)\, \mathcal{L}_{\text{CE}}(p^{S}, y) + \alpha\, \tau^2\, \mathrm{KL}\big(p^{T,\tau} \,\|\, p^{S,\tau}\big)

A small student network SS is trained to match the output distribution of a larger teacher TT. Both distributions are softened with a temperature τ\tau:

piT,τ=exp⁡(ziT/τ)∑jexp⁡(zjT/τ)p_i^{T,\tau} = \frac{\exp(z_i^T / \tau)}{\sum_j \exp(z_j^T / \tau)}

and pS,τp^{S,\tau} is defined the same way from the student's logits zSz^S. The loss mixes the ordinary cross-entropy with the true label yy, computed at τ=1\tau = 1, and a soft-target term:

L=(1−α) LCE(pS,1,y)+α τ2 KL(pT,τ ∥ pS,τ)\mathcal{L} = (1-\alpha)\, \mathcal{L}_{\text{CE}}\big(p^{S,1}, y\big) + \alpha\, \tau^2\, \mathrm{KL}\big(p^{T,\tau} \,\|\, p^{S,\tau}\big)

The gradients of the soft-target term shrink as 1/τ21/\tau^2, so the factor τ2\tau^2 keeps the balance between the two terms roughly fixed when τ\tau changes (Hinton et al., 2015).

Raising τ\tau enlarges the teacher's small probabilities, so the student learns which wrong classes the teacher considers similar to the right one. At very high τ\tau, with the logits of each example centred to mean zero, the gradient of the soft-target term becomes proportional to that of matching the logits directly, 12∑i(ziS−ziT)2\tfrac12 \sum_i (z_i^S - z_i^T)^2 (Hinton et al., 2015, §2.1).

Key Papers

Distilling the Knowledge in a Neural Network

Hinton, Vinyals & Dean2015NIPS 2014 Deep Learning Workshop
Read paper →

DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter

Sanh et al.2019NeurIPS 2019 Workshop on Energy Efficient Machine Learning and Cognitive Computing
Read paper →

Sequence-Level Knowledge Distillation

Kim & Rush2016EMNLP
Read paper →

Gemma 2: Improving Open Language Models at a Practical Size

Gemma Team2024arXiv
Read paper →

Connections

Where to go next

A newer notebook covers Knowledge Distillation, with an interactive demo.

Continue in the newer notebook