Cosine Learning Rate Schedule › The Cosine Decay Formula

Michael BrenndoerferJanuary 26, 202610 min read
Language AI HandbookAll chapters
Part 32 of 67Training Optimization
Chapter 233Cosine Learning Rate Schedule › The Cosine Decay Formula
Reading section 4 of 14 · The Cosine Decay Formula · 10 minSection 4 of 14 · 10 min

Book contents

Section 4 of 14 · 10 min read

Cosine Learning Rate Schedule › The Cosine Decay FormulaOne section · Sections in this chapter4 / 14 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The cosine schedule derives its shape from the cosine function, which decreases smoothly from 1 to −1-1 over a half-period. By remapping this interval to span from the initial learning rate down to a minimum value, we get a decay profile that is gentle near the boundaries and steeper in the middle.

Cosine Annealing

Cosine annealing refers to any learning rate schedule that uses the cosine function to interpolate between a maximum and minimum value. The term "annealing" comes from the metallurgical process of slowly cooling a material to reduce defects, analogizing the cooling of the learning rate to stabilize model parameters.

Basic Cosine Decay

We want a function that starts at ηmax⁡\eta_{\max} when t=0t = 0 and ends at ηmin⁡\eta_{\min} when t=Tt = T, following a smooth cosine curve in between. The standard cosine decay formula achieves this as follows:

η(t)=ηmin⁡+12(ηmax⁡−ηmin⁡)(1+cos⁡(πtT))\eta(t) = \eta_{\min} + \frac{1}{2}(\eta_{\max} - \eta_{\min})\left(1 + \cos\left(\frac{\pi t}{T}\right)\right)

where:

  • η(t)\eta(t): the learning rate at step tt
  • ηmax⁡\eta_{\max}: the peak learning rate at the start of the schedule (after any warmup period)
  • ηmin⁡\eta_{\min}: the floor learning rate, often set to 0 or a small fraction of ηmax⁡\eta_{\max} such as 10−510^{-5}
  • tt: the current step index, ranging from 0 to TT
  • TT: the total number of decay steps
  • cos⁡(πt/T)\cos(\pi t / T): the cosine term that drives the smooth interpolation from 1 (at t=0t=0) to −1-1 (at t=Tt=T)

The expression 12(1+cos⁡(⋅))\frac{1}{2}(1 + \cos(\cdot)) is sometimes called the "raised cosine" because it shifts and scales the cosine from its natural range [−1,1][-1, 1] up into the range [0,1][0, 1]. Multiplying by (ηmax⁡−ηmin⁡)(\eta_{\max} - \eta_{\min}) stretches this unit interval to the desired learning rate range, and adding ηmin⁡\eta_{\min} lifts the floor.

To see why this particular construction works, think of it as a two-step transformation. The raw cosine cos⁡(πt/T)\cos(\pi t / T) takes values in [−1,1][-1, 1]: it equals +1+1 at t=0t = 0, passes through 00 at t=T/2t = T/2, and reaches −1-1 at t=Tt = T. We want a multiplier that goes from 1 to 0 (not from 1 to −1-1), so we add 1 and divide by 2 to get 1+cos⁡(πt/T)2\frac{1 + \cos(\pi t / T)}{2}. This quantity ranges from 1 down to 0. Multiplying by (ηmax⁡−ηmin⁡)(\eta_{\max} - \eta_{\min}) scales that unit range to match the desired learning rate range, and adding ηmin⁡\eta_{\min} ensures we never go below the floor.

Verification at boundary values. Let us confirm the formula produces the right values at t=0t = 0 and t=Tt = T:

At t=0t = 0:

η(0)=ηmin⁡+12(ηmax⁡−ηmin⁡)(1+cos⁡(0))=ηmin⁡+12(ηmax⁡−ηmin⁡)(1+1)=ηmin⁡+(ηmax⁡−ηmin⁡)=ηmax⁡\begin{aligned} \eta(0) &= \eta_{\min} + \frac{1}{2}(\eta_{\max} - \eta_{\min})\left(1 + \cos(0)\right) \\ &= \eta_{\min} + \frac{1}{2}(\eta_{\max} - \eta_{\min})(1 + 1) \\ &= \eta_{\min} + (\eta_{\max} - \eta_{\min}) \\ &= \eta_{\max} \end{aligned}

At t=Tt = T:

η(T)=ηmin⁡+12(ηmax⁡−ηmin⁡)(1+cos⁡(π))=ηmin⁡+12(ηmax⁡−ηmin⁡)(1−1)=ηmin⁡+0=ηmin⁡\begin{aligned} \eta(T) &= \eta_{\min} + \frac{1}{2}(\eta_{\max} - \eta_{\min})\left(1 + \cos(\pi)\right) \\ &= \eta_{\min} + \frac{1}{2}(\eta_{\max} - \eta_{\min})(1 - 1) \\ &= \eta_{\min} + 0 \\ &= \eta_{\min} \end{aligned}

The schedule begins at ηmax⁡\eta_{\max} and ends exactly at ηmin⁡\eta_{\min}, as intended. At the midpoint t=T/2t = T/2, the cosine evaluates to cos⁡(π/2)=0\cos(\pi/2) = 0, giving a learning rate of exactly the average (ηmax⁡+ηmin⁡)/2(\eta_{\max} + \eta_{\min}) / 2.

The Decay Rate at Each Phase

One of the most instructive ways to understand the cosine schedule is to look at the derivative of η(t)\eta(t) with respect to tt. The derivative tells us how fast the learning rate is falling at each step:

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

The sine function starts at zero, rises to its maximum at t=T/2t = T/2, and falls back to zero at t=Tt = T. This means the learning rate is decreasing most rapidly at the midpoint of training, and barely changing at all at the very beginning and very end. Compare this with linear decay, where the derivative is a constant −(ηmax⁡−ηmin⁡)/T-(\eta_{\max} - \eta_{\min}) / T at every step: the rate of decrease is the same regardless of how far along training has progressed.

This derivative structure is exactly what we want. At the start of training, gradients are large and noisy, and a slowly-changing learning rate gives the optimizer time to orient itself without the additional disruption of a rapidly shifting step size. Near the end of training, the learning rate itself is already very small, and a nearly flat derivative means the optimizer is making barely-perceptible adjustments to the rate, allowing for very fine-grained convergence. The middle phase, where the learning rate is most actively shrinking, corresponds to the period when the optimizer is making the transition from broad exploration to local refinement.

Why Smooth Decay Matters

Step decay schedules drop the learning rate by a fixed factor at discrete checkpoints. This creates a jarring discontinuity each time the rate drops: the optimizer is cruising along at one scale and suddenly the effective step size contracts by an order of magnitude. In practice, these drops often coincide with sudden sharp decreases in training loss followed by a plateau at the new rate. While this works, it makes scheduling sensitive to the choice of when to drop.

After each step-decay drop, the loss tends to spike briefly as the optimizer adjusts to the new scale, then quickly drops to a lower floor than before. This "shock and reset" behavior works, but it suggests the model is doing a kind of forced convergence at each checkpoint rather than settling smoothly. The timing of drops has to be chosen carefully: too early and the model has not finished benefiting from the higher rate; too late and it has been oscillating around a minimum without making progress.

Linear decay, covered in the Learning Rate Decay chapter, avoids this problem with a straight-line decrease. However, linear decay is fastest right at the start, when the model may still be in a phase where slightly larger steps would help exploration, and it decreases at a uniform rate that ignores the characteristic phases of learning.

Cosine decay allocates decay budget differently. The rate decreases slowly at first, giving the model time to make larger initial progress. It decelerates more rapidly through the middle of training when parameters are converging broadly. Near the end, the rate again slows its descent, allowing the model to settle delicately into a local minimum without overshooting. This pacing matches the natural phases of gradient descent quite well.

There is also a practical advantage: cosine decay requires no manual checkpoint selection. With step decay, you choose specific milestones, and if they are wrong for your architecture, problem, or batch size, performance suffers. The cosine schedule adapts its budget continuously and automatically based solely on the current step fraction t/Tt/T.

Out[3]:
Visualization
Line chart comparing cosine, linear, and step decay learning rate schedules over 1000 steps.
Comparison of cosine, linear, and step decay learning rate schedules over 1,000 training steps. Cosine decay stays higher than linear in the first half of training, supporting broader exploration, and drops below linear in the second half, enabling finer convergence. Step decay creates three discrete drops at fixed checkpoints, visible as vertical discontinuities. All schedules start at the same peak and finish at the same floor.

The Raised Cosine: A Worked Numerical Example

To make the formula concrete, let us trace through the exact values at several checkpoints for a schedule with ηmax⁡=3×10−4\eta_{\max} = 3 \times 10^{-4}, ηmin⁡=3×10−5\eta_{\min} = 3 \times 10^{-5}, and T=1000T = 1000 steps. These values are typical for training a medium-sized language model with AdamW.

At t=0t = 0: cos⁡(0)=1\cos(0) = 1, so η=3×10−5+12(2.7×10−4)(2)=3×10−4\eta = 3 \times 10^{-5} + \frac{1}{2}(2.7 \times 10^{-4})(2) = 3 \times 10^{-4}. This is the peak, as expected.

At t=250t = 250 (25% through training): cos⁡(π⋅250/1000)=cos⁡(π/4)≈0.707\cos(\pi \cdot 250 / 1000) = \cos(\pi/4) \approx 0.707. So:

η(250)=3×10−5+12(2.7×10−4)(1+0.707)=3×10−5+12(2.7×10−4)(1.707)=3×10−5+2.30×10−4≈2.60×10−4\begin{aligned} \eta(250) &= 3\times10^{-5} + \frac{1}{2}(2.7\times10^{-4})(1 + 0.707) \\ &= 3\times10^{-5} + \frac{1}{2}(2.7\times10^{-4})(1.707) \\ &= 3\times10^{-5} + 2.30\times10^{-4} \\ &\approx 2.60\times10^{-4} \end{aligned}

The learning rate has decayed from 3×10−43 \times 10^{-4} to approximately 2.60×10−42.60 \times 10^{-4}, a reduction of only about 13% despite being 25% through training. This is the "slow start" effect at work.

At t=500t = 500 (midpoint): cos⁡(π/2)=0\cos(\pi/2) = 0, so η=3×10−5+12(2.7×10−4)(1)=1.65×10−4\eta = 3\times10^{-5} + \frac{1}{2}(2.7\times10^{-4})(1) = 1.65\times10^{-4}. This is exactly the midpoint between ηmax⁡\eta_{\max} and ηmin⁡\eta_{\min}.

At t=750t = 750 (75% through training): cos⁡(3π/4)≈−0.707\cos(3\pi/4) \approx -0.707. So:

η(750)=3×10−5+12(2.7×10−4)(1−0.707)=3×10−5+12(2.7×10−4)(0.293)=3×10−5+3.96×10−5≈6.96×10−5\begin{aligned} \eta(750) &= 3\times10^{-5} + \frac{1}{2}(2.7\times10^{-4})(1 - 0.707) \\ &= 3\times10^{-5} + \frac{1}{2}(2.7\times10^{-4})(0.293) \\ &= 3\times10^{-5} + 3.96\times10^{-5} \\ &\approx 6.96\times10^{-5} \end{aligned}

By 75% of training, the learning rate has fallen to about 6.96×10−56.96 \times 10^{-5}, already very close to the minimum of 3×10−53 \times 10^{-5}. The remaining 25% of training will use a very small, slowly-changing learning rate, which is ideal for final refinement.

At t=1000t = 1000: cos⁡(π)=−1\cos(\pi) = -1, so η=3×10−5+0=3×10−5=ηmin⁡\eta = 3\times10^{-5} + 0 = 3\times10^{-5} = \eta_{\min}.

This worked example illustrates the curve's pacing clearly: the cosine schedule changes slowly near its peak, drops most rapidly around the midpoint, and slows again near the floor. A linear schedule also reaches the midpoint value of 1.65×10−41.65\times10^{-4} at step 500, but it reaches 2.60×10−42.60\times10^{-4} at about step 148 rather than step 250. Cosine therefore spends roughly 70% more steps above 2.60×10−42.60\times10^{-4} in this example; both schedules still spend half of training above the midpoint value.