TL;DR
Knowledge distillation trains a small student network to imitate a large teacher network. Instead of training the student only on the correct labels, you also train it to match the teacher's output probabilities, softened by a temperature so that the small probabilities on wrong classes are visible. Those probabilities carry information the hard label throws away: which wrong answers are close to right and which are absurd.
The student ends up smaller and faster than the teacher while keeping much of its accuracy. It is one of the main tools in model compression.
Where you'll see it: shrinking language models for phones and browsers, turning a bank of ensemble members into one network that serves a single request, and producing the small models that ship alongside large ones in many open-weight releases.
Intuition
A label is a lossy summary
Suppose an image of a dog is labelled "dog". The label says one thing. A trained teacher, shown the same image, might say dog with 0.90, wolf with 0.07, cat with 0.02, and car with almost nothing. The teacher's full output says that a dog looks like a wolf and a little like a cat, but not like a car. That ranking is knowledge about the problem, learned from many examples, and the hard label discards it.
Hinton has called this information "dark knowledge". The wrong-class probabilities are small, easy to ignore, and very informative about how the classes relate to one another.
Why train a small model at all
Big models are accurate but expensive. They need more memory, more compute per request, and often more energy than a deployment can afford. A small model is cheap to run but usually less accurate when trained from labels alone, partly because the labels give it less to learn from. Distillation fixes that imbalance by giving the small model the teacher's richer targets. The student can often reach accuracy that would be hard to get from the same data with the hard labels alone.
- cat
- dog
The plot shows the mechanism. At the teacher is almost certain, and the useful ranking is hidden in the tiny numbers. As rises, the probabilities spread and the student sees how the classes relate.
The recipe in one picture
flowchart LR X["Input example"] --> T["Teacher network"] X --> S["Student network"] T --> TS["Soft targets at temperature T"] S --> SS["Student softened outputs"] S --> SH["Student outputs at T equal 1"] Y["True label"] --> HL["Hard label loss"] TS --> KL["Soft target loss"] SS --> KL SH --> HL KL --> L["Total loss"] HL --> L
The math
Softened outputs
Let be the teacher's logits (scores before softmax) and the student's, for classes. With temperature , define
These are the temperature softmax from Softmax Temperature. The vector is the soft target, and is the student's softened prediction. The true label is , a one-hot vector.
The loss
The student minimises a weighted sum of two terms:
where
The hard term is ordinary Cross-Entropy Loss at temperature 1. The soft term is cross-entropy against the teacher's distribution, evaluated at temperature . The weight balances the two, and is a hyperparameter often chosen in the range 2 to 10.
Why the soft term equals a KL divergence
Cross-entropy splits into entropy plus a KL divergence:
The entropy does not depend on the student, so it has no effect on the gradient. Minimising is the same as minimising the KL divergence from the teacher's softened distribution to the student's.
The factor
Differentiating with respect to the student logits brings in a factor of from the chain rule, and at high temperature the soft term's gradient shrinks by about . Multiplying the soft term by keeps its gradient magnitude roughly the same across temperatures, so keeps the same meaning when you change . This is a scaling choice, not a change to what is being learned.
Worked examples
A teacher produces logits for the classes (cat, dog, car). A student produces logits . The true label is cat.
- Teacher at : , , . Sum . So . The answer is almost one-hot.
- Teacher at : divide logits by 4 to get . Exponentials: , , . Sum . So .
- Student at : divide logits by 4 to get . Exponentials: , , . Sum . So .
- Soft cross-entropy: .
- Teacher entropy: . So .
- Hard term at : the student's probability for cat is , so .
- Total with : .
Interpretation: the soft term is the larger contributor, and it measures disagreement about the whole ranking, including how the student rates dog and car. At the student gives cat 0.463 against the teacher's 0.665, and gives dog and car more mass (0.318 and 0.219) than the teacher does (0.245 and 0.090), so the soft term pushes the student to raise cat and lower dog and car.
Illustrative numbers. A teacher takes 350 ms per request on one accelerator. A distilled student, of similar architecture but a quarter of the width, takes 35 ms. The service handles 1,000,000 requests per day.
- Teacher busy time: s per day.
- Student busy time: s per day.
- Ratio: . The student needs one tenth of the accelerator time.
- If the student loses 1 point of accuracy against the teacher, the saving has to be weighed against that loss. The distillation literature usually reports a small gap, not zero, and the size of the gap depends on the task.
Interpretation: distillation changes the cost of the service, not just its model file. The question to answer before shipping is whether the accuracy gap is acceptable on your own evaluation data.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Teacher is badly calibrated or wrong on many inputs | Student copies the teacher's mistakes | Improve the teacher, or mix in the hard-label term with a larger |
| Temperature too low (near 1) | Soft targets are almost one-hot and carry little ranking information | Raise to about 2 to 10 and tune it |
| Temperature too high | Targets are nearly uniform and the signal is weak | Lower |
| Little or no unlabelled data available | The student only sees the training set the teacher was trained on | Generate extra inputs, or use a transfer set |
| Student architecture too small for the task | The gap stays large regardless of training | Choose a larger student, or lower the compression target |
| Ensemble teacher with many members | Inference cost of the teacher is still high at training time | Average the members' soft targets, then distil once |
A common pitfall is evaluating the student only on the teacher's outputs. Agreement with the teacher is not accuracy against the truth. Always report both.
History
Model compression by mimicking a large model is older than the name. Bucila, Caruana and Niculescu-Mizil reported in 2006 that a large ensemble could be used to label a huge synthetic dataset, and a small neural network trained on those labels could match the ensemble much more cheaply. Their work was about compressing ensembles, and they used the teacher's predictions as targets.
Hinton, Vinyals and Dean gave the method its name and a general form in 2015, in the paper "Distilling the Knowledge in a Neural Network". They introduced the temperature-softened soft targets and the weighted loss described above, and they argued that the soft targets carry information that hard labels lack. Ba and Caruana had shown in 2014 that a shallow student trained to mimic a deep teacher could perform well, which helped make the idea attractive.
The approach caught on because it is simple to implement and works with any architecture that outputs a distribution. Later work applied it to language models, to object detectors, and to self-supervised models, and it became one of the standard ways to produce small deployable models.
Check yourself
QuestionIn one sentence, what information do soft targets provide that a one-hot label does not?Show answer
They give the relative probabilities the teacher assigns to wrong classes, which encode how the classes are related. For example, a dog may be close to a wolf and far from a car. A one-hot label only says which class is correct.
QuestionA teacher's logits are . Compute the softened distribution at . Give the probabilities to three decimals.Show answer
Divide the logits by 2 to get . Exponentials: , , . Sum . Probabilities: , , . These sum to 1.000, as they should.
QuestionWhy does the soft loss term get multiplied by ?Show answer
Differentiating the temperature softmax brings a factor of from the chain rule, and the soft term's gradient therefore shrinks by about as grows. Multiplying by restores the gradient scale so that the weight has a consistent meaning across different temperatures. It does not change the optimum the student is trained toward.
QuestionYour distilled student matches the teacher's predictions on the training set almost perfectly, but it is no better than a model trained on hard labels on the held-out test set. What might be going on?Show answer
The student may be copying the teacher's behaviour on the training inputs without generalising, for example because the transfer data is too close to the training set, or because the teacher itself is overfitted and its soft targets are unreliable. Check the teacher's held-out accuracy, use a separate and varied set of inputs for distillation, and tune and on a validation split.
QuestionA distilled model costs 10 times less to serve but loses 2 accuracy points. Give a decision rule for whether to ship it.Show answer
Ship it only if the accuracy loss is within the product's tolerance, measured on data that resembles real traffic, and if the cost saving is needed. Compare the expected cost of the 2-point loss (for example, errors that users notice, or downstream failures) with the saving in compute. If the errors are rare and cheap, the trade is usually worth it. If errors are costly, such as in medical or financial decisions, the answer may be no, or the student may need more work before shipping.