Skip to content
AI360XpertBeta
Core ML

Visual explainer

Knowledge Distillation

Train a student to match a teacher distribution, then serve the student alone. Follow temperature, gradients, hard labels and the limits of imitation.

A smaller student can meet a weight-storage budget that excludes its teacher.
A smaller student can meet a weight-storage budget that excludes its teacher.

Knowledge distillation trains a student using a teacher's outputs. A smaller student can be easier to deploy, but accuracy must be checked. Our synthetic budget has 10 million float32 teacher weights and 2 million student weights: at four bytes each, that is 40 MB versus 8 MB. The dashed 16 MB budget excludes the teacher's weights but accommodates the student's. Bars use eight drawing units per MB; runtime buffers and activations are excluded.

More than one label

A teacher distribution distinguishes alternatives that a hard label leaves at zero.
A teacher distribution distinguishes alternatives that a hard label leaves at zero.

Use class order D=dog, C=cat, V=vehicle throughout. A hard dog target is (1,0,0)(1,0,0). Teacher logits (2,1,0)(2,1,0) at temperature one give approximately (0.665241,0.244728,0.090031)(0.665241,0.244728,0.090031). Each bar has height 240p240p; zero targets are dots, not short positive bars. The teacher assigns different probabilities to the two alternatives. Those are model beliefs, not a universal semantic-distance scale.

Compute the soft targets

Raising temperature softens the same logits without changing their order.
Raising temperature softens the same logits without changing their order.

For logits zz, pi(T)=ezi/T/∑jezj/Tp_i(T)=e^{z_i/T}/\sum_j e^{z_j/T}. At T=4T=4, the scaled logits are (0.5,0.25,0)(0.5,0.25,0), exponentials approximately (1.648721,1.284025,1)(1.648721,1.284025,1) and their sum 3.9327473.932747. Division gives (0.419229,0.326496,0.254275)(0.419229,0.326496,0.254275). Both panels keep the 240p240p scale; the smaller bars have gained mass from dog. Temperature four is an illustrative setting, not a prescription.

Update only the student

The student changes to match outputs while the teacher stays frozen.
The student changes to match outputs while the teacher stays frozen.

The input forks to both models; their predictions enter the matching objective. The double border marks the frozen teacher, and the lower return lane updates only the student. This form of distillation imitates output distributions; it does not establish that internal reasoning or representations have been copied. The distillation paper defines the temperature-based training setup.

Serve the smaller path

The deployed prediction path needs only the trained student.
The deployed prediction path needs only the trained student.

The teacher has no connection to the serving path. The three output bars illustrate a student that exactly matches the original logits; their heights use 200p200p, a deliberate smaller scale to fit the pipeline. This is a possible output, not a measured training outcome. Teacher prediction generation and student training cost computation before deployment; the deployed student need not run the teacher.

Return to inference temperature

A trained student returns to temperature one for ordinary inference.
A trained student returns to temperature one for ordinary inference.

This controlled example holds the student's final logits at (2,1,0)(2,1,0) and changes only the stage: matching at T=4T=4, serving at T=1T=1. The bars return to the 240p240p scale. Both models use the same temperature for soft-target matching. The original method returns the student to one for ordinary inference. Sharper probabilities do not establish correctness.

Check the student family

An affine student cannot exactly match a peaked teacher response.
An affine student cannot exactly match a peaked teacher response.

Switch to a synthetic scalar regression problem to isolate capacity. Teacher outputs at inputs (0,1,2)(0,1,2) are (0,1,0)(0,1,0). Restrict the student to f(x)=ax+bf(x)=ax+b. No line passes through all three points; its least-squares solution is a=0,b=1/3a=0,b=1/3, with mean squared error (1/9+4/9+1/9)/3=2/9(1/9+4/9+1/9)/3=2/9. Coordinates use x=120+180ux=120+180u and y=280−180vy=280-180v. This limit follows from the restricted student family, not from a universal minimum model size.

Follow one gradient

The distillation gradient raises the underpredicted class and lowers the others.
The distillation gradient raises the underpredicted class and lowers the others.

Return to classification. Solid bars show a uniform student q=(1/3,1/3,1/3)q=(1/3,1/3,1/3); dashed outlines show the teacher's T=4T=4 targets, again at 240p240p. For Lsoft=T2KL(pT∥qT)L_{soft}=T^2\mathrm{KL}(p_T\Vert q_T), the gradient with respect to student logits is T(qT−pT)T(q_T-p_T). It equals approximately (−0.343582,0.027350,0.316232)(-0.343582,0.027350,0.316232). Gradient descent raises the dog logit and lowers the other two. Equal arrow lengths encode signs only, not gradient magnitudes; softmax couples probability changes.

Anchor with trusted labels

A trusted hard label can oppose an incorrect teacher target.
A trusted hard label can oppose an incorrect teacher target.

A new illustrative teacher target (0.2,0.6,0.2)(0.2,0.6,0.2) is wrong for a dog. At temperature one and uniform student logits, the soft objective lowers the dog logit, while the hard dog label raises it. These bars use 200p200p. A combined objective can use λT2KL(pT∥qT)+(1−λ)CE(y,q1)\lambda T^2\mathrm{KL}(p_T\Vert q_T)+(1-\lambda)\mathrm{CE}(y,q_1). The loss weight and temperature need validation. Labels are optional for pure distillation; trusted labels supply a separate corrective signal when available.

Where It Breaks

Teacher and student can agree perfectly on the wrong class.
Teacher and student can agree perfectly on the wrong class.

Both models now output (0.2,0.6,0.2)(0.2,0.6,0.2) at the same 200p200p scale, so both choose cat for a dog. Their agreement is perfect on this example and their prediction is wrong. Evaluate the student against trusted held-out outcomes and deployment constraints; teacher agreement alone does not measure task success.

The Quick Version

  • Smaller student weights can meet a deployment budget.
  • Soft targets distinguish alternative classes.
  • Temperature changes probabilities while preserving logit order.
  • The frozen teacher guides student updates.
  • Serving can use the student alone.
  • Soft-target training and ordinary inference use different temperatures.
  • Restricted student capacity can prevent an exact match.
  • Distillation gradients move logits toward teacher targets.
  • Trusted labels can oppose erroneous teacher outputs.
  • Perfect teacher agreement can still mean a wrong prediction.