Search

Search papers and lessons

Cosine Annealing

Level 3 · 10 min read

Cosine annealing lowers the learning rate along half a cosine wave: slowly at first, fastest in the middle, and gently down to a small final value.

On this page

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

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 ηmax⁡=0.1\eta_{\max} = 0.1, minimum rate ηmin⁡=0\eta_{\min} = 0, and four steps in total. Label the steps t=0,1,2,3,4t = 0, 1, 2, 3, 4. 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.

  1. Set the endpoints. At step t=0t = 0 the rate is ηmax⁡=0.1\eta_{\max} = 0.1. At step t=4t = 4 the rate is ηmin⁡=0\eta_{\min} = 0. Between them, the schedule must pass through values in a chosen shape. A straight line is the simplest choice.
  2. The straight-line version. Each step removes the same amount, 0.1/4=0.0250.1 / 4 = 0.025. The rates are 0.1,0.075,0.05,0.025,00.1, 0.075, 0.05, 0.025, 0. Check: each step subtracts 0.0250.025, and the last value is zero.
  3. The cosine version: a half wave. The cosine of an angle goes from 11 at 00 to −1-1 at π\pi. Map the step tt to an angle πt/4\pi t / 4, so t=0t = 0 gives angle 00 and t=4t = 4 gives angle π\pi. Then 1+cos⁡(πt/4)1 + \cos(\pi t / 4) runs from 22 down to 00.
  4. Scale the wave into the range. Multiply by half the gap, 12(ηmax⁡−ηmin⁡)=0.05\tfrac12 (\eta_{\max} - \eta_{\min}) = 0.05, and add the minimum. The formula is ηt=ηmin⁡+0.05 (1+cos⁡(πt/4))\eta_t = \eta_{\min} + 0.05 \, (1 + \cos(\pi t / 4)). Check at t=0t = 0: 0+0.05×2=0.10 + 0.05 \times 2 = 0.1, the maximum.
  5. Evaluate at each step. At t=1t = 1, cos⁡(π/4)=0.7071\cos(\pi/4) = 0.7071, so η1=0.05×1.7071=0.0854\eta_1 = 0.05 \times 1.7071 = 0.0854. At t=2t = 2, cos⁡(π/2)=0\cos(\pi/2) = 0, so η2=0.05\eta_2 = 0.05. At t=3t = 3, cos⁡(3π/4)=−0.7071\cos(3\pi/4) = -0.7071, so η3=0.05×0.2929=0.0146\eta_3 = 0.05 \times 0.2929 = 0.0146. At t=4t = 4, η4=0\eta_4 = 0.
  6. Compare. The cosine rates are 0.1,0.0854,0.05,0.0146,00.1, 0.0854, 0.05, 0.0146, 0. 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
024681000.020.040.060.080.10.12step tlearning rate
  • cosine, T = 10
  • straight line
Both curves go from 0.1 to 0.01. The cosine curve is flat near the ends and steepest at the midpoint, where it crosses 0.055.

The math

Definition

Definition: Cosine annealing schedule

Let ηmax⁡>ηmin⁡≥0\eta_{\max} \gt \eta_{\min} \ge 0 be the largest and smallest learning rates, and let TT be the number of steps in the schedule. For a step tt with 0≤t≤T0 \le t \le T, the cosine annealing rate is

ηt=ηmin⁡+12(ηmax⁡−ηmin⁡)(1+cos⁡ ⁣(πtT)).\eta_t = \eta_{\min} + \tfrac12 (\eta_{\max} - \eta_{\min}) \left(1 + \cos\!\left(\frac{\pi t}{T}\right)\right).

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 TT. The factor 1+cos⁡(πt/T)1 + \cos(\pi t / T) is the cosine shifted up by 1, so it stays between 0 and 2.

Step 3 in symbols: the angle

The step tt becomes an angle θt=πt/T\theta_t = \pi t / T. At t=0t = 0 the angle is 0, and at t=Tt = T it is π\pi. The cosine of an angle runs from 1 to −1-1 over that range, so the factor 1+cos⁡θt1 + \cos\theta_t 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 12(ηmax⁡−ηmin⁡)\tfrac12(\eta_{\max} - \eta_{\min}) maps the factor's range of 0 to 2 onto a range of 0 to ηmax⁡−ηmin⁡\eta_{\max} - \eta_{\min}. Adding ηmin⁡\eta_{\min} moves that range up. So at t=0t = 0, η0=ηmin⁡+(ηmax⁡−ηmin⁡)=ηmax⁡\eta_0 = \eta_{\min} + (\eta_{\max} - \eta_{\min}) = \eta_{\max}, and at t=Tt = T, ηT=ηmin⁡\eta_T = \eta_{\min}.

Step 5 in symbols: the rate of change

The derivative of the schedule with respect to tt is

dηtdt=−π2T(ηmax⁡−ηmin⁡)sin⁡ ⁣(πtT).\frac{d\eta_t}{dt} = -\frac{\pi}{2T}(\eta_{\max} - \eta_{\min}) \sin\!\left(\frac{\pi t}{T}\right).

The sine is zero at both ends and largest at t=T/2t = T/2. 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

Example
Four steps, by hand

Take ηmax⁡=0.1\eta_{\max} = 0.1, ηmin⁡=0\eta_{\min} = 0, T=4T = 4.

  1. Half the gap: 12(0.1−0)=0.05\tfrac12(0.1 - 0) = 0.05.
  2. Step t=0t = 0: cos⁡0=1\cos 0 = 1, so the factor is 1+1=21 + 1 = 2, and η0=0+0.05×2=0.1\eta_0 = 0 + 0.05 \times 2 = 0.1.
  3. Step t=1t = 1: the angle is π/4\pi/4 and cos⁡(π/4)≈0.7071\cos(\pi/4) \approx 0.7071. The factor is 1.70711.7071, so η1=0.05×1.7071≈0.0854\eta_1 = 0.05 \times 1.7071 \approx 0.0854.
  4. Step t=2t = 2: the angle is π/2\pi/2 and cos⁡(π/2)=0\cos(\pi/2) = 0. The factor is 1, so η2=0.05\eta_2 = 0.05.
  5. Step t=3t = 3: the angle is 3π/43\pi/4 and cos⁡(3π/4)≈−0.7071\cos(3\pi/4) \approx -0.7071. The factor is 0.29290.2929, so η3≈0.0146\eta_3 \approx 0.0146.
  6. Step t=4t = 4: the angle is π\pi and cos⁡π=−1\cos\pi = -1. The factor is 0, so η4=0\eta_4 = 0.

Interpretation: the drops between steps are 0.1−0.0854=0.01460.1 - 0.0854 = 0.0146, then 0.0854−0.05=0.03540.0854 - 0.05 = 0.0354, then 0.05−0.0146=0.03540.05 - 0.0146 = 0.0354, then 0.0146−0=0.01460.0146 - 0 = 0.0146. The two middle drops are equal and the two end drops are equal, which is the symmetry of the cosine about its midpoint.

Example
A longer run with a nonzero floor

Take ηmax⁡=0.1\eta_{\max} = 0.1, ηmin⁡=0.01\eta_{\min} = 0.01, T=10T = 10. Then 12(ηmax⁡−ηmin⁡)=0.045\tfrac12(\eta_{\max} - \eta_{\min}) = 0.045.

  1. Step t=0t = 0: the factor is 1+cos⁡0=21 + \cos 0 = 2, so η0=0.01+0.045×2=0.1\eta_0 = 0.01 + 0.045 \times 2 = 0.1.
  2. Step t=5t = 5: the angle is π/2\pi/2, the cosine is 0, and the factor is 1. So η5=0.01+0.045=0.055\eta_5 = 0.01 + 0.045 = 0.055. This is the midpoint of the range.
  3. Step t=2t = 2: the angle is 2π/10=π/5=36°2\pi/10 = \pi/5 = 36°. Its cosine is 0.80900.8090, so the factor is 1.80901.8090. Then η2=0.01+0.045×1.8090=0.01+0.0814=0.0914\eta_2 = 0.01 + 0.045 \times 1.8090 = 0.01 + 0.0814 = 0.0914.
  4. Step t=10t = 10: the angle is π\pi, the factor is 0, and η10=0.01=ηmin⁡\eta_{10} = 0.01 = \eta_{\min}.

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 0.10.1 and 0.010.01, which gives 0.1−0.009×2=0.0820.1 - 0.009 \times 2 = 0.082 at t=2t = 2, the cosine is still higher at step 2 (0.09140.0914). 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.

Example
A realistic 100-epoch run

A classifier is trained for 100 epochs with ηmax⁡=0.1\eta_{\max} = 0.1, ηmin⁡=0.001\eta_{\min} = 0.001, and T=100T = 100. The figures here are invented for illustration. Half the gap is 0.04950.0495.

  1. Epoch 25: the angle is π/4\pi/4, the cosine is 0.70710.7071, and the factor is 1.70711.7071. The rate is 0.001+0.0495×1.7071=0.08550.001 + 0.0495 \times 1.7071 = 0.0855.
  2. Epoch 50: the angle is π/2\pi/2, the cosine is 0, and the rate is 0.001+0.0495=0.05050.001 + 0.0495 = 0.0505.
  3. Epoch 75: the angle is 3π/43\pi/4, the cosine is −0.7071-0.7071, the factor is 0.29290.2929, and the rate is 0.001+0.0495×0.2929=0.01550.001 + 0.0495 \times 0.2929 = 0.0155.
  4. Epoch 90: the angle is 0.9π0.9\pi, which is 162°162°. Its cosine is −0.9511-0.9511, so the factor is 0.04890.0489.
  5. The rate at epoch 90 is 0.001+0.0495×0.0489=0.001+0.0024=0.00340.001 + 0.0495 \times 0.0489 = 0.001 + 0.0024 = 0.0034.

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 0.99π0.99\pi with cosine −0.9995-0.9995? Answer: the factor is 1−0.9995=0.00051 - 0.9995 = 0.0005, so the rate is 0.001+0.0495×0.0005=0.001+0.0000248≈0.001020.001 + 0.0495 \times 0.0005 = 0.001 + 0.0000248 \approx 0.00102.

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 ηmax⁡\eta_{\max}, ηmin⁡\eta_{\min}, and the total length TT, and the curve is only as good as those choices.

It is tempting to think that a larger ηmax⁡\eta_{\max} 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 TT 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 ηmax⁡\eta_{\max} at the start to ηmin⁡\eta_{\min} at the end.
  • The formula ηt=ηmin⁡+12(ηmax⁡−ηmin⁡)(1+cos⁡(πt/T))\eta_t = \eta_{\min} + \tfrac12(\eta_{\max} - \eta_{\min})(1 + \cos(\pi t / T)) 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 ηmax⁡=0.2\eta_{\max} = 0.2, ηmin⁡=0\eta_{\min} = 0, and T=8T = 8, at step t=4t = 4?Show answer

At the midpoint the angle is π⋅4/8=π/2\pi \cdot 4/8 = \pi/2, so the cosine is 0 and the factor 1+cos⁡1 + \cos is 1. The rate is ηmin⁡+12(ηmax⁡−ηmin⁡)×1=0+0.1=0.1\eta_{\min} + \tfrac12(\eta_{\max} - \eta_{\min}) \times 1 = 0 + 0.1 = 0.1, which is halfway between the two endpoints.

QuestionCompute the cosine learning rate at step t=2t = 2 with ηmax⁡=0.1\eta_{\max} = 0.1, ηmin⁡=0\eta_{\min} = 0, and T=4T = 4.Show answer

The angle is π⋅2/4=π/2\pi \cdot 2 / 4 = \pi/2. The cosine is 0, so the factor is 1, and the rate is 0+0.05×1=0.050 + 0.05 \times 1 = 0.05.

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 ηmin⁡+12(ηmax⁡−ηmin⁡)(1+cos⁡(πt/T))\eta_{\min} + \tfrac12(\eta_{\max} - \eta_{\min})(1 + \cos(\pi t / T)). The derivative of the cosine term is proportional to sin⁡(πt/T)\sin(\pi t / T), which is zero at t=0t = 0 and t=Tt = T and largest at t=T/2t = T/2. 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