Skip to content

Knowledge distillation: a small student copies a teacher

Train a small network to reproduce a larger model's softened predictions. See what the temperature and the T² factor do, distill any CodeSplash model into a network, and compare student and teacher on the same rows.

Applied ~13 min9 steps

What runs in CodeSplash

You will be able to

  • Write the distillation loss and explain the temperature and the T² factor
  • Explain what soft targets carry that hard labels don't
  • Distill a CodeSplash model into a network and read the student against the teacher
  • Name what distillation does not fix

Read first

01

Why copy a model?

Read

Your best model may be too slow or too large to serve: a big boosted ensemble, a deep network, an ensemble of networks. Knowledge distillationKnowledge distillationTraining a new model (the student) to copy another model's predictions, usually so a small model can stand in for a big one. trains a smaller student to reproduce the teacher's predictions. The student learns from the teacher's whole probability vector, not only from the right answer.

A teacher that says "70% churn, 20% downgrade, 10% stay" tells the student the case was close, and which wrong answer was nearly right. Those soft targetsSoft targetsTraining targets that are probabilities instead of one class, such as a teacher model's predicted probabilities. carry far more information per row than a hard label.

02

The loss

Read

Divide both models' logits by a temperatureDistillation temperatureA number above 1 that flattens a teacher's probabilities before a student copies them, so small ones carry more signal. T>1T > 1 before the softmax, which flattens the probabilities so small ones count. Then blend the usual loss on the labels with the KL divergence to the softened teacher:

L=α Lhard+(1−α) T2 KL(softmax(zt/T) ∥ softmax(zs/T)).L = \alpha\, L_{\text{hard}} + (1 - \alpha)\, T^2\, \mathrm{KL}\big(\mathrm{softmax}(z_t / T) \,\Vert\, \mathrm{softmax}(z_s / T)\big).

The T2T^2 factor keeps the soft term's gradients from shrinking as TT grows. In the reference's example, a teacher and student already softened at T=2T = 2 give a distillation term of 0.0909.

DExplorable · Distillation: soft targets at a temperature

What does a student learn from a teacher's probabilities that it can't learn from the label alone?

1.0

Dividing logits by T before the softmax spreads probability onto the wrong classes.

0.50

Loss = α·cross-entropy with the label + (1 − α)·T²·KL to the soft targets.

200

Gradient steps on the student's logits for this one example.

1.0

Step size on the student's logits.

cat0.898dog0.064fox0.031car0.003truck0.003
  • Teacher's soft targets at T
  • Student after training (T = 1)
Soft-target entropy (bits)
0.932
Student: P(dog) / P(car)
18.60
KL at T after training
0.0489
Soft-target gradient with T²
0.6785
Soft-target gradient without T²
0.6785
  • Puzzle: At T = 4 or higher, make the student match the soft targets to within 0.001 nats of KL.
  • Puzzle: Find a temperature where the soft targets carry more than 1.5 bits.
At T = 1.0 the teacher's soft targets carry 0.93 bits. After 200 steps the student thinks “dog” is 18.6 times as likely as “car” (the teacher: 66.7 times). The student ranks the wrong classes as the teacher does: that ranking is what distillation passes on.

At T = 1.0 the teacher's soft targets carry 0.93 bits. After 200 steps the student thinks “dog” is 18.6 times as likely as “car” (the teacher: 66.7 times). The student ranks the wrong classes as the teacher does: that ranking is what distillation passes on.

03

Distill in CodeSplash

In the app

Open a finished model run (any learner, a boosted ensemble included) and choose Make a model from this → Distill into a network. CodeSplash trains a neural-network student on the teacher's predictions for the training rows, with your Temperature and Label weight (1 trains on the true labels only, 0 copies the teacher only), and makes an ordinary run: folds, a leaderboard score and a final model.

The run's Lineage tab names the teacher. Erasing the teacher's training data everywhere also erases the student, because the student learned from the teacher's predictions on those rows.

04

Read the student against the teacher

Read

Compare student and teacher on the same rows: the same folds and, at the end, the same test rows. Look at three things:

  • the score gap: if it's small next to the fold spread, the student may be good enough;
  • calibration: a student trained on softened targets can be less sharp than the teacher, or sharper if the blend leans on hard labels;
  • subgroups: the student copies the teacher where the transfer rows were dense and guesses elsewhere.

Then weigh the gap against what you gain in speed and size.

05

Predict: a wrong teacher

Predict

Predict first

The teacher is systematically wrong on one customer segment. You distill with most of the weight on the soft targets. What happens on that segment?

The student fixes them, since it also sees the labelsThe student tends to copy them
Reveal

The student tends to copy the errors. Soft targets transfer the teacher's mistakes along with its useful similarities. More weight on the hard labels, or more labeled rows from that segment, is the remedy.

06

What runs here

Read
07

Common mistakes

Read
08

Check: distillation

Checkpoint

Check yourself

What does raising the distillation temperature do to the teacher's probabilities?

Show the answer

Answer: a

Dividing logits by T > 1 shrinks their differences, so the softmax spreads mass over more classes.

Check yourself

Why is the soft term multiplied by T²?

Show the answer

Answer: a

Without it, raising T would quietly turn the soft targets down relative to the hard labels.

Check yourself

Which models can be a teacher for Distill into a network?

Show the answer

Answer: a

Distillation needs the teacher's predicted probabilities or values, not its weights. Adapt and Compress, by contrast, need a network's own weights.

Check yourself

How should you judge a distilled student?

Show the answer

Answer: b

A fair comparison uses the same rows. A small loss against soft targets only says the student copied the teacher, errors included.

09

Recap

Recap
  • A student learns from a teacher's softened probabilities, blended with the true labels.
  • Temperature flattens the soft targets; the T² factor keeps their weight steady.
  • In CodeSplash, Distill into a network works from any learner and records the teacher in Lineage.
  • Distillation copies the teacher's errors too; compare on the same rows, calibration and subgroups included.

Concepts in this lesson