Inferensys

Glossary

Knowledge Distillation

A model compression technique where a smaller student model is trained to mimic the softened output logits or intermediate representations of a larger, pre-trained teacher model.
ML engineer working on model compression and quantization, laptop showing performance benchmarks, technical workspace.
MODEL COMPRESSION

What is Knowledge Distillation?

A model compression technique where a compact student model is trained to replicate the behavior of a larger, more powerful teacher model, transferring dark knowledge through softened probability distributions.

Knowledge distillation is a model compression technique where a smaller student model is trained to mimic the output logits and intermediate representations of a larger, pre-trained teacher model. Rather than training on hard labels alone, the student learns from the teacher's softened probability distribution, which encodes rich inter-class similarity information known as dark knowledge. This transfers the teacher's generalization capabilities into a compact, deployment-efficient architecture.

The process uses a temperature parameter in the softmax function to soften the teacher's output distribution, revealing subtle relationships between classes that hard labels obscure. The student is optimized using a weighted combination of the standard task loss and a distillation loss—typically Kullback-Leibler divergence—between its softened outputs and the teacher's. Variants include feature-based distillation, where the student matches the teacher's intermediate activations, and self-distillation, where a model's own predictions serve as the teacher.

MODEL COMPRESSION

Key Characteristics of Knowledge Distillation

A teacher-student training paradigm where a compact model learns to replicate the generalized behavior of a larger, more complex model by matching its softened output distribution.

01

Teacher-Student Architecture

The core framework involves two distinct models: a large, pre-trained teacher and a compact, untrained student. The student is trained on a composite loss function that combines the standard hard-label loss with a distillation loss that penalizes divergence from the teacher's softened probability distribution. The teacher's weights are frozen during this process, and the student's architecture is typically a shallower or thinner variant of a convolutional or transformer backbone.

02

Soft Targets and Temperature Scaling

The teacher's raw output logits are passed through a softmax function with a raised temperature parameter (T > 1). This softens the probability distribution, revealing the dark knowledge of inter-class similarities learned by the teacher. For example, a teacher classifying a car might assign a higher probability to 'truck' than to 'apple', providing richer supervisory signal than a one-hot hard label. The student is trained to match this softened distribution using Kullback-Leibler divergence.

03

Distillation Loss Formulation

The total training objective for the student is a weighted sum of two loss terms:

  • Student Loss: Cross-entropy between the student's predictions (at T=1) and the ground-truth hard labels.
  • Distillation Loss: KL divergence between the softened student output (at T>1) and the softened teacher output (at T>1), scaled by T² to maintain gradient magnitudes. The weighting hyperparameter α balances the influence of the true labels against the teacher's guidance.
04

Feature-Based and Relation-Based Distillation

Beyond matching output probabilities, distillation can occur at intermediate layers:

  • Feature-based distillation: The student is trained to minimize the L2 distance between its intermediate feature maps and those of the teacher, often after a learned linear projection to align dimensions.
  • Relation-based distillation: The student learns to preserve the pairwise similarity structure of the teacher's representations, transferring the manifold geometry of the embedding space rather than individual activations.
05

Self-Distillation and Online Distillation

Distillation variants that eliminate the need for a separate pre-trained teacher:

  • Self-distillation: A model is iteratively trained where a later checkpoint serves as the teacher for an earlier one, or the same architecture is used for both roles, improving generalization without a larger model.
  • Online distillation: The teacher and student are trained simultaneously from scratch, with the teacher's parameters updated as an exponential moving average of the student's weights, as seen in frameworks like DINO.
06

Application in Vision Transformers

Knowledge distillation is critical for deploying Vision Transformers on resource-constrained devices. The DeiT (Data-efficient Image Transformer) training recipe uses a convolutional RegNet as a teacher to train a ViT student on ImageNet-1k without external data. The distillation token, a dedicated learnable embedding, interacts with the teacher's hard-label predictions through a separate cross-entropy loss, enabling the transformer to learn convolutional inductive biases.

KNOWLEDGE DISTILLATION

Frequently Asked Questions

Clear, technically precise answers to the most common questions about model compression via knowledge distillation, targeting the mechanisms, trade-offs, and implementation details that matter to engineering teams.

Knowledge distillation is a model compression technique where a smaller, computationally efficient student model is trained to mimic the behavior of a larger, pre-trained teacher model. Instead of training the student solely on hard ground-truth labels, the student learns from the teacher's softened output probability distribution over classes, known as soft labels or soft targets. These soft targets are generated by raising the temperature parameter T in the final softmax layer of the teacher, which reveals the dark knowledge—the relative similarities between incorrect classes that the teacher has learned. The student is trained with a composite loss function that combines the standard cross-entropy loss against the true labels and a distillation loss, typically Kullback-Leibler divergence, against the teacher's soft targets. This process transfers the generalization capabilities and inter-class relationships of the cumbersome teacher into a compact, deployable student model without requiring the original massive training dataset.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.