Skip to content
mentoor

Topic 3 of 5 · 5 min read

Learning rates, warmup and decay schedules

Goal: Diagnose a too-high or too-low learning rate from a loss curve and propose a warmup and decay schedule.

The learning rate sets the size of every step, and it is the first setting to get right: too high and the loss explodes, too low and training crawls. Schedules change the rate during the run so you can start gently, move fast, then settle.

Check yourself · 1 of 3

A loss curve reads 2.3, 2.1, 9.7, 410, NaN. What is the most likely cause?

Choose one answer

Why it matters

Most failed training runs are a learning rate problem. The Google Research tuning playbook treats it as the most important hyperparameter to tune first. You do not need luck: each wrong rate leaves a distinct curve.

What each rate does

Go back to the one-weight model with x = 2. Each step multiplies the error by 1 - 2 * lr * x^2, which is 1 - 8 * lr.

  • lr = 0.05: factor 0.6. The error shrinks smoothly. Healthy.
  • lr = 0.2: factor -0.6. The weight jumps past the answer each step but still converges. The loss curve bounces.
  • lr = 0.3: factor -1.4. Each step overshoots by more than the last. The loss grows and ends in inf or NaN. Divergence.
  • lr = 0.001: factor 0.992. It converges, but needs hundreds of steps. Crawling.

The safe limit depends on the steepness of the loss, so a rate that works for one model or batch size can fail for another.

Warmup and decay

Modern recipes for transformers use a schedule with two parts:

  1. Warmup. Start near zero and rise linearly to the peak rate over the first few hundred or thousand steps. Early gradients are noisy and the optimizer statistics are still empty, so big early steps can wreck the weights.
  2. Decay. After the peak, lower the rate, often with cosine decay, which follows half a cosine curve down to a small floor. Large steps make progress early; small steps settle into a minimum later. Cosine annealing was popularised by the SGDR paper.
import math
def lr_at(step, total=10_000, warmup=500, peak=3e-4, floor=3e-5):
    if step < warmup:
        return peak * step / warmup
    t = (step - warmup) / (total - warmup)
    return floor + 0.5 * (peak - floor) * (1 + math.cos(math.pi * t))

In PyTorch the same shape comes from schedulers in torch.optim.lr_scheduler. Call optimizer.step() before scheduler.step().

Before and after

Weak: a fine-tune at a constant rate of 1e-3 starts from a pretrained model, and the loss spikes and never recovers; the pretrained knowledge is overwritten. Fixed: peak 2e-5 with 100 warmup steps and cosine decay. Fine-tuning usually takes a much smaller peak rate than training from scratch.

What goes wrong

  • Copying a rate from a different setup. Batch size, model size and optimizer all change the right value.
  • Warmup skipped with Adam-style optimizers. Early loss spikes are common.
  • Decay computed for the wrong number of steps. The rate hits the floor halfway through, or never gets there.

Examples

Reading three loss curves

Run 1: the loss goes 2.3, 2.1, 9.7, 410, NaN. The rate is too high, so lower it by 3x to 10x. Run 2: the loss goes 2.30, 2.299, 2.298 over 500 steps. The rate is too low, so raise it. Run 3: the loss zigzags between 1.1 and 1.6 without settling. The rate is high enough to overshoot, so add decay or lower the peak.

A schedule on paper

Total 10,000 steps, warmup 500, peak 3e-4, floor 3e-5. At step 250 the rate is 1.5e-4. At step 500 it peaks at 3e-4. At the midpoint of the decay it is about 1.65e-4. At step 10,000 it is 3e-5.

Key points

  • Too high a rate makes the loss bounce or grow to NaN; too low makes it fall very slowly.
  • In the one-weight example, rate 0.3 gives a factor of -1.4 per step, so the error grows.
  • Warmup raises the rate from near zero to the peak over the first steps.
  • Cosine decay lowers the rate smoothly to a floor so the run can settle.
  • Fine-tuning a pretrained model needs a much smaller peak rate than training from scratch.

Common mistakes

  • Reusing a learning rate from a different model size or batch size.
  • Skipping warmup and then blaming the data for early loss spikes.
  • Setting the schedule length to a different step count than the run actually uses.
  • Using a from-scratch rate when fine-tuning a pretrained model.

Check yourself · 2 of 3

What does learning rate warmup do?

Choose one answer

Check yourself · 3 of 3

You fine-tune a pretrained model at a constant 1e-3 and the loss spikes then stays high. What is the best next change?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic