Papa Programmer
Lesson 17 of 36

Learning rate, warm-up and schedules

9 min read

Optional

What the rate multiplies

Each step computes a gradient, which points towards lower loss, and moves the weights by the gradient times the learning rate, as Loss and gradient descent explained. The rate is the step size. The same gradient gives a small move with a small rate and a large move with a large one.

Fine-tuning rates are much smaller than training from scratch, because the model already works and you want to adjust it, not rebuild it. Typical starting points are around 1e-5 to 5e-5 for full fine-tuning and around 1e-4 to 3e-4 for LoRA adapters. Treat these as places to begin and always check them against the documented example for your model and method.

Too high, too low, about right

The program below runs gradient descent on a loss of (w - 3)², whose best weight is 3, from a starting weight of 0. It tries four rates for 20 steps each.

python
def run(rate, steps=20):
	w = 0.0
	for _ in range(steps):
		w -= rate * 2 * (w - 3)
	return w

for rate in [0.01, 0.4, 0.95, 1.05]:
	print(f"rate {rate:<5} -> w = {run(rate):.3f}   (best is 3)")

It prints:

text
rate 0.01  -> w = 0.997   (best is 3)
rate 0.4   -> w = 3.000   (best is 3)
rate 0.95  -> w = 2.635   (best is 3)
rate 1.05  -> w = -17.182   (best is 3)

A rate of 0.01 ends at 0.997, still far from 3: it is learning, slowly. A rate of 0.4 reaches 3.000. A rate of 0.95 overshoots on every step and swings from side to side, ending at 2.635 and not yet settled. At 1.05, each step overshoots by more than the last, so the weight ends at −17.182 and the loss keeps growing. Real models are far more complicated than this one-weight example, and the same shapes show up: slow, fine, wobbling and diverging.

Warm-up

At the start of fine-tuning, the optimiser has no useful history about the gradients, and the first few updates can be large and noisy. Warm-up starts the rate at or near zero and raises it in a straight line over the first steps, often a few percent of the run. It makes the early updates gentler, and it is cheap insurance against an unstable start.

Decay

After warm-up, most schedules lower the rate. Large steps help early, when the model is far from a good setting. Small steps help late, when you are fine-tuning the last details. Two common shapes:

  • Linear decay falls in a straight line to zero or a small floor.
  • Cosine decay follows half a cosine wave: it falls slowly at first, quickly in the middle and slowly at the end.

Both need to know the total number of steps, so set the epoch count or step count first. The program below builds both schedules with a 10 percent warm-up and a peak rate of 2e-5.

python
import math

total, warm, peak = 100, 10, 2e-5

def linear(step):
	if step < warm:
		return peak * step / warm
	return peak * (total - step) / (total - warm)

def cosine(step):
	if step < warm:
		return peak * step / warm
	progress = (step - warm) / (total - warm)
	return peak * 0.5 * (1 + math.cos(math.pi * progress))

print("step    linear      cosine")
for step in [0, 5, 10, 30, 55, 80, 100]:
	print(f"{step:>4}  {linear(step):.2e}  {cosine(step):.2e}")

It prints:

text
step    linear      cosine
   0  0.00e+00  0.00e+00
   5  1.00e-05  1.00e-05
  10  2.00e-05  2.00e-05
  30  1.56e-05  1.77e-05
  55  1.00e-05  1.00e-05
  80  4.44e-06  2.34e-06
 100  0.00e+00  0.00e+00

Both start at zero, climb to the peak at step 10, and fall to zero at step 100. At step 55, the middle of the decay, both are at exactly half the peak, 1.00e-05. The difference shows at steps 30 and 80: cosine stays higher early (1.77e-05 against 1.56e-05) and drops lower late (2.34e-06 against 4.44e-06). In practice the choice between them matters much less than the peak rate and the length of the run. Training libraries offer both through a scheduler setting, so you rarely write them yourself.

Finding a value for your run

  1. Start from a documented example for the same model and method.
  2. Try a small range. Run short trials at half the value and at twice the value, and compare the validation score.
  3. Watch for instability. A loss that jumps or becomes nan means the rate is too high, as Reading a training run showed.
  4. Record the setting together with the schedule and the warm-up, because the same peak rate behaves differently with a different schedule.

A rate that is too low often looks like a model that learned nothing: training loss moves a little and the validation score barely beats the baseline. Underfitting has other causes too, so compare with a higher rate before you conclude anything. Overfitting and underfitting covers the other direction.

You seeTry
Too highLoss jumps, or becomes nanHalve the rate, add warm-up
Too lowLoss falls very slowlyDouble the rate, or train longer
No warm-upUnstable first stepsWarm up over a few percent of steps
Rate never decaysNoisy ending, score swingsAdd linear or cosine decay
Learning rate problems
Small ratesFine-tuning adjusts a working model, so use a small peak rate.
Warm upRaise the rate over the first steps to avoid a rough start.
Decay to the endLower the rate over the run so the last steps settle.

Your reading position is saved

0%

0 of 36 complete

Contents

Learning tools