JA EN
LearnDistillation & Compression
·★ MEMBER·PAPER·9 min read

A Field Guide to Distillation Recipes — logit, feature, attention, self

Every distillation recipe is an answer to one question: which part of the teacher should the student match? This guide lines up output (logit), intermediate feature (FitNet), attention, and self-distillation in one table and four equations, then turns the choice into a decision you make from constraints — is the teacher behind an API, do the layers line up, do the head counts match.

ModalitytextTasktraining

Distilling the Knowledge in a Neural Network


There is more than one place to copy from

When you learn to cook from a master, there are several things you could imitate. You could copy only the taste of the finished dish. You could copy how the vegetables are cut during prep. You could copy where the master's eyes go while the pan is on the heat. Or you could skip the master entirely and use yesterday's version of yourself as today's standard.

That is the whole taxonomy of distillation recipes. Which part of the teacher does the student match? Change the location and you change what you need from the teacher, the shape of the loss, and the conditions under which it works at all.

The foundation — why a trained model's output carries more information than a hard label — is covered in Knowledge Distillation from Scratch. This article is the map that comes next: which recipe do you actually reach for.

The four recipes in one table

Recipe What is matched What the teacher must expose Where it shines Main trap
Output (logit / response) Final probability distribution Output distribution only Teacher behind an API, wildly different architectures Signal enters at one point only; deep students starve
Feature (FitNet) Hidden-layer feature vectors Intermediate activations Thin, deep students from the same family Dimension and scale mismatch, arbitrary layer pairing
Attention Attention matrices, or maps derived from them Per-layer attention weights Transformer-to-Transformer, pre-training-stage compression Head count, sequence length, padding positions
Self The model's own (or its deeper layers') output No teacher at all No teacher available, want regularization Reinforces its own mistakes; gains are modest

The rest of the article opens each row in turn: equation, then something you can play with, then code.

Recipe 1: output distillation — the fewest ingredients

The classic recipe, still the workhorse. You pull the teacher's and the student's final probability distributions together and never touch the teacher's internals.

L=(1α)CE(y,pS)+αT2KL ⁣(pT(T)pS(T))\mathcal{L} = (1-\alpha)\,\mathrm{CE}(y, p_S) + \alpha T^2 \cdot \mathrm{KL}\!\left(p_T^{(T)} \,\|\, p_S^{(T)}\right)
(1)

Put in words, this says: mix "match the hard label" and "match the teacher's distribution after it has been softened by temperature" in proportion α\alpha. Here yy is the ground-truth label, CE\mathrm{CE} is cross-entropy, pSp_S is the student's probability, pT(T)p_T^{(T)} and pS(T)p_S^{(T)} are the teacher's and student's distributions flattened by temperature TT, and KL\mathrm{KL} measures how far apart two distributions are (KL Divergence from Scratch).

Temperature divides the logits by TT before the softmax: raise TT and the distribution flattens. Flattening surfaces the runners-up — the candidates that were not first but were close — and that is where the teacher's knowledge lives. The T2T^2 in front of equation (1) is a correction. Dividing by the temperature shrinks the gradient by 1/T21/T^2, so without multiplying it back, changing the temperature silently changes your effective learning rate.

FIG 1Raise the temperature slider and the single dominant bar flattens, letting second and third place become visible. That flattened shape is the "soft knowledge" the teacher is handing over

The strength of this recipe is how little it asks for. You do not need the teacher's weights, the architectures can be completely unrelated, and as long as the vocabulary is shared it runs. That is why it is the default first attempt.

The weakness is the mirror image: information enters at a single point, the exit. Deep students receive a faint signal in their lower layers, and whatever intermediate features the teacher had decomposed the input into cannot be recovered once they have collapsed into a probability vector. Read the next three recipes as three answers to that one problem.

Recipe 2: feature distillation — matching the prep work

If the exit alone is not enough, match the middle too. That is the FitNet idea: take the output of one teacher layer (the hint) and pull the output of one student layer (the guided layer) toward it directly.

Lhint=r(hS(j))hT(i)22\mathcal{L}_{\text{hint}} = \left\| r\big(h_S^{(j)}\big) - h_T^{(i)} \right\|_2^2
(2)

Stated in words, all this does is move the point of comparison from the exit to the middle: pick one shelf inside each network, and penalize the difference between what sits on them. What is being compared is work in progress, not the final probability vector.

Read it as: take the student's layer- feature vector , reshape it with a regressor , and square the difference against the teacher's layer- feature . The is the squared distance — element-wise differences squared and summed.

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. Distilling the Knowledge in a Neural Network. arXiv:1503.02531Paper page·PDF
  2. FitNets: Hints for Thin Deep Nets. arXiv:1412.6550Paper page·PDF
  3. Paying More Attention to Attention: Improving the Performance of Convolutional Neural Networks via Attention Transfer. arXiv:1612.03928Paper page·PDF
  4. TinyBERT: Distilling BERT for Natural Language Understanding. arXiv:1909.10351Paper page·PDF
  5. MiniLM: Deep Self-Attention Distillation for Task-Agnostic Compression of Pre-Trained Transformers. arXiv:2002.10957Paper page·PDF
  6. Born-Again Neural Networks. arXiv:1805.04770Paper page·PDF
  7. Be Your Own Teacher: Improve the Performance of Convolutional Neural Networks via Self Distillation. arXiv:1905.08094Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment