Knowledge Distillation

A small student network learns by copying a big teacher.

🔴 advanced5 min readefficiency
Knowledge distillation trains a small, fast student network to imitate a large teacher network. The trick is to train the student on the teacher's full set of probabilities, not just its final answers. If the teacher thinks an image is ninety percent dog and eight percent wolf, that small wolf score teaches the student which classes look alike, information a plain label throws away. Softening the probabilities with a temperature makes this easier to learn, and the student often keeps most of the teacher's accuracy.

What is Knowledge Distillation?

Large deep neural networks (Teacher models like GPT-4 or 70B LLMs) achieve outstanding accuracy, but are too slow and expensive to deploy for high-throughput real-time APIs.

Knowledge Distillation (Geoffrey Hinton et al., 2015) compresses a large, high-capacity Teacher Model into a compact, ultra-fast Student Model (e.g. DistilBERT or 8B LLMs).

  TEACHER MODEL (Giant, 70B Params) ──► Soft Logits (T = 4.0) ──┐
                                                                 ├──► [ KL DIVERGENCE LOSS ] ──► Update STUDENT (8B)!
  STUDENT MODEL (Compact, 8B Params) ─► Soft Logits (T = 4.0) ──┘

The Student often keeps most of the Teacher's accuracy while running much faster on cheaper hardware, though the exact trade-off depends on the task and the size gap.

The Concept of "Dark Knowledge"

Why train a Student model on Teacher probabilities instead of raw ground-truth hard labels ([1,0,0][1, 0, 0])?

Consider classifying an image of a BMW Car:

The Teacher's soft probabilities reveal Dark Knowledge: a BMW shares visual features with a Truck, but shares zero features with a Cat!

Hard one-hot labels discard this rich structural relationship. Soft targets convey rich geometric decision boundaries to the Student.

Temperature-Scaled Softmax

To extract dark knowledge from small non-target logits, apply Temperature Scaling (TT) to Softmax outputs:

qi=exp⁡(zi/T)∑jexp⁡(zj/T)q_i = \frac{\exp(z_i / T)}{\sum_j \exp(z_j / T)}
┌──────────────────────────┬──────────────────────────┐
│ TEMPERATURE T = 1.0      │ TEMPERATURE T = 4.0      │
├──────────────────────────┼──────────────────────────┤
│ Standard Softmax.        │ Softens probabilities!   │
│ Sharp probabilities.     │ Magnifies tiny non-target│
│ Non-target logits hidden.│ class logit relationships│
└──────────────────────────┴──────────────────────────┘

Setting T>1.0T > 1.0 (typically T=3.0–5.0T = 3.0\text{--}5.0) smooths the output distribution, making relative non-target logit probabilities visible to the Student.

Combined Loss Function

The Student is trained using a weighted combination of two losses:

LStudent=α⋅T2⋅LKL(qsT,qtT)+(1−α)⋅LCE(ys,ytrue)\mathcal{L}_{\text{Student}} = \alpha \cdot T^2 \cdot \mathcal{L}_{\text{KL}}(q_s^T, q_t^T) + (1 - \alpha) \cdot \mathcal{L}_{\text{CE}}(y_s, y_{\text{true}})
  1. Distillation Loss (LKL\mathcal{L}_{\text{KL}}): Kullback-Leibler Divergence between Student soft logits qsTq_s^T and Teacher soft logits qtTq_t^T at temperature TT. (Scaled by T2T^2 to balance gradient magnitudes).
  2. Student Loss (LCE\mathcal{L}_{\text{CE}}): Standard Cross-Entropy loss between Student outputs at T=1.0T=1.0 and hard ground-truth labels ytruey_{\text{true}}.

Distillation Types in Practice

Say this out loud

Knowledge Distillation compresses a large Teacher model into a small Student model using soft probability targets. Temperature scaling softens logit distributions to reveal Dark Knowledge inter class similarity relationships. The Student is trained using KL divergence on soft Teacher logits combined with Cross Entropy on hard labels.

Followups to expect

  1. What is Self-Distillation? Training a student model using a teacher model that shares the exact same network architecture, where the student iteratively matches past epoch predictions to improve generalization.
  2. What is Synthetic Data Distillation for LLMs? Using a massive teacher LLM (GPT-4) to generate 100,000 synthetic reasoning outputs, training a smaller student LLM (LLaMA 8B) on the generated text using standard SFT.

Check yourself

Question 1 of 3

What core concept defined by Geoffrey Hinton (2015) explains why learning from soft probability distributions outperforms learning from hard 0/1 one-hot labels?

More in Deep Learning

See all →
Activation Functions4 minDropout4 minBackpropagation5 min