Search

Search papers and lessons

Learning both Weights and Connections for Efficient Neural Networks

Song Han, Jeff Pool, John Tran et al. · 2015

Han et al. (2015) remove most connections of trained image networks and retrain the rest, cutting AlexNet's weights ninefold and VGG-16's thirteenfold with no loss in accuracy.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Optimizers
  • ReadReady to readNot yet

TL;DR

Large image networks carry far more weights than they need, and each weight fetched from slow memory costs energy. Han, Pool, Tran and Dally showed that you can train a network, delete the connections with small weights, and retrain what remains, without losing accuracy. On ImageNet they cut the parameters of AlexNet from 61 million to 6.7 million and of VGG-16 from 138 million to 10.3 million.

Builds on: Convolutional Neural Network, L2 Regularization, Stochastic Gradient Descent · Leads to: Pruning, The Lottery Ticket Hypothesis: Finding Sparse, Trainable Neural Networks

Why it mattered

Pruning was studied before this paper, mostly to reduce overfitting and model size. This paper tied pruning to a hardware constraint. Moving a 32-bit number from off-chip DRAM costs about 640 picojoules, while the same number in on-chip SRAM costs about 5 picojoules. A model that fits on chip avoids most of that cost. The recipe here, a few rounds of prune and retrain, became the standard way to shrink a network after training. It is the starting point for later work on sparse models and compression, including Quantization and Knowledge Distillation, and it prompted the question of whether the sparse structure can be trained from the start, which The Lottery Ticket Hypothesis: Finding Sparse, Trainable Neural Networks takes up.

The problem

By 2015 convolutional networks had grown quickly. LeNet-5, from 1998, had under 1 million parameters and classified handwritten digits. The 2012 AlexNet entry to the ImageNet competition had about 60 million. A 2014 face model had about 120 million, and a network scaled up by Coates and colleagues had about 10 billion. Their storage, memory bandwidth and computation made them hard to run on phones and embedded systems.

The authors give a concrete estimate. Running a network with 1 billion connections 20 times per second, with each connection fetched from off-chip DRAM at 640 picojoules, takes (20)(109)(640×10−12)=12.8(20)(10^9)(640 \times 10^{-12}) = 12.8 watts for memory access alone. That is far beyond what a mobile device can supply. The question is whether the network can be made smaller without changing what it computes much.

Other routes existed. Low-rank approximations and quantization reduce the cost of each parameter, and the paper notes they can be combined with pruning. Methods that remove parameters using second-order information, such as Optimal Brain Damage and Optimal Brain Surgeon, were accurate but needed extra computation. The paper's goal is a simple method that uses ordinary training.

How it works

Here is the method on a toy layer with four weights. Pretend the trained weights of one layer are 0.90.9, −0.1-0.1, 0.050.05 and −0.6-0.6, and we choose a threshold of 0.20.2. The authors set each layer's threshold from the spread of its weights, but a fixed number makes the steps easy to follow.

flowchart LR
    A["Train the dense network"] --> B["Prune weights below the threshold"]
    B --> C["Retrain the sparse network"]
    C -->|"repeat for more compression"| B
    C --> D["Deploy the sparse network"]
  1. Train. Train the full network with ordinary training, starting from random values. The goal is to learn which connections matter, not the final weight values. In the toy layer the trained weights are 0.90.9, −0.1-0.1, 0.050.05 and −0.6-0.6. Check: four weights, all present.
  2. Prune. Remove every connection whose absolute weight is below 0.20.2. The weights −0.1-0.1 and 0.050.05 have absolute values 0.10.1 and 0.050.05, so both go. The weights 0.90.9 and −0.6-0.6 stay. Check: two of four survive, so half the layer is gone.
  3. Retrain. Keep the surviving weights under a mask, so pruned connections stay at zero. Train the remaining two weights with a small learning rate. Suppose one gradient step moves 0.90.9 to 0.850.85. Check: the surviving weights change, and the removed ones never return.
  4. Repeat or stop. Prune again with the new weights, retrain, and repeat while accuracy holds. Check: each round removes a fraction of what is left, so the savings compound.

In a real network the same steps run on every layer, each with its own threshold, and the mask is applied to every weight tensor. The next section writes the steps in symbols.

The math

Definition: Pruning mask
A binary mask mm has the same shape as a weight tensor WW. Entry mijm_{ij} is 1 if the connection is kept and 0 if it is removed. The sparse network uses the element-wise product m⊙Wm \odot W in place of WW.

For layer ll, let WlW_l be its weights and let σl\sigma_l be the standard deviation of those weights. The paper sets the threshold for that layer to its quality parameter qlq_l times σl\sigma_l.

Step 1 in symbols: training

Train the dense network to get weights W(0)W^{(0)} that minimize the loss on the training data, using Stochastic Gradient Descent. The paper uses L2 Regularization in this phase. L2 pushes weights toward zero without making them exactly zero, which is why a threshold is needed.

Step 2 in symbols: the threshold and mask

For each layer, set tl=qlσlt_l = q_l \sigma_l and define

ml,ij={1if ∣Wl,ij∣≥tl0otherwisem_{l,ij} = \begin{cases} 1 & \text{if } |W_{l,ij}| \ge t_l \\ 0 & \text{otherwise} \end{cases}

In the toy layer the threshold 0.20.2 was fixed, so the rule reads: ∣0.9∣≥0.2|0.9| \ge 0.2 keeps the first weight, and ∣0.05∣<0.2|0.05| \lt 0.2 removes the third.

Step 3 in symbols: masked retraining

Retraining minimizes the same loss, but the forward pass uses m⊙Wm \odot W, so the gradient on a pruned weight is zero. The surviving weights start from their trained values, not from new random values. The paper argues this matters because convolutional features are fragile: reinitializing layers and retraining them loses accuracy that the surviving weights still carry.

Dropout after pruning

Dropout drops each unit with probability DD during training. Pruning also reduces capacity, so the paper lowers DD for retraining. Let Ci=NiNi−1C_i = N_i N_{i-1} be the number of connections in a layer with NiN_i neurons in layer ii and Ni−1N_{i-1} in the layer before. Dropout acts on neurons, while the connection count varies quadratically with the neuron count, so the paper ties the dropout change to the connection counts and sets

Dr=DoCirCioD_r = D_o \sqrt{\frac{C_{ir}}{C_{io}}}

where DoD_o is the original dropout rate, DrD_r is the rate during retraining, CioC_{io} is the connection count before pruning, and CirC_{ir} is the count after retraining. If pruning keeps one ninth of the connections, the square root is 1/31/3, so Dr=Do/3D_r = D_o/3. With Do=0.5D_o = 0.5 the retraining rate becomes about 0.170.17.

Compression ratio and storage

The compression ratio is the number of original weights divided by the number of survivors. For AlexNet the paper reports 61 million and 6.7 million weights, a ratio of 61/6.7≈9.161 / 6.7 \approx 9.1, which the paper reports as 9 times. The paper stores the sparse layers in sparse form with indices. The storage overhead is 15.6 percent, and relative indices need 5 bits in fully connected layers and 8 bits in convolutional layers. A Sparse Matrix format pays for itself only when most entries are gone.

Results

The paper prunes four networks: LeNet-300-100 and LeNet-5 on MNIST, and AlexNet and VGG-16 on ImageNet (ILSVRC-2012, with 1.2 million training and 50 thousand validation images). The implementation is in Caffe, with a mask added to each weight tensor, and the experiments ran on Nvidia TitanX and GTX980 GPUs. The reference models are from the Caffe model zoo.

NetworkTop-1 error (ref / pruned)Top-5 error (ref / pruned)Parameters (ref / pruned)Compression
LeNet-300-1001.64% / 1.59%-267K / 22K12x
LeNet-50.80% / 0.77%-431K / 36K12x
AlexNet42.78% / 42.77%19.73% / 19.67%61M / 6.7M9x
VGG-1631.50% / 31.34%11.32% / 10.88%138M / 10.3M13x

Three findings matter most.

  • Accuracy holds. On every network the pruned model's top-1 error is equal to or lower than the reference, by at most 0.16 points (on VGG-16), and the other nets change by 0.03 points or less. The authors attribute the small improvements to pruning finding a better capacity and reducing overfitting.
  • Weights and operations fall by different amounts. AlexNet keeps 11 percent of its weights but only about 30 percent of its floating point operations, roughly 3 times fewer. The convolutional layers, which do most of the computation, were pruned less than the fully connected layers, which hold most of the parameters. VGG-16 keeps 7.5 percent of its weights and 21 percent of its operations.
  • Iteration helps up to a point. On AlexNet, iterative pruning reaches 9 times compression with no accuracy loss. A single aggressive pruning step reaches only about 5 times without loss.

The paper also reports two training trade-offs. Without retraining, accuracy begins to fall once about one third of the connections remain. The paper's "free lunch" without retraining is about 2 times compression. Retraining AlexNet took 173 hours on a Titan X, against 75 hours to train the original.

Some limits follow from the paper's own statements. The method is evaluated on image classification networks only. Layers differ in sensitivity: the first convolutional layer, which sees the 3-channel input, is the most sensitive, so the paper uses a lower threshold there. L1 regularization gives better accuracy directly after pruning, but L2 gives better accuracy after retraining, and using L1 for pruning and L2 for retraining did not beat L2 throughout. Finally, the paper says the benefit depends on hardware. It targets fixed-function hardware built for sparse networks, because general-purpose processors handle sparse computation poorly.

Reading guide

Check yourself

QuestionName the three steps of the method, in order.Show answer
Train the dense network to learn which connections matter, prune the connections whose weights fall below a threshold, and retrain the remaining sparse network to learn its final weights.
QuestionA layer has weights (0.4, −0.05, 0.3, 0.02)(0.4,\ -0.05,\ 0.3,\ 0.02) and the threshold is 0.10.1. Which weights survive, and what fraction of the layer remains?Show answer
The weights 0.40.4 and 0.30.3 survive because their absolute values are at least 0.10.1. The weights −0.05-0.05 and 0.020.02 are removed. Half the layer remains, a density of 2/4=0.52/4 = 0.5.
QuestionThe paper reports that AlexNet's parameters fall from 61 million to 6.7 million. What is the compression ratio, and why does the paper not claim the same speedup in operations?Show answer
The ratio is 61/6.7≈9.161 / 6.7 \approx 9.1. The operation count fell only to about 30 percent of the original, roughly 3 times fewer, because the convolutional layers, which do most of the computation, were pruned less than the fully connected layers, which hold most of the parameters.
QuestionA researcher prunes a trained network by 80 percent, skips retraining, and reports that accuracy is unchanged. Is this consistent with the paper?Show answer
Not as a general claim. The paper finds that without retraining, accuracy begins to drop once only about one third of the original connections remain, and that the no-loss point without retraining is about 2 times compression. Keeping one fifth of the connections without retraining would be expected to lose accuracy. With retraining, the paper reaches 9 times compression with no loss.

Sources

  • Song Han, Jeff Pool, John Tran, William J. Dally, "Learning both Weights and Connections for Efficient Neural Networks," 2015. arXiv:1506.02626
  • Song Han, Huizi Mao, William J. Dally, "Deep Compression," 2015. arXiv:1510.00149

Read the full paper

Song Han, Jeff Pool, John Tran et al., 2015. arXiv:1506.02626