Learning Rate Decay › Cosine Annealing
Language AI HandbookAll chapters Learning Rate Decay › Cosine AnnealingOne section · Sections in this chapter6 / 15 ↓
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
Cosine annealing uses a half-cosine curve to reduce the learning rate from a maximum value to a minimum value over a fixed number of steps. It became widely adopted after Loshchilov and Hutter introduced it in 2017 and combined it with warm restarts to create SGDR (Stochastic Gradient Descent with Warm Restarts).
Why Cosine?
The choice of a cosine curve rather than a simple straight line or exponential is motivated by the shape of the decay, not just convenience. At the start of the cosine curve (near ), the cosine function changes slowly, meaning the learning rate stays near its maximum for a while and decreases gently at first. Near the end (as approaches ), the cosine function is near and also changing slowly, meaning the learning rate settles near its minimum and finishes smoothly. In the middle, the cosine passes through its steepest section, driving the sharpest decline.
This shape is intuitively appealing: the model starts at full speed, gradually shifts into a slower and slower rate, and comes to a gentle stop. Contrast this with linear decay, which reduces the learning rate at a constant rate regardless of where training is, and step decay, which drops abruptly. Cosine annealing provides a natural deceleration curve.
The cosine also has a nice mathematical property: the derivative of the cosine schedule is zero at both endpoints. This means the learning rate changes most slowly exactly at the start and end of the schedule, which is often where training is most sensitive to abrupt changes.
Formulation
The basic cosine annealing schedule is:
where:
- : the minimum learning rate (often 0 or a small positive value like )
- : the maximum learning rate
- : the number of steps for one half-cosine period
- : the current step within the period
At , the cosine term is , giving . At , the cosine term is , giving . In between, the rate follows the smooth cosine curve. The factor of ensures the output lies in the range .
Worked Numerical Example
Let us trace through a specific case to build intuition. Suppose , , and .
At :
At (one quarter through):
Since , this gives approximately .
At (halfway):
At (end):
Halfway through training, the learning rate is approximately half of the initial value. The curve is symmetric around the halfway point in a certain sense: the first quarter (epochs 0-25) sees a relatively small drop from 0.1 to 0.086, while the third quarter (epochs 50-75) sees a larger drop from 0.0505 to about 0.016. The decline accelerates in the middle of the schedule.
Warm Restarts
The standard cosine schedule ends at and stays there. Loshchilov and Hutter proposed restarting the schedule after reaching the minimum, cycling back to and annealing again. This is cosine annealing with warm restarts, sometimes written as SGDR or CosineAnnealingWarmRestarts.
The restart strategy has an appealing justification: near the end of each cycle, the low learning rate drives the model into a local minimum. The restart then kicks the model out of that minimum with a higher learning rate, potentially allowing it to find a flatter, more generalizable minimum in the next cycle. The intuition is that flat minima generalize better than sharp minima, and warm restarts encourage exploration of the loss landscape.
Restarts can also be paired with a multiplicative increase in the cycle length. Each successive cycle is made longer by a factor , so the model spends more and more time at low learning rates as training progresses. A typical choice is , doubling the cycle length each restart.
A learning rate schedule that follows a cosine curve from maximum to minimum, then resets to the maximum and repeats. The restart allows the optimizer to escape sharp local minima and explore flatter regions of the loss landscape that tend to generalize better.
The warm restart also provides an elegant approach to model selection. At the end of each cosine cycle, the model has converged to a local minimum with a very small learning rate. This is a natural checkpoint: the model at the end of each cycle can be saved and compared. Since each restart nudges the model toward a different minimum, the ensemble of cycle-end checkpoints can sometimes outperform any single checkpoint. This practice, called snapshot ensembling, was introduced alongside SGDR as a way to get multiple trained models for the cost of one training run.
def cosine_with_restarts(t, eta_max, eta_min, T0, T_mult=1):
T_cur = T0
t_remaining = t
while t_remaining >= T_cur:
t_remaining -= T_cur
T_cur = int(T_cur * T_mult)
return eta_min + 0.5 * (eta_max - eta_min) * (
1 + np.cos(np.pi * t_remaining / T_cur)
)
steps_restart = np.arange(0, 200)
cosine_fixed = [
cosine_with_restarts(t, eta_max=0.1, eta_min=1e-4, T0=50, T_mult=1)
for t in steps_restart
]
cosine_double = [
cosine_with_restarts(t, eta_max=0.1, eta_min=1e-4, T0=25, T_mult=2)
for t in steps_restart
]

The doubling schedule is useful when you are uncertain about total training time: early cycles provide frequent opportunities to explore, while later cycles devote progressively longer intervals to annealing within a basin. In this 200-epoch window, the 200-epoch cycle has only just begun at epoch 175.