Learning rate schedules: warmup and decay

The learning rate is the number that sets how far gradient descent moves in one step. A learning rate schedule does not keep that number the same for the whole training. It raises it slowly at the start and lowers it later. Pick one of six schedules and see how the learning rate and the loss change.

Short answer. A learning rate schedule is a rule that sets the learning rate at each step number. Far from the bottom, big steps are fast. Near the bottom, big steps keep jumping over it and the loss keeps shaking. So you start big and make it smaller over time (decay). The first few steps are often shaky, so many schedules also start from 0 and climb slowly (warmup). The formula this page builds is warmup + cosine: if t < W: η_max × t ÷ W, after that: η_min + (η_max − η_min) × ½(1 + cos(π·p)).

Pick a schedule with the buttons. The top chart shows the learning rate at each step and the bottom chart shows the loss. The gray dashed line is a constant learning rate at the same η_max. This is a tiny toy model: there is one weight w, and the bowl is loss(w) = w²/2 + w⁴/40, gentle near the bottom and steep on the walls. It starts at w = 3 and takes 100 steps. At each step it adds made-up noise (standard deviation 0.5, from a fixed seed) to the slope to act like a mini-batch. All six schedules get the same noise. Settings: step decay multiplies by 0.1 every 30 steps, exponential decay multiplies by 0.96 every step, cosine ends at η_min = one tenth of η_max, warmup is 10 steps, and reduce on plateau halves the rate when the loss has not beaten its best value for more than 5 steps. A real network has millions of weights and you cannot see the shape of its bowl. Still, the same picture shows up: too big jumps, too small is slow, and a bigger rate shakes more near the bottom.

Definitions

The text below explains with pictures and experiments. The textbook definitions are as follows. t is the step number counted from 0, T is the total number of steps, and L is the loss.

learning rate
The positive number η that gradient descent multiplies the gradient by when it updates a weight. It sets the size of one step, and it is a hyperparameter chosen before training.

w←w−η∂L∂w

learning rate schedule
A rule η(t) that sets the learning rate by step number t (or by epoch) instead of keeping one value. It leaves the optimizer's update rule as it is and changes only the η inside it.
step decay, exponential decay
Schedules that shrink the starting learning rate ηmax by multiplying it again and again by a number γ between 0 and 1. Step decay multiplies once every s steps, and exponential decay multiplies once every step. ⌊ ⌋ means rounding down.

η(t)=ηmaxγ⌊t/s⌋

η(t)=ηmaxγt

cosine schedule (cosine annealing)
A schedule that lowers the learning rate from ηmax to ηmin along half a cosine wave. It is ηmax at t = 0 and ηmin at t = T.

η(t)=ηmin+12(ηmax−ηmin)(1+cosπtT)

warmup (learning rate warmup)
Raising the learning rate from near 0 up to ηmax during the first W steps of training. The warmup + cosine on this page climbs in a straight line from 0, then for the remaining steps uses the cosine schedule with the progress p in place of t ÷ T.

η(t)=ηmaxtW(t<W)

η(t)=ηmin+12(ηmax−ηmin)(1+cosπp),p=t−WT−W(t≥W)

reduce on plateau
A rule that lowers the learning rate by watching the loss instead of following a set formula. If the watched loss (usually the validation loss each epoch) fails to beat its lowest value for more than patience checks, the learning rate is multiplied by a factor between 0 and 1.

1. What a learning rate schedule is

Gradient descent updates the weight at each step with w ← w − η × slope. The slope says which way to go and how steep it is. The learning rate η (eta) is the number it is multiplied by, so it is the size of one step.

A learning rate schedule does not fix η to one number. It is a rule η(t) that changes it with the step number t. Making it smaller over time is called decay. Starting near 0 and raising it for the first few steps is called warmup. A schedule does not change the optimizer (SGD, Adam). It only changes the learning rate number, so it works with any optimizer.

2. When the learning rate is too big or too small

So you start big and make it smaller. Far away, big steps get close fast. Close by, small steps cut the shaking. If the start is too shaky, put a warmup in front. With the same η = 1.5, the second warmup step (η = 0.15) gives 3 − 0.15 × 5.7 = 2.145, and the loss drops to 2.83.

Try this. ① The first view (warmup + cosine, η_max 1) has an average loss of 0.0085 over the last 10 steps. Constant gets 0.21. ② Switch schedules. Step decay gets 0.004, exponential decay 0.002, cosine 0.0084, reduce on plateau 0.0075. Only constant is more than twenty times higher. With step decay, the learning rate drops to one tenth at step 30 and the loss drops right with it. ③ Raise η_max to 1.5. Only warmup + cosine survives, at 0.012. The other five blow up at step 2. ④ Raise it to 2. Now warmup + cosine also blows up, at step 14. Warmup only gets you through the start. It cannot save a rate that is too big even at the bottom. ⑤ Lower it to 0.01. Constant gets 0.39 and warmup + cosine gets 1. It cannot reach the bottom in 100 steps, so changing the schedule does not help. ⑥ Constant at 0.05 gets 0.0043, lower than constant at 1 (0.21). A small rate is slow but shakes less. This bowl is small, so even 0.05 reaches the bottom in 100 steps.

3. Six schedules

t is the step number counted from 0, T is the total number of steps (100 here), and ⌊ ⌋ rounds down to a whole number.

ScheduleFormula or ruleSetting in the toyShape
Constantη = η_maxflat
Step decayη = η_max × γ^⌊t ÷ s⌋s = 30, γ = 0.1stairs going down one level every s steps
Exponential decayη = η_max × γ^tγ = 0.96drops fast at first, then slower and slower
Cosineη = η_min + (η_max − η_min) × ½(1 + cos(π·t ÷ T))η_min = η_max ÷ 10flat at the start and end, drops fast in the middle
Warmup + cosinea straight climb from 0 for W steps, then cosine (section 4)W = 10a short climb, then cosine
Reduce on plateauif the loss has not beaten its best value for more than patience steps, η ← η × factorpatience 5, factor 0.5depends on the loss, so you cannot know it ahead of time

Only reduce on plateau is a rule that watches the loss instead of a formula. In real training it looks at the validation loss once per epoch (one pass over all the data), not every step. The PyTorch docs say models often benefit from cutting the learning rate by a factor of 2 to 10 once learning stops improving.

4. How the formula is built: warmup + cosine

We join three pieces in order: a straight climb from 0, half a cosine wave going from 1 down to −1, and a piece that moves it into 0 to 1 and stretches it between η_min and η_max. We use the same W = 10, T = 100, η_max = 1, η_min = 0.1 as the first experiment. p is how far we are through the steps after warmup: p = (t − W) ÷ (T − W), going from 0 to 1.

Press "Next" to add one piece to the formula (the orange background marks the new piece). The curve changes to match, and the reason shows below.

Try this. ① At first there is only the straight climb from 0 to 1, and the value at t = 5 is 0.5. ② Press "Next" and half a cosine wave joins on, going down to −1 at t = 100. The value at t = 55 is 0. ③ Press again and the curve moves up and gets flatter, now running from 1 to 0. At t = 55 it is 0.5. ④ Last, it stretches to run between 0.1 and 1, so the end stops at η_min = 0.1. At t = 55 it is 0.55. This is the same curve as the top chart of warmup + cosine in the first experiment.

Why cosine? Half a cosine wave changes slowly at both ends. So the rate stays big for a long time at the start, and stays small for a long time at the end. The cosine schedule comes from the SGDR paper, and the PyTorch docs for CosineAnnealingLR write the same formula. The original Transformer paper did not use cosine after warmup. It lowered the rate in proportion to one over the square root of the step number. What comes after warmup is your choice.

5. Work it out by hand

With η_max = 1, η_min = 0.1, W = 10, T = 100, find the learning rate at a few steps.

tPartWorkη
0warmup1 × 0 ÷ 100
5warmup1 × 5 ÷ 100.5
10cosinep = 0, cos 0 = 1, ½(1 + 1) = 1, 0.1 + 0.9 × 11
40cosinep = 30 ÷ 90 = ⅓, cos(π/3) = 0.5, ½(1 + 0.5) = 0.75, 0.1 + 0.9 × 0.750.775
55cosinep = 45 ÷ 90 = 0.5, cos(π/2) = 0, ½(1 + 0) = 0.5, 0.1 + 0.9 × 0.50.55
85cosinep = 75 ÷ 90 ≈ 0.83, cos(5π/6) ≈ −0.866, ½(1 − 0.866) ≈ 0.067, 0.1 + 0.9 × 0.0670.16
100cosinep = 1, cos π = −1, ½(1 − 1) = 0, 0.1 + 0.9 × 00.1

At t = 0 the learning rate is 0, so the first step does not move. The experiment takes 100 steps, t = 0 to 99, so the rate at the last step is a tiny bit above 0.1.

6. Things to know

PropertyIn plain wordsIn the experiment
A safe size has a limitIn a bowl that curves by the same amount a everywhere (a × w² ÷ 2), once η goes past 2 ÷ a, every step jumps farther to the other side and it blows upNear the bottom a = 1, so at η_max 2 even warmup + cosine blows up
The shaking follows the learning rateWhen the slope is noisy, a bigger rate shakes more near the bottom. A constant rate cannot get below a certain levelconstant 1 gets 0.21, constant 0.05 gets 0.0043
The start is the riskiest partThis bowl gets steeper the farther you are from the bottom. The same rate jumps at the start and is fine near the bottom. Warmup gets you past that timeAt η_max 1.5 only the schedule with warmup survives
Too small too early wastes the remaining stepsA smaller rate near the end means a lower floor, but if it gets small early, it barely moves after thatstep decay (0.001 from step 90) ends higher than exponential decay (0.018 at the end)
Most need the full length up frontCosine uses T, step decay uses when to drop, exponential decay sets its ratio to fit the length. Only reduce on plateau decides by watching the lossthe table in section 3

Why warmup works in real networks has more than one explanation. The large-batch paper (see sources) found that optimization is hard early in training and used warmup to get past it. The "bowl that is steep at the start" on this page is a tiny model of that picture. It does not settle the real reason.

7. Which one to use when

ScheduleGoodBadUse it when
ConstantThe simplest: only one number to setShaking stays at the end, so it cannot go loweryou are trying things out, or looking for a good learning rate
Step decayEasy to understand. The drops happen at known steps, so logs are easy to readYou must set when to drop and by how much, and set them again if the training length changesthe number of epochs is fixed, or you are copying an older setup
Exponential decayOne number (γ) gives a smooth decayIt starts dropping right away, so the big-rate part is short, and γ must fit the lengthyou want a small, steady drop at every step
CosineStays big for a long time, then drops smoothly at the end. Only the end value to setYou must know the total steps T ahead, and it is hard to extend midwayyou can fix the training length ahead of time
Warmup + cosineGets past the shaky start, so you can use a bigger peakOne more number (W) to set, and the first steps are slowthe start is shaky, as with Transformers, or you train with large batches
Reduce on plateauNo need to know the length. It lowers the rate by watching the lossA noisy validation loss can make it drop too early, and steps are wasted while it waits to noticethe training length is hard to fix ahead, and you measure the validation loss each epoch

Where it is used

Most code that trains a neural network has a schedule. In PyTorch, torch.optim.lr_scheduler has StepLR (step), ExponentialLR (exponential), CosineAnnealingLR (cosine) and ReduceLROnPlateau (reduce on plateau). The warmup + cosine from the experiment can be written as is with LambdaLR, which takes your own factor function. LambdaLR multiplies the starting learning rate (lr, here η_max) by the factor your function returns.

import math, torch
W, T = 10, 100
def rate_factor(t):                      # factor to multiply η_max by
    if t < W:
        return t / W
    p = (t - W) / (T - W)
    return 0.1 + 0.9 * 0.5 * (1 + math.cos(math.pi * p))

optimizer = torch.optim.SGD(model.parameters(), lr=1.0)    # lr is η_max
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, rate_factor)
for t in range(T):
    ...                                  # compute the loss, loss.backward()
    optimizer.step()
    scheduler.step()                     # move to the next step's learning rate

ReduceLROnPlateau needs to see the loss, so you call it with the validation loss: scheduler.step(val_loss).

Questions

If I use Adam, do I still need a learning rate schedule?

Yes. Adam sizes the step for each weight on its own, but there is still one learning rate multiplied on top, and the schedule changes that number. The original Transformer paper also used Adam with a schedule that first rises in a straight line and then goes down.

How many warmup steps should I use?

There is no fixed answer. The original Transformer paper used 4000 steps. If the loss shoots up or turns into NaN in the first steps, try a longer warmup or a smaller peak. If warmup is too long, fewer steps get to learn with the big learning rate.

What if I want to keep training after a cosine schedule ends?

Cosine is planned for a total number of steps T and reaches η_min right at the end. To train longer, plan again with a bigger T from the start, or raise the learning rate again and bring it back down with another cosine (a warm restart, from the SGDR paper). If you cannot fix the length ahead of time, reduce on plateau is easier.

Words

Words used hereWhat books call it
size of one steplearning rate (η, lr), step size
a rule that sets the learning rate at each steplearning rate schedule, scheduler
making it smallerdecay, annealing
raising it slowly at the startwarmup
drop in stairs · drop by a ratiostep decay · exponential decay
drop in a cosine shapecosine annealing, cosine decay
reduce on plateau, how many steps to waitReduceLROnPlateau, patience
raising it big againwarm restart
step · one pass over all the datastep, iteration · epoch
how much the bowl bendscurvature
jumping farther and fartherdivergence
noise in the slopegradient noise (from stochastic gradients)

Sources

← Weight initializationBatch size and gradient accumulation →