Skip to content
mlmentorship

Knowledge distillation

Train a small student to match a large teacher's outputs. The student gets richer signal than from hard labels because the teacher's soft probabilities encode similarity structure.

Published · 6 min read ·Role-specific ·Advanced

Summary

Knowledge distillation trains a student model with a loss against a teacher’s soft predictions, not the hard label. The student learns the teacher’s full output distribution, which carries information about how classes relate (Hinton et al., 2015).

Hard labels say “this is a 7.” Teacher logits say “94 percent 7, 4 percent 1, 1 percent 9, everything else 0.01.” That extra structure tells the student that 7 looks more like 1 than like 9. A small model trained against this signal usually beats the same model trained from scratch on hard labels at matched compute.

Distillation is the dominant technique for shrinking large models in production. DistilBERT, TinyBERT, MobileBERT, and most production LLMs ship distilled variants. Often combined with pruning and quantization.

The mechanism

Given teacher logits , student logits , hard label , temperature :

  • Temperature softens both distributions. Higher exposes more of the teacher’s “dark knowledge” about non-target classes. to is typical.
  • scaling is needed because softening reduces gradient magnitude by .
  • weights the hard-label loss. gives pure distillation; is common.

Learning objective

Higher temperature exposes alternatives without changing the winning class

Temperature softens alternatives without changing the winnerFor logits (2,1,0), temperature 2 increases the probability of both losing classes. The top class remains the same.Heating redistributes mass withoutchanging orderTemperature 1: same three classesA 0.6652B 0.2447C 0.09003Temperature 2: same three classesA 0.5065B 0.3072C 0.1863A stays largest; B and C gainprobability.
Derivation
Hard and soft targets for a teacher classifying a handwritten seven Three aligned horizontal bar groups compare probabilities for classes 7, 1, and 9. The hard label is 100, 0, and 0 percent. Teacher logits 5, 2, and 1 produce 93.6, 4.7, and 1.7 percent at temperature 1. The same logits at temperature 3 produce 61.3, 22.5, and 16.2 percent. Raising temperature preserves the ordering 7 above 1 above 9 while exposing the teacher's relative preference for the two wrong classes. The student is trained to match this softened distribution. class 7class 1class 9 Hard label one-hot target 100%0%0% Only “this is 7” Teacher · τ = 1 logits [5, 2, 1] 93.6%4.7%1.7% Non-target preferences are present but tiny Transfer target · τ = 3 same teacher logits 61.3%22.5%16.2% 1 remains above 9 Student matches this distribution at τ = 3
compare each class vertically. Dividing the fixed teacher logits [5, 2, 1] by a higher temperature moves probability away from class 7, but it does not change the ranking: class 1 remains more plausible than class 9. The softened 22.5% versus 16.2% target gives the student similarity information that the one-hot label discards. Teacher and student use the same temperature for the KL term; the separate hard-label term still points to class 7. Values are an original calculation following Hinton et al. (2015).
Check: What changes between the two teacher distributions? What preference remains unchanged?
Read it this way: For logits (2,1,0), temperature 2 increases the probability of both losing classes. The top class remains the same.
Does heating these logits change which class is largest?

Variants

VariantWhat it matches
Logit distillation (above)Teacher output logits
Feature distillation (FitNets)Intermediate hidden states
Attention distillation (TinyBERT)Teacher attention maps
Sequence-level distillation (Kim & Rush, 2016)Teacher’s most likely outputs (for autoregressive models)
Self-distillationTeacher and student are the same architecture; sometimes the teacher is a previous training checkpoint

For LLMs, sequence-level distillation against teacher samples (or rejection-sampled teacher outputs) is the dominant recipe. Logit distillation is impractical at vocab size 100k+.

When it works and when it doesn’t

Works well when:

  • Teacher is significantly better than what the student could reach alone.
  • Student capacity is at least 10 to 20 percent of the teacher.
  • Training data overlaps the teacher’s training distribution.

Fails when:

  • Student is too small. Capacity gap is the dominant ceiling.
  • Teacher is already small. The “dark knowledge” margin is thin.
  • Distribution shift. Teacher predictions are unreliable on student’s deployment data.

Common pitfalls

  • Forgetting scaling. Without it, the KL term has tiny gradients and the hard-label term dominates.
  • Distilling only logits when feature distillation would help. For very small students, intermediate matching is often required.
  • Skipping the temperature. collapses the teacher’s distribution to nearly one-hot for confident predictions; you lose most of the signal.
  • Training student on teacher-correct examples only. The interesting signal is on examples where the teacher is uncertain. Use the full training set.