Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Pruning

Pruning removes low-importance weights, filters, or layers from a trained neural network to shrink it and speed up inference, then fine-tunes to recover.

Pruning is a model optimization technique that removes weights, neurons, filters, attention heads, or whole layers that contribute little to a trained neural network's output, leaving a smaller model that needs less memory, compute, and energy. In deep learning the term is a synonym for model pruning. Most networks are heavily over-parameterized: Han et al. cut AlexNet from 61 million to 6.7 million parameters and VGG-16 from 138 million to 10.3 million on ImageNet with no loss of accuracy by training, removing low-magnitude connections, and retraining the rest. Pruning is a common step before edge AI deployment, where memory, power, and inference latency budgets are tight.

How Pruning Works#

Every pruning method decides which parameters to remove and when to remove them. The importance score is usually weight magnitude, since near-zero weights change the output least, while gradient-based scores estimate each parameter's effect on the loss function more directly. Removing a large fraction at once tends to cost more accuracy than iterative pruning, which removes a small fraction, fine-tunes the remaining weights to recover, and repeats until the size or speed target is met. The Lottery Ticket Hypothesis adds that dense networks contain sparse subnetworks that, reset to their original initialization, train on their own to accuracy comparable to the full network.

Unstructured, Structured, and Semi-Structured Pruning#

Unstructured pruning zeroes individual weights anywhere in a tensor. It reaches the highest sparsity for a given accuracy, but the result is an irregular sparse matrix that dense GPU and CPU kernels still process at full cost unless a sparse runtime skips the zeros. Structured pruning removes whole filters, channels, attention heads, or layers from a convolutional neural network or transformer, so each pruned layer becomes a smaller dense layer that runs faster on any hardware, at the cost of more accuracy loss per removed parameter. PyTorch's pruning utilities apply both kinds as masks on an existing layer:

import copy

from torch import nn
from torch.nn.utils import prune

# Two copies of one convolution layer with 32 filters of shape 16x3x3
unstructured = nn.Conv2d(16, 32, kernel_size=3)
structured = copy.deepcopy(unstructured)

# Unstructured: zero the 50% of individual weights with the smallest magnitude
prune.l1_unstructured(unstructured, name="weight", amount=0.5)

# Structured: zero the 50% of whole filters (dim=0) with the smallest L2 norm
prune.ln_structured(structured, name="weight", amount=0.5, n=2, dim=0)

for name, layer in [("Unstructured", unstructured), ("Structured", structured)]:
    zeros = (layer.weight == 0).float().mean()
    empty_filters = (layer.weight.flatten(1).abs().sum(dim=1) == 0).sum()
    print(f"{name}: {zeros:.0%} of weights zeroed, {empty_filters}/32 filters entirely empty")

Both layers lose 50% of their weights, but only the structured layer ends up with 16 of its 32 filters entirely empty, which can be physically removed to leave a dense 16-filter convolution.

Semi-structured pruning sits between the two. NVIDIA's 2:4 sparsity pattern zeroes two of every four consecutive weights, which Sparse Tensor Cores on Ampere and later GPUs skip to do the same matrix math in half the time; NVIDIA measured end-to-end gains of up to 20% for a sparse ResNeXt-101 in TensorRT on an A100. On dense hardware, scattered zeros alone buy no speed: pruning Ultralytics YOLOv5x to 30% global unstructured sparsity with its prune() utility, without fine-tuning, lowered COCO mAP50-95 from 0.507 to 0.489 and left V100 inference time unchanged at 5.2 ms per image.

Real-World Applications#

Pruning matters most where a model must run on constrained hardware. Channel-pruned object detection networks fit the memory and power budgets of drones, smart cameras, and phones, enabling real-time inference without a cloud connection and keeping imagery on the device for data privacy. In manufacturing inspection, the same compression lets a defect detector run on a low-cost edge module beside the production line.

Large language models are pruned too. SparseGPT prunes OPT-175B and BLOOM-176B to 60% unstructured sparsity in one shot, without retraining, with a negligible increase in perplexity. NVIDIA's Minitron work pruned Llama 3.1 8B to a 4B model by removing layers or hidden dimensions, then recovered quality with knowledge distillation from the original model.

Pruning is often combined with other compression methods, and the term also has an older meaning outside neural networks.

  • Model Quantization: Quantization keeps every weight and lowers its numeric precision, such as from 32-bit floats to 8-bit integers. Pruned models are often quantized afterward for further savings.
  • Knowledge Distillation: Distillation trains a separate compact student to imitate a larger teacher, and it is a common way to recover the accuracy a heavily pruned model loses.
  • Dropout Layer: Dropout zeroes random activations for a single training step as regularization and leaves the parameter count unchanged, while pruning removes parameters permanently.
  • Decision Tree Pruning: In classical machine learning, pruning cuts branches from a fitted decision tree to reduce overfitting, as in scikit-learn's cost-complexity pruning, which removes the weakest subtrees as its ccp_alpha parameter grows.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning