TL;DR
Cosine annealing is a rule for the learning rate during training. The rate starts at a chosen maximum, falls along the right half of a cosine wave, and reaches a chosen minimum at the last step. It changes a little at every step, so there are no sudden drops. The curve is steep in the middle of training and flat at both ends.
Builds on: Learning Rate, Trigonometric Functions · Leads to: SGD with Warm Restarts
Where it's used
Training image classifiers on a fixed budget. When you know in advance how many training steps you will run, cosine annealing gives a schedule that needs no tuning of drop points. Many recipes for convolutional networks use it in place of step drops, alongside stochastic gradient descent.
Training language models. Large Transformer training runs, often with an adaptive optimizer such as Adam, usually warm up the learning rate over the first few thousand steps and then decay it. The decay is often cosine-shaped, because a smooth decay avoids the jumps a step schedule causes.
Restarting training from a checkpoint. The schedule can be run in repeated cycles, each one a full cosine descent that ends low and then jumps back up. This is the idea behind warm restarts, which the 2016 paper by Loshchilov and Hutter introduced together with cosine annealing.
How it works
Use a schedule with maximum rate , minimum rate , and four steps in total. Label the steps . The idea is to compare the cosine curve with a straight line between the same two endpoints. Each step is one gradient descent update, and the rate is the size of that update's move.
- Set the endpoints. At step the rate is . At step the rate is . Between them, the schedule must pass through values in a chosen shape. A straight line is the simplest choice.
- The straight-line version. Each step removes the same amount, . The rates are . Check: each step subtracts , and the last value is zero.
- The cosine version: a half wave. The cosine of an angle goes from at to at . Map the step to an angle , so gives angle and gives angle . Then runs from down to .
- Scale the wave into the range. Multiply by half the gap, , and add the minimum. The formula is . Check at : , the maximum.
- Evaluate at each step. At , , so . At , , so . At , , so . At , .
- Compare. The cosine rates are . The first and last steps drop by 0.0146 each, less than the straight line's 0.025, and the two middle steps drop by 0.0354 each, more than the straight line's 0.025. That is the shape: a slow start, a steep middle, and a soft landing.
flowchart LR A[Step t] --> B[Angle = pi times t over T] B --> C[Cosine of the angle] C --> D[Scale and shift into the range] D --> E[Learning rate for step t] E --> F[Use it for the update] F --> A
- cosine, T = 10
- straight line
The math
Definition
Let be the largest and smallest learning rates, and let be the number of steps in the schedule. For a step with , the cosine annealing rate is
Read the formula in words: the rate is the minimum, plus half the gap between the maximum and the minimum, times a factor that goes from 2 to 0 as the step goes from 0 to . The factor is the cosine shifted up by 1, so it stays between 0 and 2.
Step 3 in symbols: the angle
The step becomes an angle . At the angle is 0, and at it is . The cosine of an angle runs from 1 to over that range, so the factor runs from 2 to 0 along a smooth curve. The slope of a cosine is zero at its peak and trough, which is why the schedule is flat at both ends.
Step 4 in symbols: the scaling
Multiplying by maps the factor's range of 0 to 2 onto a range of 0 to . Adding moves that range up. So at , , and at , .
Step 5 in symbols: the rate of change
The derivative of the schedule with respect to is
The sine is zero at both ends and largest at . So the rate changes slowest at the start and end, and fastest in the middle. This is the formal version of "steep in the middle, flat at the ends."
Worked examples
Take , , .
- Half the gap: .
- Step : , so the factor is , and .
- Step : the angle is and . The factor is , so .
- Step : the angle is and . The factor is 1, so .
- Step : the angle is and . The factor is , so .
- Step : the angle is and . The factor is 0, so .
Interpretation: the drops between steps are , then , then , then . The two middle drops are equal and the two end drops are equal, which is the symmetry of the cosine about its midpoint.
Take , , . Then .
- Step : the factor is , so .
- Step : the angle is , the cosine is 0, and the factor is 1. So . This is the midpoint of the range.
- Step : the angle is . Its cosine is , so the factor is . Then .
- Step : the angle is , the factor is 0, and .
Interpretation: the floor of 0.01 means the rate never reaches zero, so the last steps still move the weights a little. Compared with the straight line between and , which gives at , the cosine is still higher at step 2 (). The cosine holds its value near the start and falls fastest in the middle, so it is the reverse of the straight line's early pace.
A classifier is trained for 100 epochs with , , and . The figures here are invented for illustration. Half the gap is .
- Epoch 25: the angle is , the cosine is , and the factor is . The rate is .
- Epoch 50: the angle is , the cosine is 0, and the rate is .
- Epoch 75: the angle is , the cosine is , the factor is , and the rate is .
- Epoch 90: the angle is , which is . Its cosine is , so the factor is .
- The rate at epoch 90 is .
Try it: the rate at epoch 90 is 0.0034, and the training run is 100 epochs long. What is the rate at epoch 99, if the angle is with cosine ? Answer: the factor is , so the rate is .
Common mistakes and tradeoffs
It is tempting to think that cosine annealing is a step schedule with many small steps. In fact it changes smoothly at every step, and there are no jumps. The drops are largest in the middle of the run, not at fixed milestones.
It is tempting to think the schedule is tuned automatically. In fact you still choose , , and the total length , and the curve is only as good as those choices.
It is tempting to think that a larger always helps. In fact a rate that is too large makes early training unstable, which is the same overshoot problem described in the learning rate lesson.
It is tempting to think that restarts are free. In fact each restart jumps the rate back up, which can knock a model away from a good region. Some practitioners save a checkpoint before each restart.
When to use it: a fixed training budget, where is known in advance and you want a schedule with no drop points to tune.
When not to use it: a run whose length is open-ended, or a run where you want to cut the rate exactly when a validation score stops improving. A plateau schedule, which lowers the rate only when progress stalls, adapts to the run instead.
Alternatives: step decay (fixed drops at chosen steps), linear decay (the straight line in the plot), early stopping (which ends a run when validation stops improving, instead of shaping the rate), and warm-up followed by any decay, which protects the early steps when the weights are still random.
History
Step schedules, which divide the learning rate by a fixed factor at chosen epochs, were the common practice for training deep networks in the 2010s. They work, but they need the drop points chosen in advance.
In 2016 Ilya Loshchilov and Frank Hutter proposed a schedule that follows a cosine curve between a maximum and a minimum, and they combined it with periodic restarts in a method called SGDR, a variant of stochastic gradient descent. The cosine shape gave a smooth decay without tuned drop points. The restarts are covered in the SGDR lesson.
Recap
- The learning rate controls how far each gradient step moves, and a schedule changes it over the run.
- Cosine annealing makes the rate follow the right half of a cosine wave, from at the start to at the end.
- The formula is flat at both ends and steepest at the midpoint.
- The schedule needs only three choices: the two endpoints and the total length.
- Read next: SGD with Warm Restarts, which repeats the cosine cycle and resets the rate at each restart.
Check yourself
QuestionWhat is the learning rate at the midpoint of a cosine schedule with , , and , at step ?Show answer
At the midpoint the angle is , so the cosine is 0 and the factor is 1. The rate is , which is halfway between the two endpoints.
QuestionCompute the cosine learning rate at step with , , and .Show answer
The angle is . The cosine is 0, so the factor is 1, and the rate is .
QuestionWhy does cosine annealing spend more of its drop in the middle of training than at the start and end?Show answer
The rate is . The derivative of the cosine term is proportional to , which is zero at and and largest at . So the rate changes slowest at the ends and fastest in the middle.
QuestionYou train a model for 300 epochs with cosine annealing and then observe that the validation score stalls at epoch 120, long before the rate reaches its floor. What is one reasonable response, and why?Show answer
A reasonable response is to check whether the rate at epoch 120 is still large enough to keep the weights bouncing around, and to consider a plateau schedule or an earlier end point. Cosine annealing is fixed by its length, so it does not react to a stall. A plateau rule would lower the rate when progress stops, which is better suited to a run whose useful length is not known in advance.
Sources
- Loshchilov and Hutter, "SGDR: Stochastic Gradient Descent with Warm Restarts" (2016): https://arxiv.org/abs/1608.03983
- Learning rate, Wikipedia: https://en.wikipedia.org/wiki/Learning_rate