Search

Search papers and lessons

Pruning

Level 3 · 10 min read

Pruning removes the weights of a trained network that contribute least, sets them to zero, and then fine-tunes what is left, so the model gets smaller and sometimes faster with little loss in accuracy.

On this page

  • Colour = topic mix
  • Linear algebra
  • Neural network basics
  • Efficiency
  • Discrete math
  • Sign in to see your progress

TL;DR

A trained network usually has far more weights than it needs. Pruning finds the weights that matter least, sets them to exactly zero, and then trains the remaining weights for a little longer so they can make up the lost ability. Done well, the network keeps almost all of its accuracy with a large share of its weights gone.

The common recipe is simple: train, remove, fine-tune, and repeat if you want more removal. Most of the work is in deciding which weights to remove.

Where you'll see it: shrinking language models and image classifiers so they fit on phones, and cutting the cost of inference on servers where the model runs millions of times a day.

Intuition

A garden with too many plants

Picture a garden planted so densely that every plant gets a little sunlight and none of them thrive. If you pull out the weakest third, the rest grow into the space. A large neural network is similar. Many connections were useful during training, but many others carry almost no signal once the network has settled. Pruning is the gardener's step: find the weak connections, remove them, and let the strong ones adjust.

The reason this works at all is that training is noisy and over-parameterised. A network with many more weights than it strictly needs can fit the training data in many different ways, and most of those ways contain a lot of redundancy. Pruning exploits that redundancy. It does not try to discover a new, smaller model from scratch. It starts from a big model that already learned something and trims it, then runs a short round of Fine-Tuning so the remaining weights can compensate.

Why small weights are a rough guide

The simplest rule is to call a weight unimportant when its absolute value is small. A weight near zero multiplies its input by almost nothing, so removing it changes the output a little. The picture below shows why this is a useful but imperfect heuristic. If the weights of one layer are roughly bell-shaped around zero, a threshold on ∣w∣|w| removes the middle of the bell and keeps the tails.

−1−0.500.5100.511.5weight value wdensity
  • weight density, σ = 0.3
If weights are bell-shaped around zero, removing all weights with |w| below 0.3 deletes the central peak and roughly 68% of the weights.

The plot shows a bell centred at zero, which is typical for trained weights. The markers at ±0.3\pm 0.3 mark a threshold. Everything between them is removed. Those are the numbers a magnitude threshold would cut.

flowchart LR
  A["Train dense network"] --> B["Score each weight (for example by magnitude)"]
  B --> C["Set lowest scoring weights to zero"]
  C --> D["Fine-tune remaining weights"]
  D --> E{"Enough sparsity?"}
  E -- "no, prune more" --> B
  E -- "yes" --> F["Ship sparse model"]

The math

Pruning as a constrained problem

Let w\mathbf{w} be the vector of all nn weights in a trained network, and let L(w)L(\mathbf{w}) be the training loss. Pruning asks for a mask m∈{0,1}n\mathbf{m} \in \{0, 1\}^n that keeps at most kk weights, and then minimises the loss over the kept weights:

min⁡w,m  L(m⊙w)subject to∥m∥0≤k.\min_{\mathbf{w}, \mathbf{m}} \; L(\mathbf{m} \odot \mathbf{w}) \quad \text{subject to} \quad \|\mathbf{m}\|_0 \le k.

Here ⊙\odot is the entrywise product, so a weight with mask value 0 is forced to zero. The term ∥m∥0\|\mathbf{m}\|_0 counts the nonzero entries of m\mathbf{m}, which is the number of kept weights. The sparsity is the fraction removed: s=1−k/ns = 1 - k/n. A sparsity of 0.9 means ninety percent of the weights are zero.

Searching over all masks is hopeless, because there are (nk)\binom{n}{k} of them. Pruning methods are therefore heuristics that pick a mask one step at a time.

Magnitude pruning

The most common heuristic keeps the kk weights with the largest ∣wi∣|w_i|. Sort the absolute values, pick the cutoff tt that leaves exactly kk of them above it, and set

mi={1if ∣wi∣≥t,0otherwise.m_i = \begin{cases} 1 & \text{if } |w_i| \ge t, \\ 0 & \text{otherwise.} \end{cases}

It costs one sort per layer and no extra training signal. Its weakness is that it ignores the loss entirely. A small weight that sits on a very sensitive direction of the loss can matter more than a large weight on a flat direction.

Saliency from curvature

A better score comes from a second-order Taylor expansion of the loss. Write the change in loss when weight wiw_i is set to zero, so Δw=−wiei\Delta \mathbf{w} = -w_i \mathbf{e}_i, where ei\mathbf{e}_i is the unit vector for weight ii. Then

ΔL≈g⊤Δw+12Δw⊤H Δw.\Delta L \approx \mathbf{g}^\top \Delta \mathbf{w} + \tfrac{1}{2} \Delta \mathbf{w}^\top H \, \Delta \mathbf{w}.

Here g\mathbf{g} is the gradient and HH is the Hessian, the matrix of second derivatives. A trained network sits near a minimum, so g≈0\mathbf{g} \approx 0. Keeping only the diagonal entry hiih_{ii} of HH gives the saliency

si=12 hii wi2.s_i = \tfrac{1}{2} \, h_{ii} \, w_i^2.

This is the score used in Optimal Brain Damage, proposed by LeCun, Denker and Solla in 1990. It removes the weights with the smallest sis_i. Weights with a large hiih_{ii} are the ones the loss cares about most.

Structured versus unstructured

Everything above removes individual weights, which is unstructured pruning. The mask is irregular, so a dense matrix multiply still does all the work on zeros unless the hardware or library has a sparse kernel. Structured pruning removes whole rows, columns, filters or attention heads. The result is a smaller dense matrix, so the speedup shows up on ordinary hardware. The price is that structured removal is coarser and usually costs more accuracy for the same number of parameters removed.

Worked examples

Example
Magnitude pruning on six weights

Start with a layer whose weights are

w=(0.8,  −0.1,  0.4,  0.05,  −0.6,  0.2).\mathbf{w} = (0.8, \; -0.1, \; 0.4, \; 0.05, \; -0.6, \; 0.2).
  1. The absolute values are 0.8,0.1,0.4,0.05,0.6,0.20.8, 0.1, 0.4, 0.05, 0.6, 0.2.
  2. We want to keep k=3k = 3 weights, which is sparsity s=1/2s = 1/2.
  3. Sorted from largest to smallest, the absolute values are 0.8,0.6,0.4,0.2,0.1,0.050.8, 0.6, 0.4, 0.2, 0.1, 0.05.
  4. The top three are 0.80.8, 0.60.6 and 0.40.4, so the threshold is t=0.4t = 0.4.
  5. Zero out everything below 0.40.4 in absolute value. Those are −0.1-0.1, 0.050.05 and 0.20.2.
  6. The pruned layer is (0.8,  0,  0.4,  0,  −0.6,  0)(0.8, \; 0, \; 0.4, \; 0, \; -0.6, \; 0).

Interpretation: half the weights are gone, and the three that survived are the three with the largest magnitude. Nothing in this calculation looked at the loss, so the result is only as good as the assumption that size tracks importance.

Example
Saliency can disagree with magnitude

Two weights in the same layer have the following values, where hiih_{ii} is the diagonal Hessian entry.

Weightwiw_ihiih_{ii}Saliency 12hiiwi2\tfrac{1}{2} h_{ii} w_i^2
A0.50.54412×4×0.25=0.5\tfrac{1}{2} \times 4 \times 0.25 = 0.5
B2.02.00.10.112×0.1×4.0=0.2\tfrac{1}{2} \times 0.1 \times 4.0 = 0.2
  1. By magnitude, weight B (2.02.0) is four times larger than weight A (0.50.5), so magnitude pruning keeps B and removes A.
  2. By saliency, weight A scores 0.50.5 and weight B scores 0.20.2.
  3. So saliency keeps A and removes B.

Interpretation: weight B is large but sits in a flat region of the loss, so the network barely notices if it goes. Weight A is small but sits where the loss is steep, and removing it would hurt. Real pruning methods approximate the Hessian diagonal because computing the full Hessian for a large network is far too expensive.

A realistic case is a dense layer with 1000×10001000 \times 1000 weights stored as 32-bit floats. That is 106×410^6 \times 4 bytes, about 4 MB. Prune it to 90% sparsity and 10510^5 weights remain. In a dense format the file is still 4 MB, because the zeros are stored. In a compressed sparse format each surviving weight also needs an index of roughly 4 bytes, so the file is about 105×(4+4)=0.810^5 \times (4 + 4) = 0.8 MB plus a small amount of row bookkeeping. The memory saving is real only if you use a sparse format, and the speed saving is real only if the kernel exploits the sparsity. This is why structured pruning is popular when the goal is latency on ordinary hardware.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Heavy pruning in one stepAccuracy collapses and fine-tuning cannot recover itPrune gradually over several rounds, with fine-tuning between them
Target runs on a standard GPU or CPU with dense kernelsUnstructured zeros give almost no speedupStructured pruning of channels or heads, or a sparse kernel
Very small modelLittle redundancy, so each removal costs moreConsider quantization or a smaller architecture instead
Model used on new data after pruningPruned for one data distribution, weak on anotherRe-evaluate on the new data and fine-tune on a representative set
Pruning by magnitude aloneRemoves sensitive weights that happen to be smallUse a loss-aware score such as saliency, or gradient-based importance

Pruning is not the only way to shrink a model. Quantization stores the same weights in fewer bits, and distillation trains a small model to copy a large one. These methods combine well. A common pipeline prunes, then quantizes, then fine-tunes. For the wider family, see Model Compression.

History

Early work on removing network connections came in the late 1980s and early 1990s. LeCun, Denker and Solla's Optimal Brain Damage (1990) used the Hessian-based saliency described above. Hassibi and Stork's Optimal Brain Surgeon (1993) refined it with a fuller use of second-order information. These methods were principled but needed expensive curvature estimates, so they were hard to use on large networks.

Interest came back in the mid-2010s, when networks grew very large. Han, Pool, Tran and Dally (2015) showed that simple magnitude pruning, followed by retraining, could remove most of the connections in AlexNet with little loss of accuracy. Their follow-up, Deep Compression, combined pruning with quantization and weight sharing. Frankle and Carbin's Lottery Ticket Hypothesis (2018) later suggested that pruned networks contain small subnetworks that can train well from their original initialization, which changed how researchers think about why pruning works. The exact compression ratios in these papers are worth checking against the papers themselves.

Check yourself

QuestionA layer has weights (0.3, -0.9, 0.1, 0.6). You prune 50% by magnitude. Which weights survive?Show answer

Keep the two largest absolute values, which are 0.90.9 and 0.60.6. The survivors are −0.9-0.9 and 0.60.6. The weights 0.30.3 and 0.10.1 are set to zero. Magnitude ignores the loss, so this is the answer the rule gives, not a guarantee about accuracy.

QuestionA weight has w=1.5w = 1.5 and diagonal Hessian entry hii=0.2h_{ii} = 0.2. Another has w=0.5w = 0.5 and hii=8h_{ii} = 8. Compute both saliencies and say which one a saliency method removes first.Show answer

For the first, s=12×0.2×1.52=12×0.2×2.25=0.225s = \tfrac{1}{2} \times 0.2 \times 1.5^2 = \tfrac{1}{2} \times 0.2 \times 2.25 = 0.225. For the second, s=12×8×0.52=12×8×0.25=1.0s = \tfrac{1}{2} \times 8 \times 0.5^2 = \tfrac{1}{2} \times 8 \times 0.25 = 1.0. The method removes the first weight, because its saliency is smaller, even though it is three times larger in magnitude.

QuestionWhy is a 90% sparse weight matrix not automatically 10 times faster on a GPU?Show answer

GPUs are fast at dense matrix multiplication because they operate on contiguous blocks. Scattered zeros still occupy positions in the matrix, so a standard dense kernel multiplies by them and does the same amount of work. The speedup needs either a sparse kernel that skips zeros, which adds indexing overhead, or structured pruning that removes whole rows or channels so the remaining matrix is smaller and dense.

QuestionYou prune a model to 80% sparsity in one step and its accuracy drops from 92% to 40%. Describe a schedule that would likely do better and explain why.Show answer

Prune gradually. Remove a modest fraction, fine-tune for a while so the remaining weights readjust, then prune again, and repeat until the target sparsity is reached. Each round lets the network move into a new working state before the next removal, so the damage per step stays small. The final fine-tuning round then recovers most of the remaining accuracy.

Sources