DL0050 Knowledge Distillation

Describe the process and benefits of knowledge distillation.

Answer

Knowledge distillation (KD) trains a small student model to imitate a large, accurate teacher model. The key trick is learning from the teacher’s temperature-softened output distribution (“dark knowledge”: e.g., a cat image looks a bit like a dog, nothing like a truck) rather than only from one-hot hard labels. The student ends up much smaller and faster while retaining most of the teacher’s accuracy, which is why KD is the standard route to deployable models.

(1) Soft Targets: Teacher logits are passed through softmax with a temperature T > 1, exposing inter-class similarity structure that hard labels hide.
(2) Combined Loss: The student minimizes a mix of distillation loss (KL to the teacher’s soft targets) and ordinary cross-entropy on the true labels.
(3) Benefits: Compression and latency for edge/real-time deployment, plus a regularization effect: students often generalize better than the same architecture trained on hard labels alone.

Mathematical Formulation:
q_i(T) = \mathrm{softmax}(z_i / T) = \frac{e^{z_i / T}}{\sum_{j=1}^{K} e^{z_j / T}}
\mathcal{L} = \alpha\, \mathcal{L}_{\text{CE}}(y, q^{\text{student}}) + (1 - \alpha)\, T^2\, \mathrm{KL}\!\left(q^{\text{teacher}}(T) \,\|\, q^{\text{student}}(T)\right)

Where:

  • z_i is the logit for class i, K the number of classes, and T > 0 the temperature; higher T yields a smoother distribution.
  • \alpha balances hard-label CE against distillation; the T^2 factor rescales the KL term because softening shrinks its gradients by 1/T^2.
Grouped bar chart of teacher output probabilities for five classes at temperatures 1, 5, and 20, showing the distribution flattening and inter-class ratios becoming visible as temperature rises.

Figure 1: Temperature smoothing: at T = 1 only the winner class is visible; at higher T the class-similarity ratios (“dark knowledge”) emerge.

Training Setup: The teacher runs in inference mode (frozen); only the student’s weights update. Both models see the same inputs, and the two losses are computed on the student’s outputs only.

Knowledge distillation diagram: input feeds a large teacher model producing soft targets via temperature softmax and a small student model, whose soft outputs form a distillation loss against the teacher and whose hard predictions form a cross-entropy loss against ground-truth labels.

Figure 2: The KD setup: the student learns from both the teacher’s soft targets (distillation loss) and the ground truth (student loss).

Practical Caveats: A weak or biased teacher transfers its errors; T and \alpha need tuning; and an extremely small student may lack the capacity to absorb the teacher. Intermediate-feature distillation and task-specific data help close the gap.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *