Majid Al-RaimiFine-tuning, iterative pruning and regularization

COE 592Lecture 4.2Part 06

Fine-tuning, iterative pruning and regularization

Pruning costs accuracy, fine-tuning at a much smaller learning rate recovers it, repeating prune and fine-tune pushes AlexNet from 5x to 9x compression, and L1 or L2 regularization during training makes weights easier to prune.

Concepts
4
Slides
41-52
Reading
24 min
Understood
0/4 concepts

Why this part matters

Every pruning method in this course, magnitude pruning, AMC, NetAdapt and the 2:4 sparsity of part 10, only reaches its quoted ratio because a fine-tuning step follows the cut. The EIE numbers in the next part assume the roughly 89% weight sparsity that this part explains how to obtain.

This is the fifth of the five questions the lecture opened with, and for embedded research it is the recipe you will actually run: cut, retrain the survivors gently, cut again. Two hyperparameters decide whether the loop works, the learning rate during fine-tuning and the regularizer used during training, and both are favorite exam questions because each has a single number or a single formula attached.

By the end you can

  1. Explain why accuracy drops after pruning and why fine-tuning at 1/10 to 1/100 of the original learning rate recovers it.
  2. Draw the Train Connectivity, Prune Connections, Train Weights pipeline with its loop arrow, and define one iteration.
  3. Read the three accuracy-loss curves and quote the 5x to 9x AlexNet result with its parameter counts.
  4. Write the L1 and L2 regularized losses, say which yields exact zeros, and explain why L2 still won for magnitude pruning.
  5. Connect Network Slimming's penalty on batch norm scaling factors to scaling-based channel pruning from lecture 04-1.

Take the AlexNet that Han, Pool, Tran and Dally started from in 2015: 61 million parameters, five convolution layers and three fully connected ones, 57.2% top-1 accuracy. They kept roughly one weight in nine and ended with a network that was not measurably worse. That is the headline of this part, and the whole trick behind it is what happens after the cut.

AlexNet before and after prune plus retrain (Han et al. 2015, Table 1 and section 4.2)

Parameters
61M to 6.7M (9x)
Top-1 error
42.78% to 42.77%
Top-5 error
19.73% to 19.67%
Original training time
75 hours on a Titan X
Retraining time after pruning
173 hours at 1/100 of the initial learning rate

Now look at what those hours say. Retraining took more than twice as long as training. The authors did not treat it as optional polish; section 3 of the paper calls the final retraining step critical and states that if the pruned network is used without retraining, accuracy is significantly impacted. So the answer to the outline's fifth question, how to fine-tune a pruned network, is a method, not a footnote: the Pruning mask decides which weights exist, and Fine-tuning decides whether the survivors can still do the job.

Why the survivors are wounded, and why the wound heals

A trained layer is a set of weights that were optimized together. Zeroing the smallest of them, the magnitude criterion from lecture 04-1, is a small perturbation to any one output, but a large Pruning ratio removes so many small contributions that the outputs shift and the accuracy drops, more steeply the higher the ratio (this is what the chart on the slide shows for the dashed curve). The survivors are still near a good solution, though. Nothing about them was wrong; the function they were tuned to complete simply lost some of its terms. Fine-tuning continues gradient descent on exactly those survivors, with the removed weights held at zero, so they can absorb the work of the missing ones.

This is why the learning rate matters so much. The slide states the rule: the fine-tuning learning rate is usually 1/100 or 1/10 of the original learning rate. In the paper, LeNet was retrained at 1/10 of the original rate (section 4.1) and AlexNet at 1/100 of the initial rate (section 4.2). A large step would throw the weights out of the basin in which the magnitude ranking was measured, undoing the very judgement the pruning made. Small steps walk the network back down the loss surface from where the cut left it.

Slide 41 makes the same point as an optimization statement. Pruning poses the problem as minimizing the loss over the pruned weights W_P subject to a budget N on the L0 norm, the count of nonzero weights. The mask chooses which entries of W_P may be nonzero at all (the constraint), and fine-tuning is the argmin over the survivors with that mask fixed: the best values the kept weights can take.

argminWPL(x;WP) s.t. WP0N\arg\min_{\mathbf{W}_P} L(\mathbf{x};\mathbf{W}_P)\ \text{s.t.}\ \|\mathbf{W}_P\|_0 \le N
Slide 41: pruning as constrained optimization; fine-tuning solves the argmin over the survivors
Pruning kicks the network partway up the loss bowl. Fine-tuning at a tenth or a hundredth of the original rate takes many small steps back to the minimum; one original-sized step overshoots to the far wall.

The paper adds a second reason to keep the survivors instead of re-initializing them. Section 3.3 observes that networks contain fragile co-adapted features: gradient descent finds a good solution when the network is first trained, but not after re-initializing some layers and retraining them. Fine-tuning starts from the co-adapted weights and preserves that structure. The paper also notes that during retraining it can help to fix the convolution parameters while the fully connected ones retrain, and vice versa, to avoid vanishing gradients through the sparse layers.

The three-box pipeline

The figure on the slide is Figure 2 of the paper. The first box, Train Connectivity, is ordinary training, but its purpose is reframed: it learns which connections are important, not their final values. The second box, Prune Connections, applies the magnitude threshold. The third box, Train Weights, retrains the sparse network. In Han et al.'s implementation the threshold was a quality parameter multiplied by the standard deviation of each layer's weights, and Caffe was modified to add a mask that disregards pruned parameters during network operation for each weight tensor.

Train Connectivity
dense training

Learn which connections matter, not just their values.

rank by |w|
Prune Connections
threshold on magnitude

Zero weights below the threshold and fix the mask.

LR / 10 to LR / 100
Train Weights
fine-tune survivors

Retrain the sparse network to recover accuracy.

Train Connectivity, Prune Connections, Train Weights (Han et al. 2015, Figure 2)

Reading the chart on slide 43

The chart plots accuracy loss against the fraction of parameters pruned away for AlexNet. The dashed purple curve is pruning with no retraining; the green curve adds fine-tuning. Reading the points off the figure gives the following.

Pruned awayPruning onlyPruning + fine-tuning
50%about -0.1about 0.0
67%about -0.9about +0.2
75%about -2.1about +0.2
80%about -4.0about 0.0
85.5%off the chartabout -0.5
90%off the chartabout -1.7
93%off the chartabout -3.8
Accuracy loss read off the slide 43 chart

The paper summarizes the same curves in section 5: without retraining, accuracy begins dropping much sooner, at one third of the original connections rather than one tenth. It describes a free lunch of removing half the connections with no loss even without retraining, while with retraining the connections can be reduced by 9x. Read that 9x carefully: it is reached with the iterative scheme of the next two concepts. A single prune and retrain, the green curve, is already at about -1.7% by 90% pruned. Two green points (at about 67% and 75% pruned) sit slightly above zero, which the authors attribute to pruning finding the right capacity of the network and thereby reducing overfitting. Because sparsity and compression ratio are two ways of saying the same thing, keep the conversion at hand: ratio = 1 / (1 - s).

RatioPruned away
2x50%
3x66.7%
5x80%
9x88.9%
10x90%
13x92.3%
Compression ratio to fraction pruned away, s = 1 - 1/ratio

Quick check

Fine-tuning a pruned network usually uses which learning rate, relative to the original?

Recall

What learning rate does fine-tuning a pruned network usually use, and why?

1/10 to 1/100 of the original (Han et al.: 1/10 for LeNet, 1/100 for AlexNet). The survivors are already near a good minimum; large steps would leave the basin and destroy the co-adapted features the pruning ranked.

Once retraining is part of the recipe, a natural question follows: why cut to the final sparsity in one go? The slides tell the story as a sequence. Prune to 30%, retrain the weights. Prune to 50%, retrain. Prune to 70%, retrain. The only change to the pipeline figure is a single arrow from Train Weights back up to Prune Connections, and that arrow is the whole idea of Iterative pruning.

Define the unit first: one iteration is a prune followed by a fine-tune. Iterative pruning repeats that unit while gradually increasing the target sparsity in each iteration, instead of jumping to the final sparsity in one step. Han et al. put it plainly in section 3.4: learning the right connections is an iterative process, pruning followed by retraining is one iteration, and after many such iterations the minimum number of connections can be found. Each iteration is a greedy search for the best connections. They also tried pruning parameters probabilistically by absolute value and found it gave worse results.

The loop arrow from Train Weights back to Prune Connections. Each pass raises the target (30, 50, 70, 80, 90 percent) and the 6 by 6 matrix loses its smallest survivors until four weights remain, a 9x ratio.

Why gradual beats one-shot

The magnitude criterion ranks weights on the network as it is right now. After a 30% cut and a fine-tune, the survivors have re-settled: some grew to cover the removed terms, some shrank because their job was taken over. The ranking for the next cut is therefore measured on a network that has adapted, and the weights it marks as small really are the least needed. A one-shot cut to 90% ranks all 90% on a network that has never adapted to any loss, so it discards weights that would have become important once their neighbors were gone. Zhu and Gupta describe the same principle in the general form of a sparsity schedule: the binary mask is updated every few hundred steps to gradually increase the sparsity of the network while allowing the training steps to recover from any pruning-induced loss in accuracy. Their cubic schedule prunes quickly at first, when redundant connections are abundant, and slowly near the end.

st=sf+(sisf)(1tt0nΔt)3s_t = s_f + (s_i - s_f)\left(1 - \frac{t - t_0}{n\,\Delta t}\right)^3
Zhu and Gupta's gradual schedule: sparsity ramps from s_i (usually 0) to s_f over n pruning steps spaced delta t apart

The schedule in this part uses five hand-picked targets rather than a closed form, but the shape is the same: larger cuts early, smaller ones late. On a 6 x 6 matrix of 36 weights, rounding 36 s gives the following zero counts, which are also what the simulator below shows.

TargetZerosNonzeros leftActual sparsityRatio
30%112530.6%1.4x
50%181850.0%2.0x
70%251169.4%3.3x
80%29780.6%5.1x
90%32488.9%9.0x
A five-step schedule on 36 weights (zeros = round(36 s))
SimulatorIterative pruning stepper: a 6 by 6 weight matrix on a sparsity schedule
+0.5%0%-1%-2%-3%-4%40%50%60%70%80%90%100%parameters pruned awayone-shot + fine-tuneiterative
Nonzeros36of 36
Sparsity0%zerosno target applied yet
Compression1.0xratio36 / 36
Toy loss0.0%iterative curve
The matrix starts dense with 36 nonzeros. Each press prunes the smallest survivors to the next target in the schedule 30%, 50%, 70%, 80%, 90% and then fine-tunes what is left.

Illustrative only. Fine-tuning is stood in for by letting each surviving weight in a row absorb 0.5 of the magnitude its row just lost, an echo of the surviving weights spreading outward in Han et al. Figure 7. The two loss curves are shape fits to the green and red curves of slide 51, not measured data. Cells with a teal border were zeroed in the latest step; survivors with a bright border moved up the magnitude ranking.

The loop also connects backwards to part 05. NetAdapt runs a short-term fine-tune after every layer it thins and a long-term fine-tune at the end, which is the same prune-then-fine-tune loop driven by a latency budget instead of a sparsity schedule. Whatever criterion or granularity you choose, the outer loop looks like this figure.

Quick check

In iterative pruning, what exactly counts as one iteration?

Recall

Define one iteration of iterative pruning and say what changes between iterations.

Prune, then fine-tune (retrain the surviving weights). The target sparsity is raised each iteration, and the weights removed are the smallest survivors of the previous round.

Slide 51 adds one bullet and one curve, and together they carry the number you will be asked for: Iterative pruning boosts the Pruning ratio from 5x to 9x on AlexNet compared to single-step aggressive pruning. The red curve is what that sentence looks like.

The slide 51 chart redrawn from its read-off points. The teal prune plus fine-tune curve draws first, then the violet iterative curve. The 5x line marks where a single aggressive cut lands, the 9x line where iterative pruning still shows no loss.
CurveWithin 0.5% loss up toReaches about -4% at
Pruning onlyabout 60%about 80% (5x)
Pruning + fine-tuningabout 85%about 93% (14x)
Iterative pruning + fine-tuningabout 92% (12.5x)about 95.5% (22x)
How far each curve goes before the loss becomes visible
Pruned awayAccuracy loss
87.5%+0.1
89%+0.05
90%0.0
91.5%-0.3
92.5%-0.6
93.5%-1.0
94.5%-2.0
95.5%-4.1
Iterative pruning points read off the slide 51 chart

Notice where the curve begins. It does not start at 40% like the others. The paper explains why in section 5: the biggest gain comes from iterative pruning, where the pruned and retrained network of the green curve is pruned and retrained again. The leftmost red dot corresponds to the point on the green line at 80% (5x) pruned further to 8x. There is no accuracy loss at 9x, and not until 10x does the accuracy begin to drop sharply.

Worked example

The 5x to 9x claim in parameters

  1. Single-step tolerance

    5x of 61M leaves 12.2M weights, which is 80% pruned away.
  2. Iterative result

    9x leaves about 6.8M; the paper's measured count is 6.7M (61 / 6.7 = 9.1x, quoted as 9x), at about 89% pruned away.
  3. What the extra iterations removed

    12.2M - 6.8M = 5.4M further weights, 44% of what single-step pruning had kept, at no accuracy cost.
  4. Where it stops

    10x would be 6.1M weights, and there the paper reports the accuracy beginning to drop sharply.

The same paper reaches 13x on VGG-16 with five iterations of pruning and retraining, shrinking it to 7.5% of its original size with fc6 and fc7 each pruned to less than 4% of their original size. These pruned networks are the starting point of Deep Compression, which quantizes the surviving weights to 8 bits in convolution layers and 5 bits in fully connected layers and Huffman codes the result to reach 35x on AlexNet (240 MB to 6.9 MB) and 49x on VGG-16. The roughly 90% Weight sparsity that EIE assumes in the next part is this number.

Quick check

On AlexNet, iterative pruning raised the achievable pruning ratio from what to what?

Recall

Quote the AlexNet number for iterative versus single-step pruning and convert both to percent pruned.

5x to 9x, which is 80% and 88.9% pruned away. 61M to 6.7M parameters, top-5 error 19.73% to 19.67%.

Fine-tuning repairs the network after the cut. Regularization (for pruning) prepares it before the cut. Follow one weight w = 0.05 through training with a penalty coefficient lambda = 0.01 and a learning rate eta = 0.1, ignoring the data loss for the moment so only the penalty acts.

Worked example

One weight under an L1 and an L2 penalty

  1. L1 pull

    The gradient of lambda |w| is lambda sign(w), so each step subtracts eta lambda = 0.001 regardless of how small w is. From 0.05 the weight reaches exactly zero in 50 steps and stays there.
  2. L2 pull

    The gradient of lambda w^2 is 2 lambda w, so each step subtracts eta 2 lambda w = 0.002 w. The weight is multiplied by 0.998 every step: after 50 steps 0.05 x 0.998^50 = 0.0452, after 1000 steps 0.0068, never zero.
  3. Two different destinations

    L1 subtracts a constant and lands on zero. L2 multiplies by a constant and only approaches it.
StepsL1L2
00.05000.0500
500.00000.0452
2000.00000.0335
10000.00000.0068
w after n steps with only the penalty acting
Five weights slide down each penalty. Under |w| every dot reaches the vertex at zero and lands inside the pruning threshold band; under w squared each dot stops at 30 percent of where it started, and the large ones stay outside the band.

The two regularized losses

The slide states the general rule. During training, or during the fine-tuning of a pruned network, a penalty is added to the loss to penalize nonzero parameters and to encourage smaller ones. The two common choices are the L1 and L2 penalties on the weights W, with lambda setting how strongly the penalty competes with the data loss L(x; W).

L=L(x;W)+λWL' = L(\mathbf{x};\mathbf{W}) + \lambda\,|\mathbf{W}|
L1 regularization: the penalty is the sum of absolute values
L=L(x;W)+λW2L' = L(\mathbf{x};\mathbf{W}) + \lambda\,\|\mathbf{W}\|^2
L2 regularization: the penalty is the squared L2 norm, also called weight decay
Lw=Lw+λsign(w)versusLw=Lw+2λw\frac{\partial L'}{\partial w} = \frac{\partial L}{\partial w} + \lambda\,\mathrm{sign}(w) \quad\text{versus}\quad \frac{\partial L'}{\partial w} = \frac{\partial L}{\partial w} + 2\lambda w
The gradients explain the behavior: L1's pull is constant near zero, L2's vanishes

Goodfellow, Bengio and Courville make both halves precise. L2 regularization, commonly known as weight decay, drives the weights closer to the origin; its update multiplicatively shrinks the weight vector by a constant factor on each step (their equation 7.5). L1 regularization, in comparison, results in a solution that is more sparse, where sparsity means that some parameters have an optimal value of exactly zero, and this property is why L1 has been used extensively for feature selection (LASSO). The gradient forms above are the reason. Near zero the L1 gradient lambda sign(w) keeps its full size, so the weight is pushed onto zero and pinned there. The L2 gradient 2 lambda w shrinks with the weight, so the push fades before it arrives.

L1L2
Gradient of the penaltylambda sign(w), constant2 lambda w, shrinks with w
Effect per stepSubtract a fixed amountMultiply by a factor below one
Where weights endExactly zero, then staySmall but never zero
Accuracy before retraining (Han et al.)BetterWorse
Accuracy after retraining (Han et al.)WorseBetter, best overall
L1 versus L2 as preparation for magnitude pruning

Why both help pruning, and why L2 won anyway

Either penalty shrinks the weights the data loss does not defend. That does two things for magnitude pruning. The ranking becomes a cleaner signal, because a weight that stayed large did so against a constant pull, and the mass of the distribution near zero grows, so more weights fall under the threshold at a given accuracy. L1 goes further and parks weights on exactly zero, so removing them changes nothing at all.

It is tempting to conclude that L1 must be the better choice for pruning, and this is the classic trap. Han et al. (section 3.1) report that L1 regularization does give better accuracy after pruning but before retraining, since more parameters sit near zero. However, the remaining connections are not as good as with L2, resulting in lower accuracy after retraining, and overall L2 regularization gives the best pruning results. They also tried L1 for the pruning phase followed by L2 for retraining and found it did not beat using L2 for both, because parameters from one mode do not adapt well to the other. That is why the slide says magnitude-based fine-grained pruning applies L2 regularization on weights.

Network Slimming: the same penalty on channel scaling factors

The second example on the slide moves the penalty from weights to channels, which connects back to the scaling-based criterion of lecture 04-1. Network Slimming (Liu et al., ICCV 2017) notes that every batch normalization layer already computes z_out = gamma z_hat + beta per channel, so gamma is a free scaling factor for that channel. Their training objective adds a sparsity penalty on those factors.

L=(x,y)l(f(x,W),y)+λγΓg(γ),g(s)=sL = \sum_{(x,y)} l\big(f(x, W), y\big) + \lambda \sum_{\gamma \in \Gamma} g(\gamma), \qquad g(s) = |s|
Network Slimming objective (Liu et al. 2017, equation 1): an L1 penalty on the batch norm scaling factors

Channels whose gamma has been pushed toward zero contribute almost nothing and are removed with a global percentile threshold (pruning 70% of channels means choosing the 70th percentile of all gamma values), followed by fine-tuning. Because a whole channel goes, this is Channel pruning, the coarse end of Pruning granularity, and the resulting network stays dense. On VGGNet the method reports a 20x smaller model and 5x fewer computing operations. The paper also describes a multi-pass scheme that repeats train, prune and fine-tune, the same loop as the previous concepts, now driven by gamma instead of |w|.

Quick check

Why does an L1 penalty drive weights exactly to zero while L2 only shrinks them?

Quick check

Network Slimming applies its sparsity penalty to which quantity?

Recall

Write the L1 and L2 regularized losses and say which one produces exact zeros.

L' = L(x; W) + lambda |W| and L' = L(x; W) + lambda ||W||^2. L1: its gradient lambda sign(w) is constant near zero, so weights land on zero. L2: its gradient 2 lambda w vanishes as w shrinks, so weights only get smaller.

Recall

Which regularizer did Han et al. find best overall for pruning, and what does Network Slimming penalize?

L2 on the weights (L1 is better before retraining, worse after). Network Slimming puts an L1 penalty on the batch normalization scaling factors gamma of each channel.

Recap

If you remember nothing else

  • Pruning without retraining starts losing accuracy once only one third of the original connections remain (about 67 percent pruned). One prune plus fine-tune holds to about 80 percent, and the full retraining scheme reaches one tenth remaining (Han et al.).
  • Fine-tuning retrains only the survivors, with the mask fixed, at 1/10 to 1/100 of the original learning rate. AlexNet took 173 hours to retrain versus 75 hours to train.
  • One iteration is prune then fine-tune. Iterative pruning raises the target sparsity each round and lifted AlexNet from 5x to 9x (61M to 6.7M parameters, top-5 error 19.73 to 19.67 percent).
  • On the curve, iterative pruning stays within 0.5 percent loss to about 92 percent pruned and reaches -4 percent only near 95.5 percent; single prune plus fine-tune holds zero loss only to 80 percent (5x).
  • L1 (lambda |W|) parks weights at exact zero; L2 (lambda ||W||^2) shrinks every weight multiplicatively. Both make small weights cheaper to remove.
  • Han et al. found L2 best overall for magnitude pruning; Network Slimming puts an L1 penalty on batch norm scaling factors to select channels, and its multi-pass scheme is the same loop.
  • Deep Compression and EIE (next part) start from exactly this 9x to 13x pruned network.

Sources