Warmup + Linear Decay Schedule

OptimizationEasy+10 XP+10

Warmup + Linear Decay Schedule

Freshly initialised weights produce wild gradients, so starting at your full learning rate can wreck a run in the first few steps. And a rate large enough to make early progress is too large to settle down later. The standard answer does both things in sequence: warm up, then decay.

Task: write warmup_linear(peak_lr, warmup_steps, total_steps) returning the learning rate for every step from 0 to total_steps - 1, each rounded to 4 decimal places.

Warmup — for steps t < warmup_steps, climb linearly to the peak:

lrt=peak×t+1warmup_stepslr_t = peak \times \frac{t + 1}{warmup\_steps}lrt​=peak×warmup_stepst+1​

The t + 1 means step 0 already starts above zero, and the last warmup step lands exactly on peak.

Decay — from warmup_steps onward, fall linearly away from the peak:

lrt=peak×(1−t−warmup_stepstotal_steps−warmup_steps)lr_t = peak \times \left(1 - \frac{t - warmup\_steps}{total\_steps - warmup\_steps}\right)lrt​=peak×(1−total_steps−warmup_stepst−warmup_steps​)

The first decay step is still exactly peak, and the rate shrinks from there.

  • Return a list of length total_steps.
  • warmup_steps can be 0, meaning no warmup at all — every step decays. Make sure you don't divide by zero getting there.
  • warmup_steps can equal total_steps, meaning the run is pure warmup and the decay branch never executes.
  • The rate stays above zero throughout; the schedule approaches zero at the end without arriving.

With warmup_steps = 2 and peak = 0.1 you should see the rate rise over two steps, sit at the peak, then slide down — the shape every transformer training run is built on.

Examples

peak_lr = 0.1
warmup_steps = 2
total_steps = 6
returns [0.05, 0.1, 0.1, 0.075, 0.05, 0.025]
peak_lr = 1.0
warmup_steps = 1
total_steps = 4
returns [1.0, 1.0, 0.6667, 0.3333]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run