Model distillation: training a small student from a large teacher

Updated

Model distillation is a technique where a compact student model learns to mimic a larger teacher model. The student is trained on the teacher's outputs, often using soft targets with a temperature parameter. This approach reduces model size and inference cost while retaining most of the teacher's performance.

What is model distillation?

Model distillation is a machine learning technique that transfers the knowledge of a large pre-trained model, called the teacher, to a smaller model, called the student. The goal is to create a compact model that performs similarly to the larger one but is more efficient for deployment.

The concept was formalized in the paper "Distilling the Knowledge in a Neural Network" by Hinton, Vinyals, and Dean, as described in the arXiv publication. They proposed training a student model on the soft outputs of a teacher model, using a temperature parameter to soften the probability distribution.

This approach is inspired by the observation that large models often have redundant capacity. Distillation compresses that knowledge into a smaller network without requiring the student to have the same architecture as the teacher, as explained in IBM's overview of knowledge distillation.

How distillation works

In standard training, a model learns from hard labels. In distillation, the student learns from the teacher's soft predictions, which contain richer information about class similarities. The teacher's logits are divided by a temperature parameter before applying softmax, producing a softer probability distribution.

The student is trained to minimize a weighted combination of the standard cross-entropy loss with ground truth labels and a distillation loss that measures the divergence between the student's and teacher's soft targets. The temperature is typically set to a value greater than 1 during training and reduced to 1 at inference.

Knowledge distillation does not require the student and teacher to share the same architecture. Any deep learning model can serve as teacher or student, as noted in the PyTorch knowledge distillation tutorial.

Benefits and applications

Distillation produces smaller models that require less memory and computational power, making them suitable for edge devices, mobile phones, and offline inference. The student model can be deployed where the teacher would be too slow or expensive.

A prominent example is DistilBERT, a distilled version of BERT. According to the DistilBERT paper, the model is 40% smaller, 60% faster, and retains 97% of BERT's language understanding capabilities. This demonstrates the effectiveness of distillation for natural language processing.

Small language models built via distillation are increasingly used in resource-constrained environments, as described in IBM's article on small language models. They enable AI capabilities on devices without constant network connectivity.

Practical implementation

Implementing knowledge distillation in frameworks like PyTorch involves modifying the training loop to include the teacher's outputs. The PyTorch knowledge distillation tutorial demonstrates this on the CIFAR-10 dataset, using a larger network as teacher and a lightweight network as student.

The tutorial adds a distillation loss, typically Kullback-Leibler divergence, between the student's and teacher's softened logits. The total loss is a weighted sum of the distillation loss and the standard cross-entropy loss. The teacher's weights are frozen during training.

This approach improves the student's accuracy without changing its architecture or inference speed. The tutorial provides code examples for extracting hidden representations and incorporating additional losses.

DistilBERT distillation resultsSize reduction: 40 percent; Speed increase: 60 percent; Capability retention: 97 percentDistilBERT distillation resultsSize reduction40 percentSpeed increase60 percentCapability retention97 percent
Figures from arXiv
Key components in knowledge distillation
ComponentDescriptionExample
Teacher modelLarge pre-trained model that provides knowledgeBERT (large)
Student modelSmaller model that learns from teacherDistilBERT
TemperatureParameter that softens probability distributionT > 1 during training, T=1 at inference
Soft targetsTeacher's softened output probabilitiesLogits divided by temperature
Distillation lossMeasures difference between student and teacher soft targetsKL divergence
Combined lossWeighted sum of distillation loss and cross-entropy lossα * distillation_loss + (1-α) * cross_entropy

Frequently asked questions

What is the temperature parameter in knowledge distillation?

The temperature is a scalar that divides the teacher's logits before softmax, producing a softer probability distribution. A higher temperature makes the distribution more uniform, revealing inter-class similarities. During training, temperature is typically set above 1, and at inference it is set to 1.

Can the student model have a different architecture than the teacher?

Yes, knowledge distillation does not require the student and teacher to share the same architecture. Any deep learning model can serve as teacher or student, as long as the output dimensions match.

How much smaller can the student model be?

The size reduction depends on the application. For example, DistilBERT is 40% smaller than BERT while retaining 97% of its capabilities. In general, distillation can produce models that are significantly smaller while maintaining most of the teacher's performance.

Is knowledge distillation only used for classification tasks?

No, distillation can be applied to various tasks including regression, natural language generation, and object detection. The technique adapts to different output types by using appropriate loss functions.

What is the difference between knowledge distillation and model pruning?

Pruning removes redundant parameters from a model, while distillation trains a separate smaller model to mimic a larger one. Both reduce model size, but distillation can achieve a more compact model by learning from the teacher's behavior rather than simply removing weights.