Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 4: Neural Network Foundations

Learning-rate schedules

P4.1440 min●●○ IntermediateReviewed Dec 2, 2026

Why this matters for machine learning

A learning rate that is right for the first hundred updates is usually too big for the last hundred. A schedule changes the rate over time, and it is one of the cheapest settings to improve a training run.

  • Large language models are trained with a warmup followed by a slow decay, so reading a training recipe means reading its schedule. (P10.B2: The pretraining objective and loss curves, coming soon)
  • Training with large batches needs a larger learning rate and a gradual start (warmup). (P4.15: Batch size and training dynamics)
  • A loss curve that is noisy, flat or exploding often points at the learning rate or its schedule. (P4.22: The training recipe and debugging)
  • Searching for good hyperparameters starts with the learning rate, and the range test narrows that search cheaply. (P4.23: Hyperparameter tuning)

Before this: Gradient descent, Stochastic and mini-batch gradient descent, Optimizers

After this lesson you can

  • Say why a constant learning rate leaves SGD wobbling, and why a very small one is slow, using numbers
  • Write step, exponential, cosine, warmup plus cosine and one-cycle schedules and compute their value at a given update
  • Explain what warmup is for, and what is and is not known about why it helps
  • Run a learning-rate range test and pick a peak rate from it, knowing what the test cannot tell you
  • Use PyTorch schedulers in the right order and say whether to step them per update or per epoch

A driver does not use one speed for a whole trip. On the highway she goes fast to cover distance. Near the parking space she slows down, because at high speed she would overshoot it. Training a network has the same two jobs: cover a lot of ground at the start, and place the parameters precisely at the end.

The speed in training is the learning rate η\eta (P1.F3). So far it was one number, picked once. A learning-rate schedule is a rule that gives ηt\eta_t, the rate for update number tt. Where the analogy breaks: a driver sees the parking space, but the optimizer cannot see how close it is. A schedule is a plan made before training starts, from the length of the run and from experience.

Why one rate is not enough

P1.F4 showed that a mini-batch gradient is the true gradient plus noise, and that with a constant η\eta the parameters hover in a noise ball around the minimum instead of settling. Take its one-parameter model: a bowl with curvature λ=1\lambda = 1, and noise in each gradient with variance 11 (P1.F4’s σ2/B\sigma^2/B). The distance dd from the minimum then follows dt+1=(1−η) dt−η ξtd_{t+1} = (1-\eta)\,d_t - \eta\,\xi_t, and once it has settled its spread is

standard deviation of d=η2−η.\text{standard deviation of } d = \sqrt{\frac{\eta}{2-\eta}}.

This is the formula of the P1.F4 Go-deeper box with λ=1\lambda = 1 and noise variance 11. The numbers:

  • η=0.1\eta = 0.1: spread 0.2290.229.
  • η=0.01\eta = 0.01: spread 0.0710.071.
  • η=0.001\eta = 0.001: spread 0.0220.022.

A ten times smaller rate gives about 10≈3.2\sqrt{10} \approx 3.2 times less wobble. So the end of training wants a small η\eta.

The start wants the opposite. Begin 55 units from the minimum with no noise, and each step multiplies the distance by 1−η1-\eta. After 50 steps the distance is 5×0.950=0.0265\times 0.9^{50} = 0.026 with η=0.1\eta = 0.1, but 5×0.9950=3.035\times 0.99^{50} = 3.03 with η=0.01\eta = 0.01: the small rate has barely started. Large η\eta is fast and wobbly, small η\eta is quiet and slow, so a schedule goes large first and small later.

P1.F4 also showed the cost of shrinking too fast: on its example (batch 4, η=0.06\eta = 0.06, 40 epochs) the decayed run ended 0.5070.507 from the minimum and the constant run 0.0790.079 away. A schedule is a design decision with a downside in both directions. The noise ball is one reason to decay. Warmup, below, is a different reason to change η\eta, at the very start.

The menu of schedules

Each schedule below gives ηt\eta_t for update t=0,1,2,…t = 0, 1, 2, \dots out of TT updates in total. The peak rate is η0\eta_0, and a final rate ηend\eta_{\text{end}} is where decay stops.

Step decay. Multiply by a factor γ\gamma (often 0.10.1) every ss updates or epochs:

ηt=η0 γ⌊t/s⌋.\eta_t = \eta_0\,\gamma^{\lfloor t/s \rfloor}.

It is simple, and it was a common recipe in image-classification training. The rate sits flat and then drops suddenly, which shows as a staircase in the loss curve.

Exponential decay. A smooth version of the same idea, with a constant factor per update. Choosing the factor so that the rate arrives at ηend\eta_{\text{end}} after TT updates gives

ηt=η0(ηendη0)t/T.\eta_t = \eta_0\left(\frac{\eta_{\text{end}}}{\eta_0}\right)^{t/T}.

Inverse-time and inverse-square-root decay. The rate ηt=η0/(1+kt)\eta_t = \eta_0/(1+kt) has the c/tc/t shape that satisfies the Robbins–Monro conditions of P1.F4. A slower fall, ηt∝1/t\eta_t \propto 1/\sqrt{t}, was used in the original transformer (Vaswani and others, 2017): the rate rose linearly for 40004000 warmup updates and then decreased in proportion to the inverse square root of the update number. Their formula was dmodel−0.5⋅min⁡(t−0.5, t⋅4000−1.5)d_{\text{model}}^{-0.5}\cdot\min(t^{-0.5},\ t\cdot 4000^{-1.5}), with Adam settings β2=0.98\beta_2 = 0.98 and ε=10−9\varepsilon = 10^{-9}. For dmodel=512d_{\text{model}} = 512 it peaks at 7.0×10−47.0\times 10^{-4} at update 40004000 and is half of that, 3.5×10−43.5\times 10^{-4}, at update 1600016000. This shape has no fixed end, so it suits a run whose length is not known in advance.

Cosine annealing. Follow half a cosine wave from the peak down to a final value:

ηt=ηend+12 (η0−ηend)(1+cos⁡πtT).\begin{aligned} \eta_t &= \eta_{\text{end}} \\ &\quad + \tfrac{1}{2}\,(\eta_0 - \eta_{\text{end}})\left(1 + \cos\frac{\pi t}{T}\right). \end{aligned}

The rate falls slowly at first, fastest in the middle and slowly again at the end, and it is exactly half-way, η0/2\eta_0/2 when ηend=0\eta_{\text{end}} = 0, at t=T/2t = T/2. The cosine form comes from Loshchilov and Hutter (SGDR, arXiv 2016, ICLR 2017). Their paper also proposed warm restarts: after a cycle of TiT_i updates the rate jumps back to the peak and a new cosine cycle starts, usually a longer one. In PyTorch, CosineAnnealingWarmRestarts(T_0=10, T_mult=2) multiplies the cycle length by TmultT_{\text{mult}}, so the restarts fall at updates 1010, 3030 and 7070.

Linear warmup. Start small and raise the rate linearly to the peak over the first ww updates, then hand over to another schedule:

ηt=η0 t+1w(t<w).\eta_t = \eta_0\,\frac{t+1}{w}\qquad (t < w).

Why do this? Two explanations have support, and the picture is not settled:

  • Early gradients can be large and the network changes fast. Goyal and others (2017) argued that the rule “scale η\eta with the batch size” (P4.15) assumes the gradient barely changes between nearby steps, and that this fails at the start of training, when the network changes rapidly. They raised the rate in a straight line over the first 5 epochs, from 0.10.1 to 3.23.2 for a batch of 8192, and found that a flat low rate followed by a sudden jump could make the training error spike.
  • Adam’s statistics start poor. Liu and others (2019) proposed that the adaptive step size of Adam has a very large variance in the first updates, because its squared-gradient average rests on few gradients, and that warmup acts as a way to reduce it. This is an explanation offered with evidence, not a closed question.

Warmup is not a cure for a bad rate, and a small network may not need it. In the widget’s model it is not needed to keep training stable: it only delays the first progress.

Warmup plus cosine. Warmup up to the peak, then a cosine down to a small final value, is a common default for modern training runs. As one documented example, GPT-3 (Brown and others, 2020) used a linear warmup over the first 375 million tokens and a cosine decay down to 10 percent of the peak over 260 billion tokens, after which training continued at 10 percent.

One-cycle. Smith and Topin (2017, “super-convergence”) used a single cycle: the rate rises to a large peak and then falls, in one pass through the training. PyTorch’s OneCycleLR has these defaults: start at η0/25\eta_0/25, rise along a cosine for the first 30 percent of the updates, then fall along a cosine to η0/25/104\eta_0/25/10^4. By default it also moves the momentum the other way, from 0.950.95 down to 0.850.85 at the peak and back, which the widget does not copy. Smith and Topin reported much faster training on some image tasks. Whether it helps on a given problem is something to test.

Here are three shapes side by side for η0=0.1\eta_0 = 0.1 over T=100T = 100 updates. The step decay drops by 0.10.1 every 3030 updates, and the other two end at 0.0010.001 (exponential) and 00 (cosine). The last row is the formula’s value at t=Tt = T, which is one past the final update.

ttstepexponentialcosine
00.10.10.1
250.10.03160.0854
500.010.010.05
750.0010.003160.0146
1000.00010.0010

Two more tools are worth knowing by name. Linear decay to zero is another common ending. ReduceLROnPlateau is the one schedule that looks at the loss: PyTorch’s version multiplies the rate by 0.10.1 when a monitored number (such as the validation loss) has not improved for 10 checks, by default.

The schedule designer

The widget trains one fixed small network (2-8-1 tanh, 200 points in an XOR pattern, plain SGD, batches of 8, 300 updates) with the schedule you build. The network and data are those of the P4.13 cell, with a little less overlap between the blobs. The upper chart is ηt\eta_t and the lower one is the loss on all 200 points. A dashed grey line shows a constant rate equal to the peak, for comparison.

Constant, peak η = 3: the loss after 300 updates is 0.289 (the lowest along the way was 0.185).

Learning rate η at each update

01230100200300update

Loss on all 200 points (lower is better; values above 2 sit on the top line)

00.511.520100200300update

This schedule

Final loss
0.289
Lowest loss
0.185
Wobble
0.047
η at the start
3
η at the end
3

Fixed: a 2-8-1 tanh network on 200 made-up points in an XOR pattern, plain SGD, batches of 8 taken in order, 300 updates. Wobble is the standard deviation of the loss over the last 50 updates. Everything is computed in your browser and is the same every time. One small model shows how the rules behave, not which schedule is best in general.

Pick a schedule and its settings. The top chart is the learning rate at each update, the bottom chart is the loss of a small network trained with it. The dashed grey line is the loss with a constant rate equal to the peak. The second mode runs the learning-rate range test: the rate grows by a fixed factor per update and the chart shows the loss against it. The model, the data and the order of batches are fixed, so every run can be repeated exactly.

Things to try, with the widget’s fixed settings (final η\eta 0.0010.001, warmup 3030 updates, step drop every 100100 updates):

  • A peak that is too big. Constant at η=3\eta = 3 ends at loss 0.2890.289, its lowest value on the way was 0.1850.185, and its last 50 values spread with a standard deviation of 0.0470.047. Step decay ends at 0.1700.170, cosine at 0.1520.152 and one-cycle at 0.1420.142, each with a spread of 0.0010.001 or less. Not every decay helps: exponential ends at 0.3010.301 and warmup plus cosine at 0.1930.193.
  • A good peak. At η=1\eta = 1 constant ends at 0.1600.160 with a spread of 0.0170.017, cosine at 0.1640.164, warmup plus cosine at 0.1520.152 and step at 0.1930.193. The final losses are close, and decay mainly removes the wobble. Warmup is not needed here: the loss falls below 0.50.5 after 4545 updates with warmup and after 3232 without it.
  • A peak that is too small. At η=0.3\eta = 0.3 constant ends at 0.1800.180, cosine at 0.3060.306 and step at 0.4050.405. At η=0.03\eta = 0.03 nothing has trained: every schedule ends between 0.650.65 and 0.680.68, close to the starting loss of 0.6970.697. Decay shrinks a rate that was already modest, so on a short run it costs progress. A schedule does not rescue a poor peak.
  • The floor. A 3000-update cosine run from 0.30.3 down to 00 ends at 0.1190.119, so about 0.120.12 is roughly how low this network gets in that run. The blobs overlap, so some points cannot be classified correctly.
  • Range test. The loss is lowest, 0.3560.356, at η=5.8\eta = 5.8 and goes above 11 at η=7.3\eta = 7.3. The suggestion, a tenth of the lowest-loss rate, is 0.580.58.

The learning-rate range test

How do you pick the peak in the first place? Leslie Smith’s Cyclical Learning Rates (arXiv 2015, WACV 2017) describes a short run with the rate increasing, to find sensible bounds for it. The abstract describes the increase as linear. Many tools now grow the rate by a constant factor (exponentially) so that every decade of rates gets equal attention:

  1. Start from a fresh model and a tiny rate such as 10−410^{-4}.
  2. Run a few hundred updates, multiplying η\eta by a fixed factor each time, up to something like 1010. The widget uses 150 updates, a factor of about 1.081.08 per update.
  3. Record the loss after each update and plot it against η\eta.
  4. The loss is flat while η\eta is too small, falls, reaches a low point, and then rises and blows up.
  5. Choose a peak well below the rate where the loss is lowest. A common rule of thumb is a tenth of it. Some guides pick the rate where the loss falls fastest instead.

Why below the lowest point? In the sweep the rate rises every update, so the loss at a rate has had little time to settle, and a rate that works for a 150-update sweep can still be too big for a run of 15,000 updates. The numbers show how loose the test is: with a sweep of 200 updates instead of 150 (same range) the lowest-loss rate moves from 5.85.8 to 2.82.8. The test gives the right order of magnitude, not an exact answer. In the widget the curve stays almost flat (above 0.640.64) until η\eta is about 11, because this network starts on a plateau, so at the suggested 0.580.58 the sweep’s loss has not fallen yet. A constant η=0.58\eta = 0.58 for 300 updates still ends at 0.1520.152. A sweep shows how the loss responds as η\eta rises, not what a long run would reach. Check the suggested peak with a real short run.

Interactions

  • Batch size (P4.15). A larger batch has less noise and tolerates a larger η\eta. The rule of Goyal and others, scale η\eta in proportion to BB, was paired with a warmup, as above. When the batch size changes, the number of updates per epoch changes too, so a schedule counted in epochs and one counted in updates stop agreeing.
  • Adam (P4.13). Adam does not remove the need for a schedule. With a steady gradient its step is about η\eta, so ηt\eta_t directly sets how far each parameter moves. In AdamW the decay is ηtλ\eta_t\lambda per update, so a schedule scales the weight decay too.
  • The run length. Cosine, one-cycle and step decay need TT in advance. If you stop early, the rate was still large. Inverse-square-root decay and a constant rate do not have this problem.

Schedules in PyTorch

Schedulers live in torch.optim.lr_scheduler. A scheduler wraps an optimizer and changes the lr stored in it each time you call scheduler.step(). The order matters: call optimizer.step() first and scheduler.step() after it. In the other order PyTorch warns that it will skip the first value of the schedule.

The second choice is the unit. StepLR(step_size=3) drops the rate after every 3 calls of scheduler.step(). Called once per epoch, that is every 3 epochs. Called once per batch, it is every 3 updates. Smooth schedules (cosine, one-cycle, warmup) are normally stepped once per update, and OneCycleLR expects that. ReduceLROnPlateau is stepped with the monitored value, scheduler.step(val_loss).

Intuition

A schedule trades speed for precision on a timetable. Early on, big steps cover ground, and the noise matters little because you are far from the minimum and the true gradient is large. Late in training the true gradient can be small next to the noise, so smaller steps are needed to average the noise out. Warmup protects the very first updates, when the network and the optimizer’s statistics are least settled. The range test is a cheap experiment to learn how big a step the loss tolerates.

Math

With TT updates, peak η0\eta_0, final rate ηend\eta_{\text{end}} and update number t=0,…,T−1t = 0, \dots, T-1:

  • Constant: ηt=η0\eta_t = \eta_0.
  • Step: ηt=η0 γ⌊t/s⌋\eta_t = \eta_0\,\gamma^{\lfloor t/s\rfloor}.
  • Exponential: ηt=η0 (ηend/η0)t/T\eta_t = \eta_0\,(\eta_{\text{end}}/\eta_0)^{t/T}.
  • Cosine: ηt=ηend+12(η0−ηend)(1+cos⁡(πt/T))\eta_t = \eta_{\text{end}} + \tfrac12(\eta_0 - \eta_{\text{end}})(1 + \cos(\pi t/T)).
  • Warmup plus cosine: ηt=η0(t+1)/w\eta_t = \eta_0 (t+1)/w for t<wt < w, then the cosine with tt replaced by t−wt - w and TT by T−wT - w.
  • One-cycle (PyTorch defaults): with u=0.3T−1u = 0.3T - 1, rise from η0/25\eta_0/25 to η0\eta_0 along a cosine for t≤ut \le u, then fall along a cosine to η0/(25⋅104)\eta_0/(25\cdot 10^4) at t=T−1t = T-1.
  • Inverse square root (Vaswani and others): ηt=dmodel−1/2min⁡(t−1/2, t w−3/2)\eta_t = d_{\text{model}}^{-1/2}\min(t^{-1/2},\ t\,w^{-3/2}), with w=4000w = 4000.
  • Noise ball (one parameter): spread =η/(2−η)=\sqrt{\eta/(2-\eta)} for curvature 11 and noise variance 11.
  • Range test: ηi=ηlo (ηhi/ηlo)i/(n−1)\eta_i = \eta_{\text{lo}}\,(\eta_{\text{hi}}/\eta_{\text{lo}})^{i/(n-1)} for i=0,…,n−1i = 0, \dots, n-1.

Code

PyTorch schedulers on a dummy optimizer (momentum SGD, peak 0.10.1, 100 updates). The program prints the rate that the update would use, at four update numbers (run locally with PyTorch 2.9.1):

import torch
from torch import nn
from torch.optim import lr_scheduler as S

model = nn.Linear(2, 1)

def lrs(make, steps=100):
    opt = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
    sched = make(opt)
    out = []
    for t in range(steps):
        out.append(opt.param_groups[0]["lr"])  # rate of this update
        opt.step()                             # first the optimizer ...
        sched.step()                           # ... then the scheduler
    return out

warm = 10
make = {
    "StepLR(30, 0.1)":   lambda o: S.StepLR(o, step_size=30, gamma=0.1),
    "CosineAnnealingLR": lambda o: S.CosineAnnealingLR(o, T_max=100),
    "OneCycleLR":        lambda o: S.OneCycleLR(o, max_lr=0.1,
                                                total_steps=100),
    "LambdaLR warmup":   lambda o: S.LambdaLR(
        o, lambda t: (t + 1) / warm if t < warm else 1.0),
}
at = [0, 30, 60, 99]
print("update", *[f"{t:>7}" for t in at])
for name, f in make.items():
    L = lrs(f)
    print(f"{name}")
    print("      ", *[f"{L[t]:7.4f}" for t in at])
update       0      30      60      99
StepLR(30, 0.1)
         0.1000  0.0100  0.0010  0.0001
CosineAnnealingLR
         0.1000  0.0794  0.0345  0.0000
OneCycleLR
         0.0040  0.0999  0.0589  0.0000
LambdaLR warmup
         0.0100  0.1000  0.1000  0.1000

LambdaLR multiplies the starting rate by the number that the function returns, which is how a warmup is written by hand. A warmup followed by a cosine can be built from LinearLR and CosineAnnealingLR joined by SequentialLR. OneCycleLR reaches its peak of 0.10.1 at update 2929 (30 percent of 100100, minus one), so the 0.09990.0999 at update 3030 is just past the top.

A schedule by hand

Peak η0=0.1\eta_0 = 0.1, a warmup of w=10w = 10 updates and a cosine over the remaining 9090 updates, ending at 00.

  • Update 00: 0.1⋅1/10=0.010.1\cdot 1/10 = 0.01.
  • Update 44: 0.1⋅5/10=0.050.1\cdot 5/10 = 0.05.
  • Update 99: 0.1⋅10/10=0.10.1\cdot 10/10 = 0.1, the peak.
  • Update 1010: the cosine starts, with t−w=0t - w = 0, so the rate is 0.10.1 again.
  • Update 5555: t−w=45t - w = 45, which is half of 9090, so the rate is 0.1⋅12 (1+cos⁡(π/2))=0.050.1\cdot\tfrac12\,(1+\cos(\pi/2)) = 0.05.
  • Update 9999: t−w=89t - w = 89, so the rate is 0.05 (1+cos⁡(89π/90))=3.0×10−50.05\,(1+\cos(89\pi/90)) = 3.0\times 10^{-5}.

Try it: four schedules, two peaks

This cell trains the same network as the widget with constant, step, cosine and warmup-plus-cosine schedules, at a peak of 1.01.0 and at a peak of 3.03.0. It prints the final loss and the wobble: the standard deviation of the loss over the last 50 updates. It runs in about a second. Change the peaks, the warmup length or the final rate, or add an exponential schedule.

Try it: four schedules, two peaks

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Running the code gives this (on the machine used for this lesson):

peak eta 1.0: final, wobble
  constant  0.160  0.017
  step      0.193  0.000
  cosine    0.164  0.000
  warm+cos  0.152  0.000
peak eta 3.0: final, wobble
  constant  0.289  0.047
  step      0.170  0.000
  cosine    0.152  0.000
  warm+cos  0.193  0.001

What to notice, and what not to conclude:

  • A constant rate keeps wobbling. At η=3.0\eta = 3.0 its last 50 losses spread with a standard deviation of 0.0470.047, and every decayed schedule has 0.0010.001 or less.
  • The best schedule depends on the peak. At 1.01.0 warmup plus cosine is lowest and the four finals lie within 0.040.04. At 3.03.0 cosine is lowest, and warmup plus cosine ends behind step decay.
  • These are one seed on a small made-up problem. The gap between 0.1520.152 and 0.2890.289 says how the rules behave here, not which one wins on a real network.

Check your understanding

  1. In the one-parameter noise model, the rate is cut from 0.10.1 to 0.010.01. What happens to the spread of the parameter around the minimum once it has settled?

  2. Which statement about warmup is best supported?

  3. A cosine schedule has peak 0.10.1, final rate 00 and T=100T = 100. What is the rate at t=50t = 50?

  4. In a range test the loss is lowest at η=5.8\eta = 5.8. Which peak rate is the better choice for the training run?

  5. A training loop calls scheduler.step() before optimizer.step(). What does PyTorch warn about?

Exercises

  1. A step schedule has η0=0.1\eta_0 = 0.1 and drops by 0.10.1 every 3030 updates. What is the rate at update 7575?
    Show answer⌊75/30⌋=2\lfloor 75/30\rfloor = 2 drops, so 0.1⋅0.12=0.0010.1\cdot 0.1^2 = 0.001.
  2. A cosine schedule has peak 0.10.1, final rate 00 and T=100T = 100. What is the rate at t=25t = 25?
    Show answer0.05 (1+cos⁡(π/4))=0.05⋅1.7071=0.08540.05\,(1+\cos(\pi/4)) = 0.05\cdot 1.7071 = 0.0854, the value in the table above.
  3. A warmup lasts w=500w = 500 updates to a peak of 3×10−43\times 10^{-4}, with the rule ηt=η0(t+1)/w\eta_t = \eta_0 (t+1)/w. What is the rate at update 9999, and at update 499499?
    Show answerAt update 9999: 3×10−4⋅100/500=6×10−53\times 10^{-4}\cdot 100/500 = 6\times 10^{-5}. At update 499499: 3×10−4⋅500/500=3×10−43\times 10^{-4}\cdot 500/500 = 3\times 10^{-4}, the peak.
  4. For the original transformer schedule with 40004000 warmup updates, how does the rate at update 1600016000 compare with the peak?
    Show answerAfter the peak the rate is proportional to t−1/2t^{-1/2}, so at four times the update number it is 1/4=1/21/\sqrt{4} = 1/2 of the peak.
  5. An exponential schedule should go from 0.10.1 to 0.0010.001 in 10001000 updates by multiplying by the same factor γ\gamma at each update. What is γ\gamma?
    Show answerγ1000=0.001/0.1=0.01\gamma^{1000} = 0.001/0.1 = 0.01, so γ=0.011/1000=eln⁡(0.01)/1000=e−0.0046≈0.9954\gamma = 0.01^{1/1000} = e^{\ln(0.01)/1000} = e^{-0.0046} \approx 0.9954.
  6. A model trains for 10 epochs of 500 batches. StepLR(step_size=3) is meant to drop the rate every 3 epochs. After how many updates does the first drop happen if scheduler.step() is called once per batch by mistake, and what was intended?
    Show answerCalled per batch, the first drop comes after 3 updates, since the scheduler counts calls. The intended drop was after 3 epochs, which is 3⋅500=15003\cdot 500 = 1500 updates. Call it once per epoch, or use step_size=1500 with per-update calls.

Next: P4.15 looks at batch size and how it interacts with the learning rate, P4.22 uses loss curves to diagnose training, and P4.23 searches over hyperparameters, starting with the learning rate.

Key takeaways

  • A constant step size makes SGD hover in a noise ball whose size grows with η\sqrt{\eta}, while a tiny step is slow. A schedule starts large for speed and ends small to settle.
  • Step decay multiplies the rate by a factor such as 0.10.1 at fixed points. Exponential decay shrinks it by a constant factor per update. Cosine follows half a cosine wave from the peak down to a final value.
  • Warmup raises the rate linearly from a small value to the peak over the first updates. It is common with large batches, with Adam and in transformers. The reasons are partly understood and partly proposed.
  • Warmup plus cosine decay is a common default for modern training runs. One-cycle goes up for the first 30 percent and then down, with the rate starting at one twenty-fifth of its peak.
  • The learning-rate range test grows the rate exponentially for a short run. Pick a peak well below the rate where the loss is lowest, because the loss turns up soon after.
  • Adaptive optimizers still need a schedule, because their step is still about η\eta. Decay is not free: on a short run a decayed rate can end higher than a smaller constant one.
  • In PyTorch call “optimizer.step()” first and “scheduler.step()” after it, and match the call to the unit the schedule counts in: per update or per epoch.
Found a mistake? Tell us