How to implement neural network pruning?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Neural network pruning removes redundant weights or neurons from a trained model to reduce its size and inference cost with minimal accuracy loss. Modern deep networks are heavily over-parameterized — often 50-90% of weights can be removed without meaningful performance degradation. Pruning is essential for deploying models on mobile devices, edge hardware, and latency-sensitive applications.
Types of Pruning
| Type | What is removed | Granularity | Hardware benefit |
| Unstructured | Individual weights (set to zero) | Fine-grained | Requires sparse hardware/libraries |
| Structured | Entire filters, channels, or layers | Coarse-grained | Direct speedup on standard hardware |
| Semi-structured | N:M sparsity (e.g., 2 of every 4 weights) | Block-level | Supported by NVIDIA Ampere+ GPUs |
Method 1: Magnitude-Based Pruning with TensorFlow
TensorFlow Model Optimization Toolkit provides built-in pruning:
Method 2: Pruning with PyTorch
PyTorch provides torch.nn.utils.prune for both structured and unstructured pruning:
Global Pruning
Prune across all layers based on global weight magnitude:
Method 3: Iterative Pruning with Fine-Tuning
The most effective approach — prune gradually and retrain between rounds:
Method 4: Lottery Ticket Hypothesis
The Lottery Ticket Hypothesis (Frankle & Carlin, 2019) states that dense networks contain sparse subnetworks ("winning tickets") that can train to full accuracy from their original initialization:
Measuring Sparsity and Compression
Common Pitfalls
- Pruning without fine-tuning: Pruning a trained model and deploying it immediately causes significant accuracy loss. Always fine-tune after pruning — even 2-3 epochs can recover most of the lost accuracy.
- Unstructured sparsity is not free speed: Setting weights to zero does not speed up inference on standard GPUs/CPUs unless you use sparse matrix libraries (e.g.,
torch.sparse, cuSPARSE). Structured pruning (removing entire channels) gives direct speedups. - Pruning too aggressively: Removing 90%+ of weights in one step is rarely recoverable. Use iterative pruning with small increments (10-20% per round) and fine-tuning between rounds.
- Ignoring batch normalization: When pruning convolutional filters, the corresponding batch normalization parameters (gamma, beta, running mean, running variance) must also be removed. Forgetting this causes shape mismatches.
- Layer sensitivity: Not all layers tolerate the same pruning rate. Early layers and the final classifier are typically more sensitive. Use per-layer sensitivity analysis to set appropriate sparsity targets.
Summary
- Use magnitude-based pruning (
l1_unstructured) as the baseline — it is simple and effective - Use iterative pruning with fine-tuning for best accuracy retention at high sparsity
- Use structured pruning to get real inference speedups on standard hardware
- TensorFlow:
tensorflow_model_optimizationwithPolynomialDecayschedule - PyTorch:
torch.nn.utils.prunewithl1_unstructuredorln_structured - Always measure both sparsity and accuracy after pruning — target the best trade-off for your deployment constraints
Related reading
- How to implement pixel-wise classification for scene labeling in TensorFlow?
- How to implement PReLU activation in Tensorflow?
- How to implement pytesseract code with opencl to make it run on GPU?
- How to implement Tensorflow batch normalization in LSTM
- How to implement sklearn's PolynomialFeatures in tensorflow?
- How to implement tensorflow Estimator with multiple models for GAN?
- How to implement Ologn decrease-key operation for min-heap based Priority Queue?
- How to implement strlen as fast as possible

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.