Foundations to Frontier
Tests
E. Deep Learning › E19 Continual, Multi-Task and Transfer Learning

Knowledge Distillation

Knowledge distillation (Hinton, Vinyals and Dean, 2015) trains a small 'student' network to imitate a large 'teacher'. The student matches the teacher's softened output probabilities, not just the hard labels. Softening with a temperature T reveals how the teacher ranks the wrong classes (the 'dark knowledge'), which carries much more information per example than a single label. The teacher can also label unlimited unlabelled data. This page covers soft targets, the distillation loss, temperature, feature-based and self-distillation, and why it works. In the exercises you implement soft targets and the distillation loss. You then distil a 1994-parameter teacher into a 258-parameter student given 300 labelled examples. Hard labels gave the student 0.56 test accuracy. Distillation on the same 300 inputs gave 0.61, 0.68 and 0.70 at T = 1, 4 and 10. Adding 3000 unlabelled inputs labelled by the teacher gave 0.77, matching the teacher (0.77).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A big network teaching a small one

A big, accurate network is often too slow for a phone, a drone or a satellite. Distillation trains a small network to copy it. The trick is to copy the big network’s full opinion, not just its final answer. “90% cat, 8% dog, 2% car” tells the student that cats look more like dogs than cars. A temperature setting controls how much of that detail is shown; the demo softens a teacher’s probabilities as you raise it.

Transfer and efficiency lab
Teacher probabilities at T = 1: top class 0.880, entropy 0.47. Higher temperature reveals which wrong classes the teacher finds similar: the 'dark knowledge' a student learns from.
Why engineers care

Onboard computers on drones, rovers and satellites have tight power and memory budgets. Distillation is a standard way to compress large models (DistilBERT, TinyBERT, distilled detectors) for deployment.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Distillation trains a small model to…
  2. 2. Soft targets reveal…
  3. 3. Raising the temperature makes probabilities…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Soft targets: p_i(T) = exp(z_i / T) / Σ_j exp(z_j / T). T = 1 is the ordinary softmax; as T → ∞ it tends to uniform (entropy ln C, 2.30 for 10 classes).
  • Distillation loss: L = α T² KL(p^teacher(T) ‖ p^student(T)) + (1 − α) CE(y, p^student(1)). The T² keeps gradient sizes comparable, since soft-target gradients shrink as 1/T².
  • High-temperature limit: with zero-mean logits, matching softened outputs reduces to matching logits by squared error (Hinton et al.).
  • Variants:
    • feature (hint) distillation matches intermediate activations (FitNets);
    • attention transfer matches attention maps;
    • self-distillation uses the same architecture as both teacher and student;
    • data-free distillation synthesises inputs.
  • Compression: DistilBERT has 66M parameters against BERT-base’s 110M (1.67× smaller) and keeps about 97% of its GLUE score.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The KD loss is multiplied by…
  2. 2. As T → ∞ the soft targets become…
  3. 3. FitNets distil…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Distillation in code

Exercise 1.

  • Higher temperature softens targets.
  • The distillation loss vanishes when the student matches the teacher.

Practice.

  • Compute teacher logits once (or on the fly with augmentation) in eval mode.
  • Typical T is 2–10 and α 0.5–0.9.
  • Feed the teacher as much unlabelled, in-distribution data as you can: it is free supervision.
  • Hugging Face, torchdistill and NVIDIA TensorRT Model Optimizer provide recipes.
  • Combine distillation with pruning and quantisation for maximum compression.
T, alpha = 4.0, 0.9
with torch.no_grad(): t_logits = teacher(x)
s_logits = student(x)
loss = alpha * T * T * F.kl_div(F.log_softmax(s_logits / T, -1), F.softmax(t_logits / T, -1), reduction="batchmean") \
     + (1 - alpha) * F.cross_entropy(s_logits, y)
🐍 Exercise 1: soft targets and the distillation loss
Implement temperature softmax, the Hinton distillation loss and entropy.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Typical distillation temperatures:
  2. 2. Extra unlabelled data is…
  3. 3. Teacher logits are computed in…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why soft targets carry more information. A hard label gives log₂ C bits at most and says nothing about which wrong answers are plausible. The teacher’s distribution encodes similarity structure between classes and the uncertainty for each input. It acts as a richer, lower-variance training signal and as a data-dependent regulariser (related to label smoothing, but input-specific).

Matching a function, not labels. With unlabelled inputs, distillation becomes regression onto the teacher’s function. The student can learn it wherever it has inputs, without any labels, as long as it has the capacity. If the teacher’s function is simpler than its parameter count suggests, a small student can match it almost exactly.

Limits. The student cannot exceed the teacher on the teacher’s errors (except through regularisation effects), and capacity gaps that are too large can hurt. Teacher-assistant chains and progressive distillation address this.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Soft targets act like…
  2. 2. With unlabelled inputs, distillation becomes…
  3. 3. Very large teacher–student gaps can be bridged by…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How much does a small student gain from soft targets and unlabelled data?

History.

  • C. Buciluǎ, R. Caruana and A. Niculescu-Mizil, “Model compression” (KDD 2006).
  • G. Hinton, O. Vinyals and J. Dean, “Distilling the knowledge in a neural network” (2015).
  • V. Sanh et al., “DistilBERT” (2019).

Exercise 2.

  • Task: 10-class classification from tanh features of x ∈ ℝ²⁰.
  • Teacher: 20 → 64 → 10 (1994 parameters) trained on 3000 labelled examples (test accuracy 0.767).
  • Student: 20 → 8 → 10 (258 parameters) trained 300 Adam steps on:
    • 300 hard labels;
    • the teacher’s softened outputs on the same 300 inputs (T = 1, 4, 10, loss scaled by T²);
    • softened outputs on those 300 plus 3000 unlabelled inputs (T = 4).
student training test accuracy
hard labels (300) 0.562
distilled, T = 1 0.613
distilled, T = 4 0.675
distilled, T = 10 0.703
distilled with 3000 unlabelled inputs, T = 4 0.768
teacher (for reference) 0.767
  • Soft targets beat hard labels on the same inputs: +0.05 at T = 1, rising to +0.14 at T = 10, as softer targets expose more of the teacher’s class similarities.
  • Unlabelled data is the biggest lever: with the teacher labelling 3000 extra inputs, the 7.7× smaller student matches the teacher.
  • Seeds:
seed hard labels T = 10 with unlabelled data teacher
1 0.589 0.698 0.724 0.752
2 0.572 0.694 0.723 0.743

The ordering is the same in every seed.

  • Caveat: the student here has enough capacity for the task; with too small a student, distillation helps less.
🐍 Exercise 2 (research): how much does a small student gain from soft targets and unlabelled data?
Distil a 64-unit teacher into an 8-unit student with hard labels, soft targets at T = 1, 4, 10, and soft targets on extra unlabelled inputs.

Distillation in AI, robotics and aerospace

  • Edge deployment: compact detectors and segmenters on drones and planetary rovers distilled from large models.
  • Language models: DistilBERT, TinyBERT and distilled LLMs for onboard assistants.
  • Policies: distilling expensive planners or ensembles into fast neural controllers.
  • Design lesson: collect unlabelled in-domain data; it is the teacher’s most valuable input.

References

  1. C. Buciluǎ, R. Caruana and A. Niculescu-Mizil (2006). “Model compression.” Proc. KDD.
  2. G. Hinton, O. Vinyals and J. Dean (2015). “Distilling the knowledge in a neural network.” arXiv:1503.02531.
  3. A. Romero et al. (2015). “FitNets: hints for thin deep nets.” Proc. ICLR.
  4. V. Sanh, L. Debut, J. Chaumond and T. Wolf (2019). “DistilBERT, a distilled version of BERT.” arXiv:1910.01108.