863. Fix the warmup schedule stuck at zero
Why it matters A learning rate stuck at 0 looks like a model that does not learn. Integer division is one of the easiest ways to cause it.
The starter code below runs, but it has a realistic bug. Read the spec, find the bug and fix it. Keep the function name and signature.
warmup_cosine_lr(step, base_lr, warmup, total) should return the learning rate for an int step:
- if step < warmup: base_lr * (step + 1) / warmup (linear warmup)
- otherwise: progress = (step - warmup) / max(1, total - warmup), clipped to at most 1, and lr = 0.5 * base_lr * (1 + cos(pi * progress))
Inputs: step, warmup and total are ints with warmup >= 0 and total >= warmup. base_lr is a float.
Return a Python float rounded to 8 decimals.
def warmup_cosine_lr(step, base_lr, warmup, total)Example 1
Input: warmup_cosine_lr(4, 0.001, 10, 100)
Output: 0.0005
Example 2
Input: step = 9, base_lr = 0.001, warmup = 10, total = 100
Output: 0.001
Example 3
Input: step = 100, base_lr = 0.001, warmup = 10, total = 100
Output: 0.0
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 6 tests to be accepted.
Hint 1
Evaluate (4 + 1) // 10 in your head.
Hint 2
Look for every // in the function.
Hint 3
Replace // with / where you want a fraction.
Common mistakes
- Fixing only the warmup branch.
- Forgetting to clip progress past total.
- Starting warmup at 0 instead of
base_lr/ warmup.
Understand the idea
In Python, // floors. (step + 1) // warmup is 0 for every warmup step except the last, so the model gets no updates during warmup. The same bug in progress makes the cosine jump straight from base_lr to 0. True division / fixes both.
Shapes and data flow
Scalars only.