Pruning: removing weights, and what it costs
Zero the smallest weights or remove whole units, fine-tune what's left, and measure the result on rows the model didn't train on. Know when pruning saves memory and when it saves time.
What runs in CodeSplash
- TRN016Magnitude pruning and structured pruning Partly runs in CodeSplash
- Deep neural network (torch MLP) Verified in Python and R
You will be able to
- Explain magnitude pruning and the sparsity it reports
- Tell unstructured pruning from structured pruning, and which one speeds a model up
- Prune a CodeSplash network with Compress, with and without fine-tuning
- Measure a pruned model fairly
The smallest weights matter least, usually
ReadMagnitude pruningWeight pruningSetting a network's smallest weights to zero to shrink it, then checking how much accuracy that costs. sets every weight below a threshold to zero. Trained networks usually have many weights near zero whose removal barely moves the predictions.
In the reference's example, a two-by-two matrix is pruned at a threshold of a tenth: the two small entries become zero, the others stay, and the reported sparsity is 0.5, half the weights. Pruning the largest weights instead would wreck the layer.
Scattered zeros or whole units?
ReadUnstructured pruning removes single weights wherever they are. The matrix keeps its shape, so it only gets smaller if it's stored in a sparse format, and only faster with sparse kernels that skip zeros.
Structured pruningStructured pruningRemove whole units, channels, heads or layers instead of single weights, so the smaller network runs faster everywhere. removes whole units, channels, attention heads or layers. The result is an ordinary, smaller dense network that runs faster everywhere. It usually costs more accuracy for the same share of weights, because whole units are coarser to remove.
Prune in CodeSplash
In the appOpen a finished neural-network run and choose Make a model from this → Compress. Prune sets a share of the weights to zero, smallest first across all weight matrices; the output layer is kept whole. Fine-tune after pruning then trains the kept weights for a few epochs while pruned ones stay zero, which wins back some accuracy. 8-bit weights, if chosen, round what's left afterwards.
Choose rows the model didn't train on to measure it. On its own training rows the pruned model's score is optimistic, and every fold scores the same compressed network.
Prune, then fine-tune
ReadRemoving weights moves the predictions a little; a few epochs of training let the remaining weights compensate. Pruning in several rounds, each followed by fine-tuning, usually beats one large cut. Distillation can help too: fine-tune the pruned network on the original model's soft targets instead of only the labels.
Predict: 50% fewer weights
PredictPredict first
You prune half the weights of a dense network by magnitude and keep the matrices in their usual dense format. How fast does it predict?
Reveal
About the same. A dense matrix full of zeros still multiplies every entry. Speed needs structured pruning (a smaller dense network) or sparse storage and kernels.
What runs here
ReadCommon mistakes
ReadCheck: pruning
CheckpointCheck yourself
A layer has 400 weights. After magnitude pruning, 100 are left nonzero. What sparsity is reported (as a share)?
Show the answer
Answer: 0.75
Sparsity is the share of weights set to zero: 300 / 400 = 0.75.
Check yourself
You need a network that runs faster on an ordinary CPU, without special sparse kernels. Which pruning fits?
Show the answer
Answer: a
A smaller dense network is faster everywhere. Scattered zeros need sparse formats and kernels to pay off.
Check yourself
Which rows should Compress measure a pruned model on?
Show the answer
Answer: a
On its own training rows a pruned model looks better than it is. Held-out rows show what pruning cost.
Check yourself
What does Fine-tune after pruning do in Compress?
Show the answer
Answer: a
Fine-tuning lets the remaining weights compensate for the removed ones. 8-bit rounding, if chosen, comes after.
Recap
Recap- Magnitude pruning zeros the smallest weights; sparsity is the share removed.
- Unstructured pruning saves space only when stored sparsely; structured pruning gives a smaller, faster dense network.
- In CodeSplash, Compress prunes (output layer kept whole), can fine-tune the kept weights, and measures on the rows you choose.
- Measure on rows the model didn't train on.
Concepts in this lesson