Deep Learning

Knowledge Distillation

Compressing massive Teacher models into fast Student networks using dark knowledge soft targets.

🔴 advanced5 min readefficiency
Knowledge Distillation (Hinton et al., 2015) compresses large Teacher neural networks into smaller, faster Student models. The Student is trained on Soft Targets generated by passing data through the Teacher at a high Temperature T. Soft probabilities reveal Dark Knowledge (inter class similarity relationships), allowing a compact Student model to match 95 percent of a giant Teacher's accuracy at 10x faster inference speed.

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 achieves $\sim 95%$ of Teacher accuracy while running $10\times$ faster on cheaper inference hardware!

The Concept of "Dark Knowledge"

Why train a Student model on Teacher probabilities instead of raw ground-truth hard labels ($[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 ($T$) to Softmax outputs:

$$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.0$ (typically $T = 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:

$$\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 ($\mathcal{L}_{\text{KL}}$): Kullback-Leibler Divergence between Student soft logits $q_s^T$ and Teacher soft logits $q_t^T$ at temperature $T$. (Scaled by $T^2$ to balance gradient magnitudes).
  2. Student Loss ($\mathcal{L}_{\text{CE}}$): Standard Cross-Entropy loss between Student outputs at $T=1.0$ and hard ground-truth labels $y_{\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