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.
What runs in CodeSplash
- TRN015Knowledge distillation Runs in CodeSplash
- Deep neural network (torch MLP) Verified in Python and R
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
Why copy a model?
ReadYour 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.
The loss
ReadDivide 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. 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:
The factor keeps the soft term's gradients from shrinking as grows. In the reference's example, a teacher and student already softened at 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?
Dividing logits by T before the softmax spreads probability onto the wrong classes.
Loss = α·cross-entropy with the label + (1 − α)·T²·KL to the soft targets.
Gradient steps on the student's logits for this one example.
Step size on the student's logits.
- 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.
Distill in CodeSplash
In the appOpen 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.
Read the student against the teacher
ReadCompare 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.
Predict: a wrong teacher
PredictPredict 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?
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.
What runs here
ReadCommon mistakes
ReadCheck: distillation
CheckpointCheck 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.
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