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
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.
Small at first, big in the middle, small at the end
Linear warmup, then cosine decay
Reading off the schedule
The schedule in numpy
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
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.
Bytewise
All courses