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).
★☆☆☆☆ 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. Distillation trains a small model to…
2. Soft targets reveal…
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.).
🐍 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. Typical distillation temperatures:
2. Extra unlabelled data is…
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. Soft targets act like…
2. With unlabelled inputs, distillation becomes…
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 ∈ ℝ²⁰.
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
C. Buciluǎ, R. Caruana and A. Niculescu-Mizil (2006). “Model compression.” Proc. KDD.
G. Hinton, O. Vinyals and J. Dean (2015). “Distilling the knowledge in a neural network.” arXiv:1503.02531.
A. Romero et al. (2015). “FitNets: hints for thin deep nets.” Proc. ICLR.
V. Sanh, L. Debut, J. Chaumond and T. Wolf (2019). “DistilBERT, a distilled version of BERT.” arXiv:1910.01108.
Student 0.562 with hard labels; 0.703 with T = 10; 0.768 with 3000 unlabelled inputs (teacher 0.767).
Practice
Show:
Level 0
Problem 1Level 0
Factor multiplying the distillation loss at T = 4?
16.
Problem 2Level 0
Year of Hinton, Vinyals and Dean’s distillation paper?
2015.
Problem 3Level 0
Compression ratio from 110M to 66M parameters (3 decimals)?
1.667.
Level 1
Problem 4Level 1
Parameters of a 20 → 64 → 10 MLP with biases?
1994.
Problem 5Level 1
Parameters of a 20 → 8 → 10 MLP with biases?
258.
Problem 6Level 1
Maximum entropy over 10 classes (ln 10, 3 decimals)?
2.303.
Problem 7Level 1
Fraction of the gap recovered: student 0.675, baseline 0.562, teacher 0.767 (3 decimals)?
0.551.
Level 2
Problem 8Level 2
Student accuracy with hard labels only?
0.562.
Problem 9Level 2
Distilled student accuracy at T = 10?
0.703.
Problem 10Level 2
Student accuracy with 3000 unlabelled inputs?
0.768.
Problem 11Level 2
Teacher accuracy?
0.767.
Problem 12Level 2
Distilled student accuracy at T = 1?
0.613.
Problem 13Level 2
Labelled examples available to the student?
300.
Level 3
Problem 14Level 3
Show that the gradient of T²·CE(q, softmax(z/T)) with respect to z is T(softmax(z/T) − q).
The gradient of CE(q, softmax(u)) with respect to u is softmax(u) − q. With u = z/T, the chain rule adds a factor 1/T, so the gradient with respect to z is (softmax(z/T) − q)/T. Multiplying by T² gives T(softmax(z/T) − q). Since the difference itself shrinks roughly as 1/T for large T, the overall scale stays comparable across temperatures. ∎
Problem 15Level 3
Explain why unlabelled inputs helped so much.
With only 300 inputs, the student sees the teacher’s function at few points and must interpolate. The teacher can label any input for free, so 3300 points pin down its decision function far more densely. The task’s true function is simple enough for 8 hidden units, so the student can match the teacher almost exactly. ∎
Problem 16Level 3
Compare distillation with pruning for model compression.
Pruning removes weights from the trained model itself, keeping its architecture and needing sparse kernels or structured removal for speed-ups. Distillation trains a new, freely chosen architecture (dense, hardware-friendly) to mimic the teacher. It needs training data and compute but gives full control of the deployed model’s size and shape. The two combine well. ∎
Problem 17Level 3
Advise on deploying a terrain classifier on a small drone.
Train an accurate large teacher on the ground. Collect lots of unlabelled flight imagery and distil into a small CNN that fits the onboard accelerator, at T around 4, with augmentation. Quantise the student and validate against the teacher on held-out flights, especially rare terrain types. ∎
Level 4
Problem 18Level 4
Distilled student accuracy at T = 4?
0.675.
Problem 19Level 4
Student with unlabelled inputs, seed 1?
0.724.
Problem 20Level 4
Unlabelled inputs in the transfer set?
3000.
Coding practice
🐍 Code 1 (Level 0): compression ratio
Teacher / student parameters.
🐍 Code 2 (Level 0): MLP size
d h + h + h c + c.
🐍 Code 3 (Level 0): gradient scale
T².
🐍 Code 4 (Level 0): maximum entropy
ln C.
🐍 Code 5 (Level 0): gap recovered
(student − baseline)/(teacher − baseline).
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Knowledge Distillation: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.