WHY do soft targets carry more information?
Consider an image classifier over {cat, dog, car}. The hard label for a photo of a cat is [1,0,0]. But the teacher might output [0.9,0.099,0.001]. That tells the student something the hard label cannot: "a cat looks much more like a dog than like a car." This inter-class similarity structure is called dark knowledge.
(1) Match the teacher's soft targets. Use cross-entropy (equivalently KL divergence) between teacher soft probs pT(T) and student soft probs qT(T), both at temperature T:
Lsoft=−∑ipiT(T)logqiT(T)
(2) Still get the true answer right. Standard cross-entropy with hard labels y at T=1:
Lhard=−∑iyilogqi(1)
Combine them with a mixing weight α:
WHY the T2 factor? (This is the step everyone forgets.)
When you backprop through softmax-with-temperature, the gradient of Lsoft w.r.t. student logits scales like 1/T2 (one 1/T from the student softmax, one from the chain rule through the logit division). To keep the magnitude of the soft-target gradient comparable to the hard-target gradient, we multiply by T2. Without it, raising T silently shrinks the soft loss into irrelevance.
Gradient sketch (WHY 1/T2):
For softmax cross-entropy, ∂L/∂zi=qi−pi. With temperature, the student prob is qi(T) and each logit is scaled by 1/T, so
∂zi∂Lsoft=T1(qi(T)−pi(T)).
For small logits (Taylor expand ez/T≈1+z/T), qi(T)−pi(T)≈KT1(zi−vi) where vi are teacher logits, giving an overall ∼1/T2. Multiplying the loss by T2 cancels it. ✅
At T=1:e4=54.6,e1=2.72,e−2=0.135, sum =57.5.
p=[0.950,0.047,0.0023].
Why this step? Confidence is crushed onto "cat"; dog/car info is nearly invisible.
At T=2: logits become [2,0.5,−1].
e2=7.39,e0.5=1.65,e−1=0.368, sum =9.40.
p=[0.786,0.175,0.039].
Why this step? Now "dog" is a clearly non-trivial 17.5% — the student can learn that cats resemble dogs. That's the dark knowledge surfacing.
Total:L=0.5⋅22⋅0.640+0.5⋅0.223=1.280+0.112=1.392.
Why this step? The T2=4 scaling makes the soft term dominate here — exactly its purpose: force the student to absorb the teacher's structure while the hard label keeps it honest.
Feature-based KD: match intermediate hidden states/attention maps (student learns how the teacher thinks, not just what it outputs).
Sequence-level KD: teacher generates text, student trained on that generated text as data (used heavily for instruction-following distillation, e.g. Alpaca-style).
Recall Feynman: explain to a 12-year-old
Imagine a genius chef (teacher) and a student cook. If the chef only says "this dish is 'pizza'," the student learns a little. But if the chef says "it's mostly pizza, kind of like a calzone, and nothing like soup," the student learns way more — including how foods relate to each other. Knowledge distillation is teaching the small model using the big model's full opinion, not just its final answer. The "temperature" is like asking the chef to slow down and share the subtle in-between thoughts instead of blurting the answer.
Dekho, knowledge distillation ka funda simple hai: ek bada powerful "teacher" model hai jo already achha train ho chuka hai, aur ek chhota "student" model banana hai jo fast aur lightweight ho. Ab agar hum student ko sirf hard label (jaise "yeh cat hai", ek-hot vector) se sikhayein, toh woh utni hi info paata hai. Lekin teacher jab predict karta hai toh woh poori probability distribution deta hai — jaise "90% cat, 9% dog, 0.1% car". Yeh distribution batata hai ki cat dog jaisi lagti hai par car jaisi bilkul nahi. Isko hum "dark knowledge" kehte hain, aur yehi cheez student ko extra smart banati hai.
Problem yeh hai ki achha teacher over-confident hota hai — woh 0.9999 type numbers deta hai, toh useful similarity info chhup jaati hai. Isliye hum temperature T use karte hain. Logits ko T se divide karke softmax lete hain, jisse distribution flat ho jaati hai aur woh chhoti-chhoti values (dog wali) clearly visible ho jaati hain. T bada karo toh distribution uniform ki taraf jaati hai, chhota karo toh sharp (one-hot) ban jaati hai.
Loss mein do cheezein combine hoti hain: ek soft loss (teacher ki soft probs match karo) aur ek hard loss (sahi answer bhi sahi rakho), aur inko α se mix karte hain. Ek important detail — soft loss ko T2 se multiply karna padta hai, kyunki temperature ki wajah se gradient 1/T2 chhota ho jaata hai, aur T2 usko wapas balance kar deta hai. Yeh factor log bhool jaate hain aur phir distillation kaam hi nahi karta.
LLMs ke liye yeh technique bahut powerful hai kyunki har token pe poore vocabulary (50k words) ki distribution milti hai — matlab bahut rich signal. Isi se DistilBERT, TinyLlama jaise chhote models bante hain jo bade models ki competence sasti mein inherit kar lete hain. Yaad rakho: Soft, Warm, Squared.