TL;DR
Stochastic gradient descent trains a network by taking many small steps, and the learning rate sets how big each step is. SGD with warm restarts lowers the learning rate along a cosine curve to a small value, then jumps it back up to its maximum and starts the decay again. The jump does not reset the weights, which is why the restart is called warm. Loshchilov and Hutter proposed this in 2016 and showed that it reaches a good test error in fewer epochs than the step schedule they compared against.
Builds on: Stochastic Gradient Descent, Learning Rate, Cosine Annealing · Leads to: the SGDR paper
Where it's used
- Training image classifiers on CIFAR. CIFAR-10 has 50,000 training images and 10,000 test images, each 32 by 32 pixels, in 10 classes. CIFAR-100 has the same images in 100 classes. Loshchilov and Hutter trained a wide residual network called WRN-28-10 (28 layers, with a widening factor of 10) on both datasets. Their default run used a budget of 200 epochs. With warm restarts, the test error on CIFAR-10 and CIFAR-100 reached about 4 percent and about 20 percent, respectively, roughly 2 to 4 times faster than that default schedule.
- Ensembles from one training run. At the end of each run the learning rate is small and the model is close to a good answer. Saving the models taken right before each restart, called snapshots, and averaging their predictions gives an ensemble without training extra models. The paper reports this for CIFAR and for a dataset of EEG recordings, where snapshots of a single run lowered a median error of about 9 percent by 1 to 2 percentage points.
- Any run where you want a usable model early. If a training budget may be cut short, a schedule with restarts gives a good model at the end of every run, not only at the end of the budget. The paper calls this anytime performance: how good the model is at any point during training.
How it works
A tiny instance first. Use a maximum learning rate of 1.0, a minimum of 0, and a run length of 4 epochs. An epoch is one full pass over the training set. The paper updates the learning rate after every mini-batch, a small group of examples used for one step; here we update once per epoch to keep the numbers round.
- Start a run. Set the learning rate to the maximum, 1.0. Set a counter
T_cur(epochs since the last restart) to 0. Check: the rate is at its largest, and no epochs have passed. - Train for one epoch. Now
T_cur= 1 out of 4, a quarter of the run. The cosine rule gives a rate of about 0.854. Check: the rate fell, but only a little, and it is still between 0 and 1. - Train for a second epoch.
T_cur= 2, half the run. The rate is exactly 0.5, halfway between 1.0 and 0. Check: the rate is exactly the average of the two ends. - Train for a third epoch.
T_cur= 3. The rate is about 0.146. Check: the drop from 0.5 to 0.146 equals the drop from 0.854 to 0.5, and both are larger than the drops at the two ends of the run, so the rate falls fastest in the middle. - Train for a fourth epoch.
T_cur= 4 = the run length. The rate is 0, the minimum. The run ends. Check: the drop from 0.146 to 0 is small again, so the rate eases to a stop. - Restart. Set
T_curback to 0 and the rate back to 1.0. The weights are not reset; training continues from where it stopped. Check: the first step of the new run uses the rate 1.0. - Keep the right model. The paper treats the final solution of each run, reached when the rate is at its minimum, as the recommended model. Its figures plot only these solutions. Models saved just before the restarts can also be kept as snapshots for an ensemble, as the ensembles case above describes.
- learning rate
The math
Within run , after epochs since its start, the learning rate is where and are the largest and smallest rates allowed in run , and is the length of run in epochs. When reaches , the run ends and a new run starts with .
Symbols in words: is the learning rate at the current step; is the cosine function; is the circle constant, about 3.14159. The fraction runs from 0 at the start of a run to 1 at its end.
Step 2 in symbols
After one epoch of a run with , , so . Then and
Step 4 in symbols
At , , so . The cosine is flat near 0 and near and steep near , so the rate changes most in the middle of the run.
Step 6 in symbols
Restarts can have a fixed length, , or lengths that grow by a factor at each restart, . With and , the run lengths are 1, 2, 4, 8, and the restarts fall at cumulative epochs 1, 3, 7, 15.
Worked examples
- Take , , .
- At , the fraction is . The cosine of is 0.
- Substitute: .
- At , the fraction is 1. The cosine of is .
- Substitute: .
- Interpretation: the rate starts at 1, is halfway at epoch 2, and reaches 0 at the end of the run.
The paper's Figure 1 uses a maximum of 0.05 and a minimum of 0. Take a single run with epochs.
- At , the fraction is . The cosine of is .
- Compute the factor .
- Multiply by : .
- At , the fraction is , the cosine is 0, so .
- At , .
- Interpretation: the rate falls from 0.05 to 0.0427 in the first quarter, which is about 15 percent of the maximum, and it is still falling fastest in the middle of the run.
The paper's Table 2 reports test errors (lower is better) on CIFAR-10 for ensembles built from snapshots. The single-model baseline, one run with one snapshot, has a median error of 4.03 percent over 16 runs. One run with three snapshots gives 3.51 percent. Three independent runs with three snapshots each give 3.25 percent.
- Improvement from snapshots in one run: percentage points.
- Improvement from adding two more independent runs: percentage points.
- Interpretation: the first gain comes from free snapshots within one run; the second comes from paying for two more full runs and is smaller.
Try it: on CIFAR-100, the baseline is 19.57 percent and the best ensemble in the table is 16.21 percent, with 16 runs of three snapshots each. How many percentage points lower is the best ensemble? Answer: percentage points.
Common mistakes and tradeoffs
It is tempting to think a restart starts training from scratch with random weights. In fact the weights are kept and only the learning rate jumps up, because the restart is a way to keep improving the model that already exists, not a fresh start.
It is tempting to think that snapshots taken one after another at the end of training add diversity to an ensemble. In fact the paper found that snapshots at epochs 148, 149 and 150 did not improve the results, while snapshots taken right before restarts, at epochs 30, 70 and 150, did, because models taken at different restarts differ more.
It is tempting to think restarts make each epoch cheaper. In fact each epoch costs the same; the gain is in how many epochs it takes to reach a good error, as the paper's experiments measure.
Use warm restarts when you want a usable model at many points in training, when you want snapshot ensembles at no extra training cost, or when the training budget is uncertain. A single run of 200 epochs with no restarts gave the paper's best final error, but its anytime performance was the worst until the last few epochs, so it is a poor choice when early models matter. Alternatives are a step schedule, which drops the rate by a fixed factor at set epochs, as in the baseline the paper compared against, and a plain cosine decay with no restarts. The paper also reports that the default schedule's test error rose after about epoch 120, a sign of Overfitting, while SGDR showed only very mild overfitting.
History
Restart techniques were common long before deep learning. Gradient-free search methods restart from new starting points to find several good answers in a landscape with many peaks. Gradient-based methods also used restarts. The paper notes that O'Donoghue and Candes showed restarts give an acceleration on smooth functions, including when they are applied to accelerated gradient schemes.
For training deep networks, the standard practice was a step schedule that divided the learning rate by a fixed constant at set epochs. Loshchilov and Hutter adapted the restart idea to stochastic gradient descent in 2016 and tested it on wide residual networks. A follow-up study by Huang and colleagues used snapshots of the same kind of schedule to build ensembles, and the paper reports that its own ensembles improved on that work. The paper's stated goal is better anytime performance, meaning good models early in training, and its snapshot results were what drew follow-up work.
Recap
- Stochastic gradient descent takes steps whose size is the learning rate.
- Cosine annealing lowers the rate along half a cosine from a maximum to a minimum over one run.
- A warm restart jumps the rate back to its maximum and keeps the weights, so training continues.
- The rate changes fastest in the middle of a run and slowest at the ends.
- Run lengths can grow by a factor each restart, so good models appear early and often.
- Models saved at the end of runs can be averaged into ensembles at no extra training cost.
Read next: the SGDR paper, which gives the full experiments and the ensemble results.
Check yourself
QuestionRecall: what happens to the learning rate and the weights when a warm restart begins?Show answer
The learning rate jumps back up to its maximum, and the weights are kept, so training continues from where the previous run ended. The restart changes only the rate.
QuestionComputation: a run has a maximum of 0.2, a minimum of 0, and a length of 10 epochs. What is the learning rate at ?Show answer
The fraction is , and the cosine of is 0. So .
QuestionWhy: why does the paper take snapshots right before restarts, rather than at any epochs near the end of training?Show answer
Models taken right before restarts come from different points of the cosine cycles, so they differ more, and an ensemble gains from that diversity. Snapshots taken on consecutive final epochs sit close together in training, and the paper found they did not improve the ensemble.
QuestionApplied: with and , the run lengths are 10, 20, 40, and so on. At which epochs, counting from 0, does a new run begin within the first 35 epochs?Show answer
The first run ends at epoch 10, so a new run begins there. The second run is 20 epochs long and ends at epoch 30, so a new run begins there too. The third run starts at epoch 30 and is 40 epochs long, so no further restart falls within the first 35 epochs. Answer: epochs 10 and 30.