EasyOptimization

Cosine Annealing LR Scheduler

Optimization

Easy

Problem

Cosine annealing smoothly decreases the learning rate following a half-cosine curve. Unlike linear decay, it slows down the rate of decrease near the start and end, spending more training time at moderate learning rates. This schedule is widely used in vision and language model training.

Given a base learning rate, a minimum learning rate, total steps, and the current step, compute the learning rate.

Formula

lr = min\_lr + \frac{1}{2}(base\_lr - min\_lr)\left(1 + \cos\left(\frac{\pi \cdot current\_step}{total\_steps}\right)\right)

At step 0 the cosine term equals 1, so lr = base_lr. At step = total_steps the cosine term equals -1, so lr = min_lr.

Theory

During training, the learning rate controls the step size of each parameter update. The ideal step size changes over the course of training:

A fixed learning rate is always a compromise. Learning rate schedulers solve this by reducing the rate over time, so you get the best of all phases.


The Problem with Linear Decay

The simplest schedule is linear decay: the learning rate drops at a constant rate from start to finish.

\eta(t) = \eta_{\max} - (\eta_{\max} - \eta_{\min}) \cdot \frac{t}{T}

This works, but it has a suboptimal shape. Linear decay:

Ideally, you want a schedule that:

This is exactly the shape of a cosine curve.


The Cosine Annealing Formula

\eta_t = \eta_{\min} + \frac{1}{2}(\eta_{\max} - \eta_{\min})\left(1 + \cos\left(\frac{t}{T} \cdot \pi\right)\right)

Breaking this apart:

The result is a smooth curve from \eta_{\max} down to \eta_{\min}.


Tracing the Curve Step by Step

With \eta_{\max} = 0.1, \eta_{\min} = 0, T = 100:

At t = 0 (start):

At t = 10 (10% through):

At t = 25 (25% through):

At t = 50 (halfway):

At t = 75 (75% through):

At t = 90 (90% through):

At t = 100 (end):

Compare to linear decay at the same points: 0.1, 0.09, 0.075, 0.05, 0.025, 0.01, 0. Cosine keeps the rate higher for longer in the first half, then drops more steeply.


Cosine vs. Linear: The Key Difference

Looking at how much "training budget" is spent at different learning rate levels:

Linear decay:

Cosine annealing:

Cosine spends more time in the productive regimes (high rates for exploration, low rates for convergence) and less time in the intermediate zone.


Warm Restarts (SGDR)

A powerful extension is cosine annealing with warm restarts (Loshchilov and Hutter, 2017, the "SGDR" paper):

Instead of one long cosine curve, run multiple shorter cycles:

  1. Start at \eta_{\max}, anneal to \eta_{\min} over T_0 steps
  2. Restart: jump the learning rate back to \eta_{\max}
  3. Anneal again, but over T_1 = T_0 \times \text{mult} steps (longer cycle)
  4. Restart again, anneal over T_2 = T_1 \times \text{mult} steps
  5. Continue...

Why restarts help:

With \text{mult} = 2: cycles of length 10, 20, 40, 80, ... steps.


Handling Steps Beyond Total Steps

When the current step t exceeds T, most implementations clamp the learning rate at \eta_{\min}. The cosine formula would extrapolate beyond \cos(\pi), which would start increasing the learning rate again. Clamping prevents this.

Some implementations allow this "overshoot" intentionally as a form of warm restart (a full cosine period instead of half).


Where Cosine Annealing Shows Up

Examples

Example 1

Input
base_lr = 0.1, min_lr = 0, total_steps = 100, current_step = 0
Output
0.1
Explanation
At step zero, the cosine factor selects the base learning rate.

Example 2

Input
base_lr = 0.1, min_lr = 0, total_steps = 100, current_step = 100
Output
0.0

Hints

  1. Convert the step fraction into an angle by multiplying it by pi.
  2. Scale one plus the cosine between the base and minimum rates.

Requirements

Constraints

Starter Code

import math

def cosine_annealing_schedule(base_lr: float, min_lr: float, total_steps: int, current_step: int) -> float:
    """
    Returns the cosine-annealed learning rate for the requested step.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Step 0 (start)public
Step at endpublic