Search

Search papers and lessons

SGDR: Stochastic Gradient Descent with Warm Restarts

Ilya Loshchilov, Frank Hutter · 2016

Loshchilov and Hutter (2016) restart SGD's learning rate on a schedule and decay it with a cosine each time, reaching good test error 2 to 4 times faster on CIFAR.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Optimizers
  • Probability
  • Linear algebra
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Loshchilov and Hutter train neural networks with stochastic gradient descent, but they change the learning rate on a schedule. The rate falls along a cosine curve within a run, then jumps back up to its maximum at a "warm restart" and falls again, with each run longer than the last. The authors report that this reaches good test error on CIFAR-10 and CIFAR-100 two to four times faster than the step schedule used at the time, and that snapshots taken before each restart can be averaged into an ensemble at no extra training cost.

Builds on: Stochastic Gradient Descent, Momentum, Cosine Annealing · Leads to: SGD with warm restarts

Why it mattered

Deep networks were then the best-performing method for many classification problems, and training them on large datasets often took several days even on high-performance GPUs, so any speedup had real value. With snapshot ensembles the authors report new state-of-the-art results on CIFAR-10 and CIFAR-100, and they also show gains on a dataset of brain recordings and on a downsampled version of ImageNet.

The paper's practical contribution is a simple recipe. In its own words, the schedule needs only two hyperparameters, the initial learning rate and the total number of epochs. The solution it reports is the one at the end of the last completed run, so the stopping point is fixed by the schedule rather than searched for.

The problem

Stochastic Gradient Descent updates the parameters xtx_t using the gradient computed on a small batch of data, and a Learning Rate ηt\eta_t sets the step size. The learning rate is the main knob in training. A large rate moves quickly but cannot settle into a narrow minimum. A small rate settles but is slow.

The common practice at the time was a step schedule: keep the learning rate constant, then divide it by a fixed constant at set epochs. The drop epochs must be chosen in advance, so the schedule commits to a training length before anything is known about the run. The paper is concerned with anytime performance, meaning how good the model is at any point during training, and a fixed step schedule gives little guidance on that.

Second-order methods such as L-BFGS were not in favour for deep networks because of stochastic gradients, ill-conditioning and saddle points. Adaptive methods such as Adam approximated the inverse curvature in a cheap diagonal way. The paper's narrower aim is anytime performance: plain SGD with momentum, the optimiser behind the best residual networks, can reach good error sooner if its learning-rate schedule is changed.

The authors also draw on earlier restart ideas from gradient-free and gradient-based optimisation. Those restarts reset a search to explore again, or reset an accelerated method when it starts to oscillate. The paper adapts the spirit of a restart to SGD by raising the learning rate, not by starting from scratch.

How it works

The method has one repeating cycle. Think of a tiny run: the maximum learning rate is 0.1, the minimum is 0, and the first run lasts 4 epochs.

  1. Start a run at the maximum. At the first step of the run the learning rate equals its maximum, here 0.1. The parameters continue from where the previous run ended, so the restart does not throw away what was learned. Momentum carries over too.
  2. Decay along a cosine. The rate falls over the run as half a cosine wave. After 1 of the 4 epochs it is about 0.085, after 2 epochs it is 0.05, and after 3 epochs it is about 0.015.
  3. Reach the minimum at the end. At the end of the run the rate is 0, its minimum.
  4. Warm restart. The next run begins at the maximum again, and its length is multiplied by a factor TmultT_{mult}. A setting the paper uses for its ensembles starts with 10 epochs and doubles the length each time, so the restarts come at epochs 10, 30, 70 and 150.
  5. Report a solution. Because the rate is high right after a restart, the parameters can be worse there. The paper therefore reports the solution at the end of the most recent completed run, where the rate is at its minimum, not the last step.
flowchart LR
  A["Run 1: rate falls 0.1 to 0"] --> B["Restart: rate back to 0.1"]
  B --> C["Run 2: twice as long, falls to 0"]
  C --> D["Restart"]
  D --> E["Run 3: falls to 0"]
  E --> F["Report end of last finished run"]

The restart is a rise in the learning rate, not a reset of the parameters. The paper notes that this rise can temporarily worsen performance, which is why the reporting step matters.

The math

Stochastic gradient descent updates the parameters with

xt+1=xt−ηt∇ft(xt)x_{t+1} = x_t - \eta_t \nabla f_t(x_t)

where ∇ft\nabla f_t is the gradient on the tt-th batch and ηt\eta_t is the learning rate. The paper uses SGD with Nesterov momentum, which keeps a velocity vtv_t, starting at zero, and updates it with a momentum rate μt\mu_t that sets how much of the old velocity is kept:

vt+1=μtvt−ηt∇ft(xt),xt+1=xt+vt+1.v_{t+1} = \mu_t v_t - \eta_t \nabla f_t(x_t), \qquad x_{t+1} = x_t + v_{t+1}.
Definition: Cosine annealing within a run

Within restart number ii, which lasts TiT_i epochs, the learning rate is

ηt=ηmini+12(ηmaxi−ηmini)(1+cos⁡(TcurTiπ))\eta_t = \eta^{i}_{min} + \tfrac{1}{2}\left(\eta^{i}_{max} - \eta^{i}_{min}\right)\left(1 + \cos\left(\frac{T_{cur}}{T_i}\pi\right)\right)

where TcurT_{cur} is the number of epochs since the last restart, updated at every batch so it can take fractional values.

Step 1: the start of a run

When Tcur=0T_{cur} = 0, the cosine term is cos⁡(0)=1\cos(0) = 1, so ηt=ηmaxi\eta_t = \eta^{i}_{max}. This is the warm restart: the rate jumps back to its maximum.

Step 2: the decay

As TcurT_{cur} grows, the fraction Tcur/TiT_{cur}/T_i moves from 0 to 1, and the angle moves from 0 to π\pi. The cosine moves from 1 to −1-1, so the factor 12(1+cos⁡)\tfrac{1}{2}(1 + \cos) moves from 1 to 0, and the rate falls smoothly from the maximum to the minimum.

Step 3: the end of a run

When Tcur=TiT_{cur} = T_i, the cosine is −1-1, so ηt=ηmini\eta_t = \eta^{i}_{min}. The paper's recommended solution is taken at this point.

Step 4: the run lengths

With T0T_0 epochs in the first run and multiplier TmultT_{mult}, run ii has length Ti=T0 Tmult i−1T_i = T_0 \, T_{mult}^{\,i-1}, so the restart epochs are cumulative sums of these lengths. For T0=10T_0 = 10 and Tmult=2T_{mult} = 2, the lengths are 10, 20, 40 and 80, and the restarts come at epochs 10, 30, 70 and 150.

Worked examples

Example 1: the rate across one short run (tiny)

  1. Set the maximum ηmaxi=0.1\eta^{i}_{max} = 0.1, the minimum ηmini=0\eta^{i}_{min} = 0, and the run length Ti=4T_i = 4 epochs.
  2. At Tcur=0T_{cur} = 0: the cosine is cos⁡(0)=1\cos(0) = 1, so the rate is 0+12(0.1)(1+1)=0.10 + \tfrac{1}{2}(0.1)(1 + 1) = 0.1.
  3. At Tcur=2T_{cur} = 2: the angle is 24π=π2\tfrac{2}{4}\pi = \tfrac{\pi}{2}, the cosine is 0, so the rate is 12(0.1)(1+0)=0.05\tfrac{1}{2}(0.1)(1 + 0) = 0.05.
  4. At Tcur=4T_{cur} = 4: the angle is π\pi, the cosine is −1-1, so the rate is 12(0.1)(0)=0\tfrac{1}{2}(0.1)(0) = 0.
  5. Interpretation: the rate is high at the start, half the maximum at the middle, and zero at the end, with a smooth fall in between.

Example 2: when the restarts come (standard)

  1. The setting the paper uses for its ensembles on CIFAR is T0=10T_0 = 10 with Tmult=2T_{mult} = 2.
  2. Run 1 has length 10, so the first restart is at epoch 10.
  3. Run 2 has length 10×2=2010 \times 2 = 20, so the second restart is at epoch 10+20=3010 + 20 = 30.
  4. Run 3 has length 20×2=4020 \times 2 = 40, so the third restart is at epoch 30+40=7030 + 40 = 70.
  5. Run 4 has length 40×2=8040 \times 2 = 80, so the fourth restart is at epoch 70+80=15070 + 80 = 150.
  6. These are the epochs the paper uses for its ensemble snapshots, taken right before each restart: 30, 70 and 150.
  7. Interpretation: the gap between restarts doubles, so later runs spend more epochs refining the solution and fewer epochs exploring.

Example 3: how much the ensemble helped (realistic)

  1. The paper's single WRN-28-10 model, the median of 16 runs, had a CIFAR-10 test error of 4.03 percent.
  2. Averaging three snapshots from one run (M = 3) gives 3.51 percent.
  3. The relative reduction is (4.03−3.51)/4.03=0.52/4.03(4.03 - 3.51) / 4.03 = 0.52 / 4.03, which is about 0.129, or 12.9 percent.
  4. Averaging 16 runs with three snapshots each gives 3.14 percent.
  5. The relative reduction from the single model is (4.03−3.14)/4.03=0.89/4.03(4.03 - 3.14) / 4.03 = 0.89 / 4.03, which is about 0.221, or 22.1 percent.
  6. Interpretation: more than half of the total gain (0.52 of 0.89 points) comes from snapshots of one run, which are free in compute, and the rest from independent runs, which cost full training each.

Try it: on CIFAR-100, the single model has error 19.57 percent and the 16-run ensemble has 16.21 percent. What is the relative reduction? Answer: (19.57−16.21)/19.57=3.36/19.57(19.57 - 16.21) / 19.57 = 3.36 / 19.57, which is about 0.172, or 17.2 percent.

Results

CIFAR-10 and CIFAR-100, single models. The paper reproduces the default schedule of the wide residual network WRN-28-10 with an initial rate of 0.1 and 0.05. With 0.05 the median test errors over 5 runs were 4.13 percent on CIFAR-10 and 20.21 percent on CIFAR-100. With warm restarts starting at 10 epochs and doubling, the errors were 4.03 and 19.58 percent. On the wider WRN-28-20, the same restart setting reached 3.74 and 18.70 percent.

Speed. The paper says test errors near 4 percent on CIFAR-10 and near 20 percent on CIFAR-100 are reached about 2 to 4 times faster than with the default schedule. Its introduction states that SGDR needs 2 to 4 times fewer epochs for comparable or better results.

Overfitting. SGDR lowers the training loss faster until about epoch 120. After that the default schedule overfits, and its test error rises. The SGDR runs show only very mild overfitting.

Ensembles. Table 2 reports ensembles of snapshots. One run with one snapshot gives 4.03 and 19.57. One run with three snapshots gives 3.51 and 17.75. Three runs with three snapshots each give 3.25 and 16.64. Sixteen runs with three snapshots each give 3.14 and 16.21, the best numbers reported. These are ensembles of the same architecture, not new architectures.

EEG recordings. On a brain-signal classification dataset, the authors report a median reference error of about 9 percent, which snapshots of one run improved by 1 to 2 points, and snapshots from both settings improved by 2 to 3 points.

Downsampled ImageNet. At 32 by 32 pixels, the best SGDR setting (starting at 10 epochs, doubling, initial rate 0.01) reached a top-1 error of 39.24 percent and a top-5 error of 17.17 percent. The paper compares this with AlexNet's results on full-size ImageNet, 40.7 and 18.2 percent, which used images with about 50 times as many pixels. The authors call these preliminary experiments and note that longer runs and hyperparameter tuning would likely improve the numbers.

Limits. The ImageNet experiment uses 32 by 32 images and is labelled preliminary; the authors say longer runs and tuning would likely improve the numbers. Their experiments use wide residual networks on CIFAR, and the paper itself lists ImageNet and MS COCO as future work. The recommendation to report the end of a run, not the last step, is motivated by the rise in error right after a restart.

Reading guide

  • Section 1 (introduction): needs Stochastic Gradient Descent and Momentum. The four schedules in Figure 1 are the best visual summary of the method, so look at them before the equations.
  • Section 2.1 and 2.2 (related work): the restart ideas from gradient-free search and from accelerated gradient methods. You can skip them on a first reading; the method does not depend on them.
  • Section 3 (the method): needs Cosine Annealing and the Learning Rate. Equation 5 is the formula in the math section above, and the paragraphs around it define the restart and the recommended solution.
  • Section 4.1 and 4.2 (CIFAR experiments): the architecture is a residual network variant, and Trigonometric Functions explain the cosine in the schedule. Table 1 is the core evidence.
  • Section 4.3 (ensembles): needs Ensemble Learning, the idea of averaging the predictions of several models. The table reports the numbers used above.
  • Sections 4.4 and 4.5 (EEG and downsampled ImageNet): supporting experiments with smaller claims.
  • Sections 5 and 6 (discussion and conclusion): the claims about anytime performance and about the two hyperparameters.

Check yourself

QuestionIn SGDR, what happens to the learning rate at a warm restart, and what happens to the parameters?Show answer
The learning rate jumps back up to its maximum. The parameters are not reset: the next run starts from where the previous run ended, so what was learned is kept, and the higher rate lets the search leave that point.
QuestionUsing the schedule with maximum 0.1, minimum 0 and run length 4 epochs, what is the learning rate after 1 epoch?Show answer
The cosine term is cos⁡(π/4)≈0.7071\cos(\pi/4) \approx 0.7071, so the rate is 12(0.1)(1+0.7071)≈0.0854\tfrac{1}{2}(0.1)(1 + 0.7071) \approx 0.0854.
QuestionWhy does the paper report the solution at the end of the last completed run rather than the last step?Show answer
Right after a warm restart the learning rate is high, which temporarily worsens performance. The end of a completed run, where the rate has reached its minimum, is the solution the paper reports.
QuestionA team wants to use snapshots to build an ensemble without extra training time. Which epochs does the paper use, and why are those snapshots useful?Show answer
The paper takes snapshots right before the last restarts, at epochs 30, 70 and 150 for the chosen setting. They are free in compute because they are taken during the runs already being trained. The paper finds that snapshots taken before restarts help, while naively taking the last three epochs of one run (148, 149, 150) did not improve results.

Sources

Read the full paper

Ilya Loshchilov, Frank Hutter, 2016. arXiv:1608.03983