Neural Network

Knowledge Distillation: Compressing a Large Network Into a Deployable Model

khaled August 20, 2023 3 mins read
Knowledge Distillation: Compressing a Large Network Into a Deployable Model

Knowledge Distillation: Compressing a Large Network Into a Deployable Model

Training a large neural network to high accuracy is one problem. Deploying it with real latency and memory constraints is another. Knowledge distillation — training a small student network to mimic a large teacher network — is one of the most effective model compression techniques. It regularly achieves 5–10× parameter reduction with less than 2% accuracy loss, making it the compression method of choice for production deployment.

The Core Idea: Soft Targets

Standard supervised training uses hard labels — one-hot vectors where the correct class has probability 1. Hinton, Vinyals, and Dean (2015) introduced the key insight: a trained network's output probability distribution is far more informative than its hard label.

When a teacher network classifies a cat image, it also assigns small but meaningful probabilities to "tiger" and "leopard." These non-zero probabilities encode the teacher's learned similarity structure — the dark knowledge. Training a student on these soft target distributions transfers structure that hard labels discard entirely.

The student's loss combines:

  • Soft loss: cross-entropy between student and teacher soft predictions (at temperature T)
  • Hard loss: standard cross-entropy against true labels

The temperature T controls how soft the distributions are — higher T reveals more inter-class relationships.

Intermediate Layer Distillation

Output-level distillation transfers knowledge from the teacher's final predictions. Intermediate distillation transfers representations from hidden layers — attention maps, feature maps, or intermediate activations.

FitNets align intermediate feature maps between teacher and student using regression losses. This is particularly effective when the student is much smaller, providing richer guidance than output-level signals alone.

Attention transfer: distil attention maps from each residual block's feature maps — effective for ResNets and ViTs.

Relational knowledge distillation: match pairwise relations between examples in a mini-batch, transferring structural properties of the representation space rather than absolute activation values.

Distillation for Language Models

Knowledge distillation has been extraordinarily productive for compressing large language models:

  • DistilBERT: a 6-layer student distilled from BERT-base (12 layers), retaining 97% of BERT's performance on GLUE with 40% fewer parameters and 60% faster inference
  • TinyBERT: uses both output and intermediate layer distillation for 7.5× parameter reduction
  • MiniLM: uses relation distillation on self-attention outputs for aggressive compression with high quality

Ensemble Distillation

Ensembles consistently outperform single models but are expensive to serve. Distilling an ensemble into a single student achieves near-ensemble performance at single-model inference cost. The ensemble's averaged soft predictions serve as richer targets than any individual model.

Implementation

# Combined distillation loss
temperature = 4.0
soft_loss = F.kl_div(
    F.log_softmax(student_logits / temperature, dim=1),
    F.softmax(teacher_logits / temperature, dim=1),
    reduction='batchmean'
) * (temperature ** 2)

hard_loss = F.cross_entropy(student_logits, labels)

loss = 0.7 * soft_loss + 0.3 * hard_loss

Conclusion

Knowledge distillation is the most principled and consistently effective approach to neural network compression for deployment. Soft targets transfer structure that hard labels discard. Intermediate layer distillation provides additional signal for aggressive compression. Ensemble distillation delivers ensemble-level performance at single-model cost. Start with output-level distillation; add intermediate distillation if you need greater compression ratios.

Keywords: knowledge distillation, model compression, teacher student network, soft targets, DistilBERT, TinyBERT, intermediate distillation, ensemble distillation, neural network deployment