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.
- 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.
- 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.
- 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.
- 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
- Too big: it jumps. It passes the bottom and lands on the other wall, gets an even bigger slope there, and jumps even farther. In the toy bowl the slope at w = 3 is
3 + 3³ ÷ 10 = 5.7. One step with η = 1.5 (worked out without the noise) gives3 − 1.5 × 5.7 = −5.55, and the loss grows from 6.53 to 39.12. The next step goes even farther and blows up. In real training the loss suddenly shoots up or turns into NaN. - A bit big: the shaking stays. It does not blow up, but near the bottom each step is big, so it keeps jumping over the bottom. Mini-batches make the slope shake a little at every step, and that shake is multiplied by the learning rate. The bigger the rate, the bigger the shaking.
- Too small: it is slow. The steps are small, so it does not reach the bottom within the steps you have. It shakes less, but time runs out first.
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.
| Schedule | Formula or rule | Setting in the toy | Shape |
|---|---|---|---|
| Constant | η = η_max | flat | |
| Step decay | η = η_max × γ^⌊t ÷ s⌋ | s = 30, γ = 0.1 | stairs going down one level every s steps |
| Exponential decay | η = η_max × γ^t | γ = 0.96 | drops fast at first, then slower and slower |
| Cosine | η = η_min + (η_max − η_min) × ½(1 + cos(π·t ÷ T)) | η_min = η_max ÷ 10 | flat at the start and end, drops fast in the middle |
| Warmup + cosine | a straight climb from 0 for W steps, then cosine (section 4) | W = 10 | a short climb, then cosine |
| Reduce on plateau | if the loss has not beaten its best value for more than patience steps, η ← η × factor | patience 5, factor 0.5 | depends 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.
| t | Part | Work | η |
|---|---|---|---|
| 0 | warmup | 1 × 0 ÷ 10 | 0 |
| 5 | warmup | 1 × 5 ÷ 10 | 0.5 |
| 10 | cosine | p = 0, cos 0 = 1, ½(1 + 1) = 1, 0.1 + 0.9 × 1 | 1 |
| 40 | cosine | p = 30 ÷ 90 = ⅓, cos(π/3) = 0.5, ½(1 + 0.5) = 0.75, 0.1 + 0.9 × 0.75 | 0.775 |
| 55 | cosine | p = 45 ÷ 90 = 0.5, cos(π/2) = 0, ½(1 + 0) = 0.5, 0.1 + 0.9 × 0.5 | 0.55 |
| 85 | cosine | p = 75 ÷ 90 ≈ 0.83, cos(5π/6) ≈ −0.866, ½(1 − 0.866) ≈ 0.067, 0.1 + 0.9 × 0.067 | 0.16 |
| 100 | cosine | p = 1, cos π = −1, ½(1 − 1) = 0, 0.1 + 0.9 × 0 | 0.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
| Property | In plain words | In the experiment |
|---|---|---|
| A safe size has a limit | In 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 up | Near the bottom a = 1, so at η_max 2 even warmup + cosine blows up |
| The shaking follows the learning rate | When the slope is noisy, a bigger rate shakes more near the bottom. A constant rate cannot get below a certain level | constant 1 gets 0.21, constant 0.05 gets 0.0043 |
| The start is the riskiest part | This 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 time | At η_max 1.5 only the schedule with warmup survives |
| Too small too early wastes the remaining steps | A smaller rate near the end means a lower floor, but if it gets small early, it barely moves after that | step decay (0.001 from step 90) ends higher than exponential decay (0.018 at the end) |
| Most need the full length up front | Cosine 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 loss | the 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
| Schedule | Good | Bad | Use it when |
|---|---|---|---|
| Constant | The simplest: only one number to set | Shaking stays at the end, so it cannot go lower | you are trying things out, or looking for a good learning rate |
| Step decay | Easy to understand. The drops happen at known steps, so logs are easy to read | You must set when to drop and by how much, and set them again if the training length changes | the number of epochs is fixed, or you are copying an older setup |
| Exponential decay | One number (γ) gives a smooth decay | It starts dropping right away, so the big-rate part is short, and γ must fit the length | you want a small, steady drop at every step |
| Cosine | Stays big for a long time, then drops smoothly at the end. Only the end value to set | You must know the total steps T ahead, and it is hard to extend midway | you can fix the training length ahead of time |
| Warmup + cosine | Gets past the shaky start, so you can use a bigger peak | One more number (W) to set, and the first steps are slow | the start is shaky, as with Transformers, or you train with large batches |
| Reduce on plateau | No need to know the length. It lowers the rate by watching the loss | A noisy validation loss can make it drop too early, and steps are wasted while it waits to notice | the 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 here | What books call it |
|---|---|
| size of one step | learning rate (η, lr), step size |
| a rule that sets the learning rate at each step | learning rate schedule, scheduler |
| making it smaller | decay, annealing |
| raising it slowly at the start | warmup |
| drop in stairs · drop by a ratio | step decay · exponential decay |
| drop in a cosine shape | cosine annealing, cosine decay |
| reduce on plateau, how many steps to wait | ReduceLROnPlateau, patience |
| raising it big again | warm restart |
| step · one pass over all the data | step, iteration · epoch |
| how much the bowl bends | curvature |
| jumping farther and farther | divergence |
| noise in the slope | gradient noise (from stochastic gradients) |
Sources
- Ilya Loshchilov, Frank Hutter, SGDR: Stochastic Gradient Descent with Warm Restarts, arXiv:1608.03983. The cosine schedule and warm restarts.
- Priya Goyal et al., Accurate, Large Minibatch SGD: Training ImageNet in 1 Hour, arXiv:1706.02677. A warmup that gets past hard optimization early in training.
- Ashish Vaswani et al., Attention Is All You Need, arXiv:1706.03762. Adam, with a schedule that rises in a straight line for warmup_steps = 4000 and then drops in proportion to one over the square root of the step number.
- PyTorch docs: CosineAnnealingLR (the cosine formula), ReduceLROnPlateau (cut by 2 to 10 when learning stalls, patience), LambdaLR (starting learning rate × your factor).
← Weight initializationBatch size and gradient accumulation →