Skip to content

Knowledge distillation

Also called: model distillation, teacher-student distillation, distilled model, distilled models

Published:

Where did knowledge distillation come from?

The paper that made the idea popular is Hinton, Vinyals and Dean's from 2015. Their starting point: averaging the predictions of many models improves results, but deploying that whole ensemble is expensive [1]. They propose compressing the ensemble's knowledge into a single model and test it on MNIST and on the acoustic model of a heavily used commercial speech recognition system [1].

How are the teacher and student trained?

The Keras example walks through it. A small student model is trained to match a large, pre-trained teacher [2]. The loss has two parts: matching the teacher's softened logits and matching the ground-truth labels [2]. Softening means dividing the logits by a temperature inside the softmax, which flattens the probability distribution and exposes the relationships between classes that the teacher has learned [2]. Only the student's weights are updated [2].

A simple way to picture it: a teacher that reads handwritten digits might give a 7 a high probability of being 7 and a small but non-zero probability of being 1. The student learns not just that the image is a 7, but that a 7 can look like a 1.

How is it used with language models?

DistilBERT is one of the best-known examples. Its authors apply distillation during pre-training and use a triple loss that combines language modelling, distillation and cosine-distance losses [3]. The result is 40% smaller and 60% faster than BERT while keeping 97% of its language understanding [3].

Frequently asked questions

How is distillation different from quantization?

Quantization stores the same model in fewer bits. Distillation trains a new, smaller model to imitate a larger one.

Can a student model beat its teacher?

The usual aim is to stay close to the teacher; DistilBERT keeps 97% of BERT's language understanding.

Sources

  1. Distilling the Knowledge in a Neural Network — arXiv
  2. Knowledge Distillation — Keras
  3. DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter — arXiv