TL;DR
A trained neural network can be far larger than it needs to be. Model compression makes it smaller and faster without giving up much accuracy, using three main levers: remove weights that contribute little, store the remaining numbers with fewer bits, or train a small network to imitate a large one. The goal is a model that fits on a phone, answers in milliseconds, or costs less to serve.
Where you'll see it: shipping a speech model to a phone, serving a large language model at lower cost per request, and running a vision model on a camera with a tiny chip.
Intuition
Most of a big network is redundant
Think of a large trained network as a crowd of people who learned the same lesson from slightly different angles. Many of them carry overlapping information. A few carry almost nothing. If you could find the idle ones and move the useful knowledge into a smaller team, the team would be cheaper to run and would still answer most questions correctly.
Training is expensive and wasteful in a specific way. Gradient descent needs many parameters to find a good solution, because it is easier to search in a large space. Once the solution is found, the network does not need all of that room. Compression tries to keep what the solution needs and drop the rest.
There are three ways to act on this, and they work at different levels:
- Pruning removes weights, or whole neurons and channels, that are near zero or contribute little.
- Quantization keeps every weight but stores each one with fewer bits, for example 8 bits instead of 32.
- Distillation trains a small student network to match the output of a large teacher, so the knowledge moves into a smaller shape.
These can be combined. A common pipeline prunes a model, quantizes what is left, and fine-tunes it again to recover accuracy.
A picture of soft targets
Distillation depends on a fact about how networks express what they know. A classifier does not just say "this is a cat". Its output probabilities also say how similar the cat is to a dog and to a truck. Those small probabilities hold information the hard label throws away.
The softmax temperature controls how much those small probabilities show. The plot below shows a two-class case. The horizontal axis is the difference between the two class scores (the logits). The solid curve is the ordinary softmax probability for the first class. The dashed curve is the same probability computed with temperature 4, which pushes the probabilities toward each other and makes the small differences visible.
- T = 1
- T = 4
The math
Setup
Let be the logit (raw score) for class in a classifier with classes. The softmax with temperature turns the logits into probabilities:
At this is the ordinary softmax. Larger produces softer distributions, and as grows the distribution approaches uniform. Here is the logit of class , is the number of classes, and is a positive number chosen by the person doing the distillation.
Pruning
Given a trained weight matrix , pick a sparsity target and set to zero the weights with the smallest absolute values. A simple rule keeps the top fraction of weights by magnitude:
and the pruned weights are , the element-wise product. Pruning is usually followed by fine-tuning on the training data so the remaining weights can take over the lost capacity.
Quantization
Uniform quantization to bits maps a real value in a range to one of levels spaced evenly. The step size is
and the rounding error for any value is at most . Storing a 32-bit float as an 8-bit integer cuts memory by a factor of four, before counting the small overhead of scale factors.
Distillation loss
The student is trained on two objectives at once. The first is ordinary cross-entropy against the true labels . The second matches the teacher's softened distribution and the student's softened distribution , both computed at temperature :
Here balances the two terms, and is the cross-entropy loss between a target distribution and a predicted one. The factor compensates for the fact that softer targets produce smaller gradients, so the soft term keeps a useful size as changes. This scaling follows Hinton and colleagues' 2015 paper, Distilling the Knowledge in a Neural Network.
Worked examples
A layer has one million weights, all stored as 32-bit floats. The weights lie in .
- Storage at 32 bits: bytes bytes, about 4 MB.
- Step size at 8 bits: .
- Largest rounding error: .
- Storage at 8 bits: byte bytes, about 1 MB, a factor of four smaller.
- A weight of is rounded to the nearest level. The levels near it are . Since , the nearest level is , giving , an error of about , well inside the bound.
Interpretation: 8-bit storage costs a tiny rounding error per weight, and the errors are random enough that most networks lose little accuracy. Outliers with very large values widen the range, so they make the step size coarser, which is why some schemes clip them first.
A dense layer maps 1000 inputs to 1000 outputs with a weight matrix of size , so it has parameters.
- Approximate , where is and is . This is a rank-50 approximation, the sort of thing matrix rank measures.
- Parameters after factoring: .
- The layer now has one tenth as many parameters, and each input passes through two smaller matrix multiplies instead of one large one.
- If the weight matrix is close to rank 50, the approximation error is small. If it is not, the error is large and accuracy drops.
Interpretation: the saving is only as good as the assumption that the layer's weights are nearly low-rank, and that has to be checked on the trained weights before factoring.
A teacher gives logits for three classes. Compare the soft targets at and .
- At : , , . The total is . The probabilities are , and .
- At : divide the logits by 4 to get . Then , , . The total is . The probabilities are , and .
- Both sets sum to 1. At the second class has probability 0.018, which is almost invisible. At it is 0.245, and the third class is 0.090 instead of 0.0003.
Interpretation: at the student learns only "class 1". At it also learns that class 2 is far more plausible than class 3, which is the extra knowledge distillation transfers.
Tradeoffs and failure modes
Each lever has a different cost, and the right choice depends on the bottleneck. Pruning and quantization change an existing model, while distillation trains a new one and needs a teacher.
| Situation | Problem | Better choice |
|---|---|---|
| Memory is the limit, and the hardware runs int8 | Quantization error on outliers | Per-channel scales, clipping, or quantization-aware training |
| Unstructured sparsity on a GPU | Zeros save memory but not time, because the hardware still multiplies them | Structured pruning that removes whole channels or heads |
| Accuracy must match the original | Pruning or low bit widths lose a few points | Fine-tune after compression, or keep a larger student |
| Only a trained model exists, no data | Distillation needs inputs to run the teacher on | Generate synthetic inputs, or use simpler quantization |
| Latency on a new device | The compressed model is still slow | Measure on the target device, since the best method depends on the hardware |
Some practical warnings:
- Accuracy numbers can hide damage. A model can keep its average accuracy and fail on rare classes or on distribution shifts. Evaluate on slices, not only the headline test score.
- Compression and speed are different goals. Removing weights reduces the number of operations on paper. Speed-ups appear only when the runtime exploits the sparsity or the lower precision.
- Distillation can copy the teacher's mistakes. The student learns the teacher's soft outputs, including wrong ones, so the teacher has to be good first.
- Compression can interact with training choices. Regularisation such as L2 regularisation changes how many weights end up near zero, so a model trained with it may prune more cleanly.
History
Yann LeCun, John Denker and Sara Solla proposed pruning as a principled method in 1990, in a paper called "Optimal Brain Damage". They used second-order information about the loss to decide which weights could be removed with the smallest increase in error. Their question was about generalisation and simplicity, not about deployment, but the idea of removing parameters and retraining is the root of modern pruning.
Cristian Buciluă, Rich Caruana and Alexandru Niculescu-Mizil published "Model Compression" in 2006. They trained a small model to copy the outputs of a large ensemble, which is an early form of distillation. Their setting was a large ensemble that was too slow to use in production.
Geoffrey Hinton, Oriol Vinyals and Jeff Dean wrote "Distilling the Knowledge in a Neural Network" in 2015. They introduced the temperature-softened targets described above and the scaling. The paper made distillation a standard tool and gave the name to a wide family of methods.
Song Han, Huizi Mao and William Dally combined pruning, quantization and weight sharing in "Deep Compression" in 2015, and showed that the three together could shrink large image networks by large factors with little loss in accuracy. The work helped motivate hardware and software that handle sparse and low-precision models.
The field grew because the same problem keeps coming back. Models get larger each year, and the cost of running them on real devices and at scale does not shrink on its own. Compression is the practical answer, and the three levers remain the core toolkit.
Check yourself
QuestionA layer has 2 million float32 weights. About how many megabytes does it use, and how much does it use after 8-bit quantization?Show answer
Storage at 32 bits is bytes, about 8 MB. At 8 bits it is bytes, about 2 MB, a fourfold reduction before scale-factor overhead.
QuestionFor a 500 by 500 weight matrix, you replace it with a rank-20 factorization where is and is . How many parameters does the factored form use, compared with the original?Show answer
The factored form uses parameters. The original has . The factored form uses 8% of the original, about 12.5 times fewer parameters.
QuestionWhy does raising the temperature to 4 reveal more information in the teacher's outputs than temperature 1, even though the most likely class stays the same?Show answer
Dividing the logits by 4 shrinks the gaps between them, so the exponentials become closer in value. The top class keeps the largest probability, but the small probabilities of the other classes grow from nearly zero to visible values. These small values carry information about which wrong classes resemble the right one.
QuestionA pruned model has 90% of its weights set to zero, but it runs no faster on the GPU than the dense model. Explain why, and name one change that would make the sparsity pay off.Show answer
Unstructured zeros still occupy positions in the dense matrix multiply, so the hardware performs the same operations. The speed-up needs either structured sparsity that removes whole rows, channels or heads so the matrices really get smaller, or a runtime and hardware that skip zero entries. Structured pruning is the simplest change.
Sources
- Hinton, Vinyals, Dean, Distilling the Knowledge in a Neural Network (2015): https://arxiv.org/abs/1503.02531
- Han, Mao, Dally, Deep Compression (2015): https://arxiv.org/abs/1510.00149
- Knowledge distillation (Wikipedia): https://en.wikipedia.org/wiki/Knowledge_distillation