Knowledge Distillation

Machine Learning
Introduced by
Hinton, G., Vinyals, O. & Dean, J.
Year
2015
Type
Neural network compression (teacher–student)

What does it do?

Knowledge Distillation is a model-compression method, introduced by Geoffrey Hinton, Oriol Vinyals and Jeff Dean in 2015, in which a small student model is trained on the output distributions of a large, already-trained teacher as well as on the true labels.

The student is typically much smaller and faster than the teacher, and the aim is to keep much of the teacher's predictive performance. Its training objective combines a label-based cross-entropy term with a divergence term that pulls the student toward the teacher.

The intuition behind the method

A large, accurate model is often too slow or heavy to deploy. Rather than training a small model on hard labels alone, the small model is also trained to reproduce the large model's probability outputs. Those outputs contain more than the single correct class: they show which classes the teacher treats as similar and how confident it is about each prediction. The student learns from this additional information, which can help it generalise better than label-only training while remaining much smaller.

History

Knowledge Distillation was introduced by Geoffrey Hinton, Oriol Vinyals and Jeff Dean in a 2015 NeurIPS Deep Learning Workshop paper, which framed compressing a large model into a smaller one by training the smaller model on the larger model's outputs. Sanh and colleagues' 2019 DistilBERT paper applied the approach to BERT, producing a smaller, faster, cheaper and lighter model.

When to use it

Choose knowledge distillation when a strong, trained teacher model already exists, the deployment target needs a smaller or faster model, and a task-specific dataset is available on which the teacher's outputs can be generated and the student trained. It has been applied to large text models such as BERT. Do not use it when no trained teacher is available, because distillation cannot begin without one. Be cautious when the task dataset is small: the student then has little to learn from the teacher and is at risk of overfitting, and distillation may offer less benefit in that case. Compare the distilled student against a model trained directly on the labels using held-out data, and check the dataset size that your own task requires.

Limitations and considerations

  • Distillation cannot begin without a trained and available teacher, and the student can inherit the teacher's errors.
  • With a small task dataset, the student has little to learn from the teacher's outputs and is at risk of overfitting.
  • It adds a second training stage and the need to tune alpha on top of the original model's training.
  • The student usually approaches rather than matches the teacher, so some loss in accuracy should be expected.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • The student can be made substantially smaller and faster than the teacher, which is the main practical reason to use the method.
  • Training on the teacher's full output distributions gives the student more information per example than hard labels alone, which is the mechanism behind the expected gain in generalisation.
  • It reuses an already trained teacher, so the teacher does not need to be trained again for the compression step.
  • The mixing weight alpha gives direct control over how strongly the student follows the ground-truth labels versus the teacher's outputs.

Applications

  • Compressing a large pretrained language model into a smaller student, as in the DistilBERT work.
  • Speeding up inference for classification or prediction tasks where the full teacher is too slow to serve.
  • Shrinking models for deployment on resource-constrained hardware or in latency-sensitive settings.
  • Transferring the behaviour of a heavy model into a lightweight one by training the small model on the teacher's output distributions together with the true labels.

How it works

How it works

1

A large, well-trained teacher model is taken as given; it must already be trained and available before distillation begins. The teacher holds the predictive behaviour to be transferred, but it is usually too expensive to run at the scale where the student is needed.

2

For each input, the teacher produces its full probability distribution over the classes rather than only the top label. These distributions show which classes the teacher regards as similar, giving the student a richer training signal than the ground-truth labels alone. In the mechanics formula this distribution is denoted p_t.

3

The student is trained to satisfy two objectives at once. The loss in the mechanics formula combines a cross-entropy term between the true labels y and the student's predictions p_s with a Kullback–Leibler divergence term between the teacher's distribution p_t and the student's p_s. The mixing weight alpha sets the balance between the two terms: a larger alpha gives more weight to the true labels, and a smaller alpha gives more weight to the teacher. The formula is a simplified presentation in which the divergence is computed directly on the probability distributions. Implementations differ in how teacher and student outputs are scaled before this comparison, so the exact form used in the original paper should be checked against the source.

Formula
\mathcal{L} = \alpha\, \mathrm{CE}(y, p_s) + (1-\alpha)\, \mathrm{KL}(p_t \,\|\, p_s)

Common pitfalls

  • Starting distillation without a properly trained teacher, leaving nothing reliable to transfer.
  • Applying it to a small dataset without checking on held-out data whether it gives a benefit over a model trained directly on the labels.
  • Leaving alpha at an arbitrary value, although it sets the balance between the hard-label and teacher-based terms and so changes what the student learns.
  • Expecting the student to fully match the teacher rather than approach it.

Frequently asked questions

Why use soft labels instead of just the correct answer?

The teacher's full probability distribution reveals how similar the classes look to it and how confident it is. This extra signal, sometimes called dark knowledge, can help the student generalise better than hard labels alone.

How much smaller can the student be?

Distilled models such as DistilBERT are substantially smaller and faster than their teachers. The size and accuracy trade-off depends on the task, the architectures and the training setup, so it should be measured on your own data.

What do I need before distilling?

You need a teacher model that is already trained and available, plus a task-specific dataset. If the dataset is small, the student has little to learn from the teacher and may overfit, so check the dataset size your task requires.

What is the alpha in the loss?

Alpha mixes the two parts of the distillation loss: a cross-entropy term on the true labels and a Kullback–Leibler divergence term that aligns the student's distribution with the teacher's. Tuning it balances learning from ground truth against learning from the teacher.

Related methods

Sources

  1. Hinton, G., Vinyals, O. & Dean, J. (2015). Distilling the Knowledge in a Neural Network. NeurIPS Deep Learning Workshop.

    Open publication reference
  2. Sanh, V., Debut, L., Chaumond, J. & Wolf, T. (2019). DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter. arXiv:1910.01108.

    Open publication reference