About

What Is Knowledge Distillation? Teaching a Small Model with a Big Model's Probabilities

It helps most when labels are scarce, and temperature should match how confident the teacher is

Short version: knowledge distillation trains a small model (the student) to follow the probability distribution a big model (the teacher) outputs. There's more to learn from that than from a single correct answer, so it matters most when you're short on labels. After reading this you'll know why distillation works, how to set the temperature, and whether distillation, quantization or fine-tuning fits your job.

In one line

Normal training only gives the answer: "this image is a 3" (a hard label). Distillation also gives the teacher's distribution (a soft label), like "98.3% a 3, 1.2% a 2, 0.25% an 8." Those small wrong-answer probabilities carry information: a 3 looks a bit like a 2 and an 8. Hinton and colleagues called this dark knowledge in their 2015 paper.

Temperature (T) decides how much to spread that distribution out. Divide the model's raw outputs (logits) by T before turning them into probabilities, and the bigger T is, the more the small probabilities grow into view. The 98.3% above is with T=4. At plain T=1, it was 100% for 3 and zero for everything else, to four decimal places.

I tried it — on handwritten digits

I used the 1,797 8×8 handwritten digits that ship with scikit-learn, on torch 2.8.0. 1,257 for training, 540 for testing. The teacher is an MLP with 301,066 weights (97.2% test accuracy). The student has 1,210. That's 249 times smaller. I compared a student trained on hard labels alone with one trained on 90% teacher distribution (T=4), averaged over 5 seeds.

labeled images   hard only   distill (T=4)
50               80.1%       85.3%
100              87.5%       90.8%
1,257 (all)      96.8%       97.1%

With only 50 labels there's a 5-point gap. With all of them it's 0.3 points, basically the same. I went in assuming distillation always helps. At this size, with enough data, hard labels teach almost everything. Distillation earns its keep when labels are scarce.

What if the student never sees a 3?

I ran a small version of an experiment from the paper. I removed every 3 from the student's training data. The hard-label student got none of the 55 test 3s right. It had never seen one, so of course.

Distillation is different. Even on images that aren't 3s, the teacher attaches "a little bit 3." Learn from that, and the student learns what a 3 is without seeing a single one. Except at T=4 it only got 3%. I was stuck here for a while. Raising T changed things.

trained without 3s   3s correct   overall
hard only            0%           87.2%
distill T=4          3%           87.8%
distill T=8          41%          91.5%
distill T=20         72%          93.6%

A student that never saw a 3Accuracy on the 55 test 3s (sklearn digits, 1,210-weight student, mean of 3 seeds)hard labels only0%distill, T = 43%distill, T = 841%distill, T = 2072%100%The teacher was so confident that T = 4 still hid the small probabilities. Raising T made them visible.

This teacher was so confident that even at T=4 the wrong-answer probabilities were nearly zero. The hidden information wasn't visible, so the student couldn't learn it. Temperature isn't a fixed "usually 2 to 4" value. You pick it to match how confident your teacher is. The quick way is to print a few teacher distributions and raise T until non-zero numbers show up on the wrong answers. (Mean of 3 seeds.)

Two ways it's used with LLMs

Learn the distribution directly: like the experiment above, the student follows the teacher's next-token probabilities. That's how DistilBERT came out of BERT (the paper: 40% smaller, keeps 97% of language understanding, 60% faster). The Gemma 2 technical report abstract also says its 2B and 9B models were trained with distillation instead of next-token prediction. You need the teacher's logits, so only whoever has the teacher's weights can do this.

Learn the text the teacher writes: have a big model write answers and fine-tune a small model on them. You only get text, no probabilities, so it works through an API. Orca (13B) learning from GPT-4's step-by-step explanations is this kind.

This post used to say "Phi-3 was made by distilling GPT-4." The Phi-3 technical report abstract only describes its data as "heavily filtered publicly available web data and synthetic data." It says nothing about distilling from GPT-4, so I took it out. "Gemini → Gemma" is gone too, because the Gemma 2 abstract doesn't name the teacher.

If you train on API outputs, read the terms first. Anthropic's commercial terms (D.4) forbid using the services to train competing AI models. Check other providers' terms for a similar clause before you start.

Distillation, quantization, fine-tuning — when to use which

Make the current model lighter as-is: quantization. No training. Try this first.

Make the model itself much smaller: distillation. You have to collect teacher outputs and run training, so it takes work. It fits when the task is narrow, like classification, and the big model's API cost or latency is the problem.

Teach a small model your job's format: fine-tuning or LoRA. If people write the answers, it's this. If a big model writes them, it's effectively distillation.

If a classification or extraction job running on a big model's API is too slow or too expensive and you want to see whether a small model can take over, you can ask negabaro. We can start by measuring how far a student gets on the logs you already have.

Key Concepts

1

Train the teacher (big model) first, or pick an existing big model

2

Divide the teacher's logits by T and turn them into probabilities — raise T until non-zero numbers show up on wrong answers (72% on unseen 3s at T=20 here)

3

Student loss = KL divergence to the teacher distribution (× T²) mixed with cross-entropy on the hard labels (9:1 here)

4

Compare side by side with a hard-label-only student to see whether distillation actually wins (50 labels: 80.1% → 85.3%, all: 96.8% → 97.1%)

5

If you train on API outputs, check the provider's terms for a ban on training competing models first

Pros

  • ✓ Beats hard-label training when labels are scarce (50 labels: +5.2 points)
  • ✓ The student partly learns classes it never saw (72% on 3s without any 3s)
  • ✓ The model itself gets smaller, cutting inference cost and latency (a 249× smaller student here)

Cons

  • ✗ Hard to beat the teacher, and the gain is small with plenty of data (+0.3 points with all labels)
  • ✗ Temperature has to match the teacher (T=4 on an overconfident teacher gave 3%)
  • ✗ Training on API outputs can break the terms (Anthropic commercial terms D.4)

Use Cases

BERT → DistilBERT (40% smaller, keeps 97% of performance) Gemma 2 2B and 9B — trained with distillation instead of next-token prediction Orca 13B — learned from GPT-4's step-by-step explanations Moving a narrow classification or extraction job off a big model's API onto a small model
← What Is Quantization? Shrinking Models with Lower-Precision Weights What Is Mixture of Experts (MoE)? Big Models, Small Compute →