All courses

Learning-rate warmup and decay schedules

Sketch linear warmup followed by cosine decay. Explain why warmup protects the early phase, when Adam's statistics are unreliable, and why decaying the learning rate lowers the final loss.

Builds on: The Adam optimizer

▶ Watch this lesson Start the whole course

What the animation says

One rate isn't enough

Adam needs one number from you: the learning rate, eta. Set it too big, and the first steps can throw the weights somewhere the run never recovers from. Set it too small, and training crawls. Even a good constant value leaves the final loss higher than it could be. So nearly every serious run changes the learning rate over time.

Adam's first step

Look at Adam's very first step. Its averages of the gradient and the squared gradient have seen just one minibatch. After bias correction, the step direction is the gradient divided by its own size. Three parameters here, with gradients of point zero zero three, minus fifty, and point two. Each gets divided by its own absolute value. So on step one, how far does each parameter move?

Warmup: a gentle ramp

Every parameter moves a full eta. The gradient's size doesn't matter, only its sign. And that sign comes from one noisy minibatch, with averages built on almost nothing. Warmup fixes this: start eta near zero and raise it in a straight line, over roughly the first one percent of steps. Peak three times ten to the minus four, a thousand warmup steps: step five hundred gets half, one point five times ten to the minus four. By the time eta is large, Adam's averages can be trusted.

Decay: calm the jitter

Late in training, the weights are near a low region, but every gradient is noisy. So they jitter around the bottom instead of settling. The jitter grows with eta. Big rate, wide bounce. Shrink eta toward a small floor, and the bounce shrinks with it. The loss settles lower than any constant rate would reach.

Cosine decay, by the numbers

After warmup, use cosine decay. Let f run from zero at the end of warmup to one at the last step. Eta equals the floor, plus half the range, times one plus the cosine of pi times f. At f zero, cosine is one: the peak. At f one, cosine is minus one: the floor. Our run: peak three times ten to the minus four, floor ten times smaller, a thousand warmup steps, eleven thousand total. Tap the step where eta sits exactly halfway between peak and floor.

The schedule in numpy

Step six thousand is halfway through the decay. Cosine is zero, so eta is the floor plus half the range: one point six five times ten to the minus four. Here's the schedule in numpy. This branch is warmup: the peak times t over W. These two lines are the cosine decay. Run the loop, and it prints the worked steps. Plotted, the curve stays high, falls fastest in the middle, then flattens on the floor.

Why change the learning rate during training?

With Adam you pick one learning rate $\eta$. Too big a value and the first steps can push parameters into a bad region the run never recovers from. Too small and training crawls. Even a good constant $\eta$ leaves the final loss higher than it could be. Nearly every serious run therefore *changes* $\eta$ over time. What shape works, and why does each part of the shape help?

Small at first, big in the middle, small at the end

**Warmup:** at step 1, bias-corrected Adam gives $\hat m/\sqrt{\hat v} = g/|g| = \pm1$ (ignoring the tiny $\epsilon$). Every parameter moves a full $\eta$, steered by one noisy minibatch. $\hat v$ averages only a few gradients, so the step sizes are untrustworthy. Ramping $\eta$ up keeps steps small until those statistics settle. **Decay:** noisy gradients make parameters jitter around a low region, and the jitter grows with $\eta$. Shrinking $\eta$ late shrinks the jitter, so the loss settles lower.

Linear warmup, then cosine decay

Let $t$ count optimizer steps (not token positions), with $S$ total steps, of which the first $W$ are warmup. $\eta_{max}$ is the peak rate and $\eta_{min}$ the final floor. $$\eta_t = \eta_{max}\,\frac{t}{W}\quad (t\le W)$$ $$\eta_t = \eta_{min} + \tfrac12(\eta_{max}-\eta_{min})\big(1+\cos(\pi f)\big),\; f=\tfrac{t-W}{S-W}$$ $f$ runs from 0 to 1 after warmup. At $f=0$, $\cos=1$ gives $\eta_{max}$. At $f=1$, $\cos=-1$ gives $\eta_{min}$. Typical choices: $W$ is about 1% of $S$, and $\eta_{min}\approx 0.1\,\eta_{max}$.

Reading off the schedule

Let $\eta_{max}=3\times10^{-4}$, $\eta_{min}=3\times10^{-5}$, $W=1000$, $S=11000$. This is a short toy run, so warmup takes a larger share than usual. - $t=500$: $3\times10^{-4}\cdot 0.5 = 1.5\times10^{-4}$ - $t=3500$: $f=0.25$, $\cos(\pi/4)=0.707$. Then $3\times10^{-5}+1.35\times10^{-4}\cdot1.707 = 2.60\times10^{-4}$ - $t=6000$: $f=0.5$, $\cos=0$. Then $3\times10^{-5}+1.35\times10^{-4}=1.65\times10^{-4}$ - $t=11000$: $\cos\pi=-1$, giving $3\times10^{-5}$ The curve stays high for a while, falls fastest in the middle, then flattens.

The schedule in numpy

The function below reproduces the worked example. Use the same function to plot the whole curve.
import numpy as np

def lr(t, eta_max=3e-4, eta_min=3e-5, W=1000, S=11000):
    if t <= W:
        return eta_max * t / W
    f = (t - W) / (S - W)
    return eta_min + 0.5 * (eta_max - eta_min) * (1 + np.cos(np.pi * f))

for t in [500, 1000, 3500, 6000, 11000]:
    print(t, f"{lr(t):.3e}")

Remember

Ramp $\eta$ up linearly over the first ~1% of steps. Adam's early steps are full-size ($\approx\eta$ per parameter) yet built on too few gradients to trust. Then cosine-decay $\eta$ toward a small floor. A smaller $\eta$ means less gradient-noise jitter, so the parameters settle into a lower loss.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.