Learning Rate: Warmup + Cosine Decay

easy
Unit 2 · Training0/3 to milestone

Almost every modern LLM is trained with a linear warmup followed by cosine decay: start small so early updates don't blow up, peak, then glide down.

Implement lr_at(step, max_lr, warmup_steps, total_steps, min_lr=0.0):

  • Warmup (step < warmup_steps): rise linearly, max_lr * (step + 1) / warmup_steps.
  • Decay (warmup_steps <= step < total_steps): with progress p=step−warmuptotal−warmupp = \frac{\text{step} - \text{warmup}}{\text{total} - \text{warmup}},
lr=min_lr+12(max_lr−min_lr)(1+cos⁡(πp))lr = \text{min\_lr} + \tfrac{1}{2}(\text{max\_lr} - \text{min\_lr})(1 + \cos(\pi p))
  • After total_steps: stay at min_lr.
#training#optimization#llm2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Learning Rate: Warmup + Cosine Decay · TechGradify