Knowledge Distillation: How Smaller Models Learn from Larger Models
From the publisher’s feed, collected in the ZeroLabs newsroom.
A 100-million-parameter model and a 1-billion-parameter model may solve the same task, but deploying the larger one is not always practical. Memory budgets, latency constraints, energy consumption, and edge-device limitations mean that raw accuracy is only part of the story. A model must also fit within the available budget of time, money, and computational resources.This raises a natural question: can we transfer what a large model has learned into a much smaller one?Knowledge distillation (KD) is one of the most effective answers to that question. Rather than training a compact model from scratch on hard labels, we train it to mimic the behavior of a larger, already-capable “teacher” model. The result is a “student” model that retains much of the teacher’s performance while running faster, consuming less memory, and deploying on hardware the teacher could never fit on.Knowledge Distillation — The ConceptKnowledge Distillation (KD) is a technique used to compress the models. KD is most often applied to large deep learning neural networks with many layers and learnable model parameters. A compact “student” model is trained to mimic a larger, more complex “teacher” model by learning from its soft probability outputs rather than hard labels alone. For LLMs, KD has emerge as an effective means of transferring advanced capabilities from leading proprietary models to smaller, more accessible open source models. The teacher provides a much richer training signal than a one-hot label, allowing the student to learn not only the correct class but also relationships among alternative outputs. This technique helps in deploying deep models on edge devices ike mobiles or IoT.Smaller models are usually faster, and less computationally demanding. However, they might also lack the accuracy, refinement and knowledge capacity of a large model with far more parameters. In the seminal 2015 paper, “Distilling the Knowledge in a Neural Network,” Hinton et al proposed to circumvent these limitations by dividing training into two distinct stages with distinct purposes. The authors presented an analogy: whereas many insects have a larval form optimized for extracting energy and nutrients from the environment and a totally different adult form optimized for traveling and reproduction, conventional deep learning uses the same models for both the training and deployment stages, despite their different requirements. Smaller models are fundamentally more explainable: in a model with hundreds of billions of parameters, it’s difficult to interpret the contributions of different parts of the neural network. Transferring representations learned by large “black-box” models to simpler models can help elucidate transformative insights in fields like medical diagnostics and molecular discovery.Knowledge distillation transfers knowledge from a larger frozen teacher to a smaller trainable student.The key features of KD involves model compression, performance retention, faster inference, soft target learning, teacher-student framework, and regularization effect.Let’s understand the distinction betweem the distillation processes:Response distillation: transfers output behaviorLogit distillation: transfers output distributionsFeature distillation: transfers intermediate representationsAttention distillation: transfers attention patternsReasoning-trace distillation: transfers externally generated reasoning demonstrationsThe Core Idea: Learning from Soft TargetsIn standard supervised learning, a classification model is trained to match one-hot labels: the correct class gets probability 1.0, and every other class gets 0.0. These are called hard targets. They provide strong supervision, but they are also informationally sparse. A one-hot label tells the model what the answer is, but nothing about how the answer relates to the alternatives.Hard labels identify the correct class; soft targets additionally encode the teacher’s relative confidence across classes.Now consider what happens when a well-trained teacher model looks at an image and sees a cat. Its raw outputs before the final softmax (the logits) might look something like this:Cat: 5.0Dog: 1.0Horse: 1.0Fox: 0.5After applying softmax at temperature T = 1, the teacher outputs:Cat: 0.98Dog: 0.01Horse: 0.01Fox: 0.00The teacher is nearly certain the image is a cat. But look closer: the relative probabilities assigned to the non-cat classes are not arbitrary. The teacher is expressing that this image resembles a dog more than a horse, and a horse more than a fox. These soft targets (the full probability distribution over all classes) encode relationships that a one-hot label discards entirely.Geoffrey Hinton and colleagues, in their seminal 2015 paper “Distilling the Knowledge in a Neural Network,” called the information carried by these small probabilities dark knowledge. Even when the teacher is highly confident, the residual probability mass reflects the geometric structure of the learned feature space. Training the student to match this entire distribution forces it to internalize the same similarity structure, not just the final decision boundary.This is why soft targets are so powerful. The teacher is not merely asserting “this is a cat.” It is also saying: “if I am wrong, I am more likely to be wrong about a dog than a horse.” That inter-class relationship information is completely absent from a hard label.Temperature: Revealing the Teacher’s Internal StructureThe mechanism that makes soft targets useful is temperature scaling inside the softmax. The standard softmax is:https://medium.com/media/ec63624cde0b74cef21f4be471bc8eab/hrefIn distillation, we introduce a temperature parameter T > 0:https://medium.com/media/0bfd64df5ae35608a6a01861f501ba7b/hrefWhen T = 1, this is the normal softmax. When T > 1, the logits are divided by a larger number, shrinking the differences between them. The resulting distribution becomes softer (more uniform) and the small differences between non-maximal logits become more visible.Returning to our example, at T = 5 the same logits produce:Cat: 0.55Dog: 0.22Horse: 0.15Fox: 0.08Now the teacher’s belief that the image looks more like a dog than a horse is clearly expressed in the probabilities. Higher temperatures amplify the dark knowledge. During distillation, the student is trained to match these softened distributions (usually at an elevated T), while at inference time both teacher and student revert to T = 1 for sharp predictions.Increasing temperature softens the teacher’s probability distribution, making relative probabilities among non-dominant classes more visible.Temperature is the knob that controls how much of the teacher’s internal similarity structure is revealed to the student.The Mathematics of DistillationWith the intuition in place, the training objective is straightforward. The student is trained with a combined loss that balances two sources of supervision.Standard Cross-Entropy LossThe hard-label term keeps the student grounded in the ground truth. The first component of the loss uses the ground-truth label. Lethttps://medium.com/media/19392a556b7de1d2ee86df2a195553de/hrefbe the one-hot ground-truth vector and let (p_i^S) denote the student’s prediction at (T=1).The standard cross-entropy loss ishttps://medium.com/media/36cd86441dbdc691b002fdc0526f4277/hrefBecause (y) is one-hot, only the probability assigned to the correct class contributes to the loss.This term ensures that the student remains grounded in the actual training labels rather than simply reproducing the teacher’s behavior.Distillation LossThe soft-label term matches the student’s softened outputs to the teacher’s softened outputs. Let pᵀ and pˢ be the teacher’s and student’s probability distributions, both computed at the same elevated temperature T > 1. The distillation loss is the Kullback-Leibler (KL) divergence between these distributions:https://medium.com/media/0ea7673197cbd7c20037e6598333ebc5/hrefMinimizing this forces the student’s softened output to become as close as possible to the teacher’s softened output, including the dark knowledge in the smaller probabilities.Standard Softmax FunctionLet the teacher or student produce a vector of logits:https://medium.com/media/f89509c8fff47d42bf297bee7785b1fd/hrefwhere C is the number of classes. Under standard softmax, the probability assigned to class (i) ishttps://medium.com/media/56c6f0e74d0f5489326bd019823cdf5c/hrefThe class with the largest logit receives the highest probability.Temperature-Scaled SoftmaxKnowledge distillation introduces a temperature (T>0) to soften the probability distribution:https://medium.com/media/803fbe5c90352958e70907e63e3d4998/hrefWhen (T=1), this reduces to the standard softmax. Increasing (T) reduces the differences between probabilities, producing a softer distribution.For (T1), it becomes softer. During distillation, the teacher and student are typically evaluated at the same elevated temperature (T>1).Combined LossIn practice, we keep a small amount of hard-label supervision while matching the teacher. The classic combined objective is:https://medium.com/media/b3f036aee0e5049092e7eab7165131bc/hrefWhere:α ∈ [0, 1] balances the two terms (typical values range from 0.1 to 0.5)pˢ in the CE term is computed at T = 1pᵀ and pˢ in the KL term are both computed at elevated temperature TThe factor T² compensates for the fact that the gradients of the soft cross-entropy scale as 1/T². Without this multiplier, the distillation term would vanish as temperature increases.Knowledge distillation combines hard-label supervision with teacher-generated soft-target supervision.As T increases, the softened distributions become smoother and the gradients become smaller. Multiplying the distillation loss by T² keeps its contribution to optimization at a comparable scale. The temperature T and the mixing weight α are the two main hyperparameters of knowledge distillation. Together, they control how strongly the student is pulled toward the teacher’s soft distribution versus the true hard labels.Why the (T²) Factor MattersIncreasing the temperature makes the teacher and student distributions softer. However, this also reduces the magnitude of the gradients produced by the distillation loss. In the high-temperature regime, the gradient contribution scales approximately ashttps://medium.com/media/6b53f503311840d1949906b17d9f9c82/hrefTherefore, multiplying the distillation loss by (T²) approximately compensates for this scaling:https://medium.com/media/cffa464c6cb629085b11bc23d5cede0a/hrefWithout this correction, increasing (T) would make the distillation gradients progressively smaller, reducing the influence of the teacher’s soft targets during optimization.Thus, (T) and (α) play different but complementary roles:T: controls the softness and information content of the teacher contribution.α: controls the balance between teacher supervision and hard-label supervision.Together, these two hyperparameters determine how strongly the student learns from the teacher and how much it remains anchored to the original ground-truth labels.The Distillation PipelineThe practical pipeline follows a clear sequence:Train the teacher. A large, high-capacity model (e.g., ResNet-50, BERT-base, GPT-scale LLM) is trained to convergence on the task dataset using standard cross-entropy.Freeze the teacher. The teacher’s weights are fixed. It acts as a static source of soft targets.Generate soft targets. Training data is passed through the teacher to obtain softened logits at temperature T.Train the student. The smaller student model learns from both the teacher’s soft targets (via KL divergence) and the ground-truth hard labels (via cross-entropy). Only the student’s parameters are updated.Deploy the student. The teacher can be discarded. The student runs inference at T = 1.The standard offline knowledge-distillation workflow.Because soft targets contain richer and more stable information than hard labels alone, the student can often be trained with fewer examples and a higher learning rate than the original model would require from scratch.What Knowledge Can Be Distilled?Not all knowledge lives in the final output layer. Distillation methods vary by what they transfer from teacher to student.Different distillation methods transfer different forms of information from teacher to student.Response-based knowledge is the classic approach: the student matches the teacher’s final logits or probability distribution. This is simple, requires no access to the teacher’s internal layers, and works well for classification. The limitation is that when the teacher’s soft targets have very low entropy (when it is extremely confident) there is little information in the non-maximal probabilities to transfer. This is why response-based methods rely heavily on temperature scaling to increase entropy.Feature-based knowledge goes deeper. The student is trained to reproduce intermediate activations from the teacher’s hidden layers; the same representations the teacher uses for feature extraction. This requires access to the teacher’s internals and often involves a linear projection to align dimensions when the student has a different architecture. Feature-based distillation is common in vision models (CNNs) and NLP Transformers, where matching hidden states or attention maps transfers structural understanding that output-level distillation alone might miss.Relation-based knowledge moves one step further. Instead of matching individual layer outputs, the student learns the relationships between representations, correlations between feature maps, pairwise similarities between samples, or distance structures in the embedding space. This captures the teacher’s understanding of how different inputs relate to each other, which is especially useful in metric learning and representation learning tasks.An example of feature- and logit-level knowledge distillation, where the student learns from both the teacher’s intermediate representations and output-level predictions.Distillation SchemesThe field has evolved beyond the simple “frozen teacher, trainable student” setup.Offline distillation is the standard approach: the teacher is pre-trained and frozen, and the student learns from its static outputs. This is the dominant paradigm for LLM distillation, where the teacher is often a large proprietary model whose weights cannot be modified.Online distillation trains both teacher and student simultaneously. This is useful when a suitable pre-trained teacher is not available, or when the domain is dynamic. In online distillation, the teacher continues learning while distilling its evolving knowledge into the student in real time. Online distillation performs knowledge transfer while the teacher signal is being learned or updated during training, rather than relying entirely on a separately pretrained, frozen teacher.Self-distillation uses a single network as both teacher and student. Deeper layers of the network supervise shallower layers through auxiliary classifiers attached at intermediate depths. During training, the deeper classifiers guide the shallower ones via KL divergence on outputs and L2 regularization on feature maps. After training, the auxiliary classifiers are dropped, leaving a model that was effectively larger during training but compact at deployment. The analogy is apt: many insects have a larval form optimized for extracting energy and an adult form optimized for travel and reproduction. Conventional deep learning uses the same architecture for both training and deployment despite their different requirements. Self-distillation is one way to decouple them. Self-distillation refers to knowledge transfer where the teacher and student originate from the same model or model family, rather than using a separate larger teacher.From CNNs to Large Language ModelsThe principles of distillation extend naturally across architectures, though the specifics vary.In computer vision, a typical setup distills a ResNet-50 teacher (≈25M parameters, 76.1% ImageNet top-1 accuracy) into a ResNet-18 student (≈11.7M parameters). Trained from scratch, ResNet-18 achieves around 69.8% top-1 accuracy. With distillation, it typically reaches 72.5–73.5% — recovering a meaningful fraction of the teacher’s performance while running roughly 2.2× faster. The student is not copying parameters; it is learning the decision boundaries and feature hierarchies that the teacher discovered.In Transformers, distillation often involves matching more than just final logits. A 12-layer Transformer teacher might supervise a 6-layer student through hidden-state alignment and attention-map distillation. Because the student has fewer layers, a mapping is defined; for example, letting every student layer imitate selected teacher layers. The student learns not just what the teacher predicts, but how it attends to tokens and structures information internally.For large language models, distillation has become one of the most important techniques for producing smaller, faster models that retain a surprising fraction of a frontier model’s capability. The teacher (a model with tens or hundreds of billions of parameters) can supervise the student in several ways:Logit distillation: At every token position, the teacher provides a full distribution over the vocabulary. The student learns to match this distribution.Response distillation: The teacher generates complete responses that serve as high-quality training targets for instruction tuning.Feature distillation: Intermediate hidden states or attention patterns are matched, preserving deeper linguistic structure.Teacher-generated reasoning traces: The teacher can generate step-by-step explanations or intermediate solution traces that are used as additional supervision during student training.Synthetic data generation: The teacher generates large volumes of instruction-response pairs, explanations, or preference data that are used to fine-tune the student.The teacher–student principle remains consistent, while the distillation signal changes across architectures.In modern LLM pipelines, these methods are frequently combined. A frontier model is first instruction-tuned or preference-tuned, then used to generate synthetic data and soft labels that supervise a much smaller student. The result is a compact model that is both task-adapted and knowledge-rich.When does knowledge distillation fail?The limitations of knowledge distillation are as follows:A weak teacher cannot magically create better knowledge.Teacher errors can be transferred to the student.Student capacity limits how much knowledge can be absorbed.Teacher and student architecture mismatch can make feature distillation difficult.Temperature selection is task-dependent.Teacher inference can be expensive during training.Distillation does not guarantee identical behavior or accuracy.For LLMs, generated responses can contain hallucinations or undesirable behaviors that the student may inherit.Distillation, Fine-Tuning, and Other Compression TechniquesDistillation is often confused with fine-tuning, but they serve different purposes. Fine-tuning adapts a pre-trained model to a new task or domain; the architecture usually stays the same size, and the goal is specialization. Distillation explicitly uses a stronger teacher to supervise a smaller student; the goal is knowledge transfer and compression. In practice, the two are often combined: a large model is fine-tuned on a high-quality dataset, then distilled into a smaller student.Distillation is also just one tool in the model compression toolbox. Quantization reduces numerical precision (e.g., FP32 → INT8). Pruning removes unnecessary weights or channels. Low-rank factorization approximates large matrices as products of smaller ones. Weight sharing forces parameter groups to reuse values. These techniques operate on the parameters of an already-trained model. Distillation operates on the behavior of a teacher, producing an entirely new weight set for a different architecture. Because they act on different axes, they are highly complementary. A common deployment pipeline is: distill into a smaller architecture, prune redundant weights, then quantize to low precision.Knowledge distillation complements rather than replaces other model compression techniques.Why This MattersKnowledge distillation is not merely a compression trick. It is a way of asking: what does a large model actually know, and how much of that knowledge is structural rather than parametric? The fact that a 10× smaller model can recover a large fraction of a teacher’s accuracy suggests that much of the teacher’s capability lies in the patterns and relationships it has learned, not in the sheer count of its parameters.For practitioners, this means that deploying capable AI on edge devices, in low-latency APIs, or under tight budget constraints is not a matter of accepting dramatically worse performance. It is a matter of transferring the right knowledge ( the soft targets, the feature hierarchies, the relational structure) from a model that can afford to be large to a model that can afford to be deployed.The teacher does not need to give the student every weight it has ever learned. It only needs to teach the patterns, the reasoning shortcuts, and the important distinctions. That, in essence, is what knowledge distillation does.References1. Hinton, G., Vinyals, O., & Dean, J. (2015). Distilling the Knowledge in a Neural Network. arXiv:1503.02531. The foundational paper that introduced temperature-scaled softmax and the teacher–student distillation framework.2. Cioppa, A., et al. Online distillation for semantic segmentation in live sporting events. Cited in the notes as an example of online distillation where a large model trains on live match data while simultaneously distilling into a smaller real-time model.3. Microsoft Research. (2023). Orca: Progressive Learning from Complex Explanation Traces of GPT-4. An example of using a large proprietary LLM to generate reasoning traces and synthetic training data for instruction-tuning a smaller student model.4. GeeksforGeeks. Knowledge Distillation in Machine Learning. https://www.geeksforgeeks.org/machine-learning/knowledge-distillation/5. IBM Think. Knowledge Distillation. https://www.ibm.com/think/topics/knowledge-distillationKnowledge Distillation: How Smaller Models Learn from Larger Models was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.