PIXELBANKv9.1.0
Menu

Implement a warmup + cosine decay learning rate scheduler.

The schedule has two phases:

  1. Warmup (steps 0 to warmup_steps-1): Linear warmup from 0 to max_lr lr=max_lr×stepwarmup_stepslr = \text{max\_lr} \times \frac{\text{step}}{\text{warmup\_steps}}
  2. Cosine decay (steps warmup_steps to total_steps-1): Decay from max_lr to min_lr lr=min_lr+0.5×(max_lr−min_lr)×(1+cos⁡(π×step−warmup_stepstotal_steps−warmup_steps))lr = \text{min\_lr} + 0.5 \times (\text{max\_lr} - \text{min\_lr}) \times (1 + \cos(\pi \times \frac{\text{step} - \text{warmup\_steps}}{\text{total\_steps} - \text{warmup\_steps}}))

Input: warmup_steps total_steps max_lr min_lr Output: Learning rate for each step (0 to total_steps-1), one per line, rounded to 6 decimal places.

Example:

Input:
2 6 0.001 0.0001
Output:
0.000000
0.000500
0.001000
0.000775
0.000325
0.000100
Reasoning:
  • The warmup phase starts from step 0 to 1 (since warmup_steps = 2), with a linear increase from 0 to max_lr = 0.001. At step 0, the learning rate is 0.001×02=0.0000000.001 \times \frac{0}{2} = 0.000000, and at step 1, it's 0.001×12=0.0005000.001 \times \frac{1}{2} = 0.000500.
  • From step 2 onwards, the cosine decay phase begins, with the learning rate calculated as 0.0001+0.5×(0.001−0.0001)×(1+cos⁡(π×step−26−2))0.0001 + 0.5 \times (0.001 - 0.0001) \times (1 + \cos(\pi \times \frac{\text{step} - 2}{6 - 2})).
  • Applying this formula for steps 2 to 5 gives the remaining learning rates: at step 2, lr=0.001000lr = 0.001000 (since cos⁡(0)=1\cos(0) = 1), at step 3, lr=0.000775lr = 0.000775, at step 4, lr=0.000325lr = 0.000325, and at step 5, lr=0.000100lr = 0.000100.
  • These calculated learning rates are then rounded to 6 decimal places and output, one per line, resulting in the given sample output.

Constraints:

  • 0 < warmup_steps < total_steps
  • min_lr < max_lr
  • Round to 6 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
LR Scheduler - Medium | PixelBank