Edge AI Lab

Classify images and detect objects with real ONNX models in your browser, try quantization and pruning, and compare edge with cloud.

8interactive experiments

Pruning

Removing the least important weights or channels to cut model size and computation, and the difference between structured and unstructured pruning.

Intermediate10 min read

Educational draft content that needs expert review before it is used as an academic reference. Specifications, versions and licences change; check the official source. No experimental result is shown here unless it was actually run.

Learning objectives

  • Define and compute sparsity
  • Distinguish structured from unstructured pruning and their effect on speed
  • Explain the role of fine-tuning

Pruning removes parts of the network thought to be unimportant. The simplest method is magnitude pruning, which zeroes the weights with the smallest absolute values by threshold or ratio. Sparsity is the fraction of zero weights.

Unstructured pruning removes individual weights and yields sparse matrices; it saves space if stored in a sparse format but speeds up execution only if the hardware and kernels support sparsity. Structured pruning removes whole neurons, channels or heads so dimensions really shrink and execution speeds up on ordinary hardware, but accuracy loss can be larger.

Pruning is usually followed by fine-tuning to recover accuracy and may be repeated in stages. The “lottery ticket” hypothesis suggests that small sub-networks exist that train as well; it is an active research topic.

Diagram

Pruning: a dense network versus a pruned oneTwo three-layer networks; the first has all connections, the second has some weak connections removed.DensePruned (weak links removed)Fewer non-zero weights speed up execution only if the runtime supports sparsity
Schematic, not a real network.

Terminology (with Arabic equivalents)

Sparsityالتخلخل
Magnitude pruningالتقليم بالمقدار
Structured pruningالتقليم المهيكل
Fine-tuningإعادة تدريب دقيقة
Lottery ticket hypothesisفرضية تذكرة اليانصيب

Worked example

A weight matrix of 1000 elements with 70% pruned: 700 zeros and 300 non-zeros. If stored as an ordinary dense matrix the size stays 1000 elements; the saving is realised only in a sparse format (values + indices).

Code snippet

PythonNumPy, illustrative
import numpy as np

def magnitude_prune(w, sparsity):
    k = int(w.size * sparsity)
    thr = np.partition(np.abs(w).ravel(), k)[k]
    mask = np.abs(w) >= thr
    return w * mask

w = np.random.default_rng(0).normal(size=(10, 100))
wp = magnitude_prune(w, 0.7)
print('sparsity', float((wp == 0).mean()))

An illustrative snippet that was not executed in the lab environment; check library versions in their official documentation before use.

Check yourself

  1. Unstructured pruning speeds up execution on ordinary hardware:

    • Always
    • Only if the hardware and kernels support sparsity
    • Never
    • Only on phones
    Answer and explanation

    Only if the hardware and kernels support sparsity — Scattered zeros save no time without specialised kernels.

  2. What does structured pruning remove?

    • Scattered weights
    • Whole channels or neurons
    • Training data
    • Layer names
    Answer and explanation

    Whole channels or neurons — It actually shrinks the dimensions.

  3. What is sparsity?

    • The number of layers
    • The fraction of zero weights
    • Training speed
    • Batch size
    Answer and explanation

    The fraction of zero weights — The ratio of zeros to all weights.

References

Suggested reading (links not verified yet)

  • Han et al. (2015). Learning both Weights and Connections for Efficient Neural Networks. NIPS 2015 Not verified
  • Frankle & Carbin (2019). The Lottery Ticket Hypothesis. ICLR 2019 Not verified
  • Blalock et al. (2020). What is the State of Neural Network Pruning? MLSys 2020 Not verified

Image and audio processing in the experiments runs inside your browser. Your images and audio are never uploaded and conversations are not stored. Privacy