BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Debug and fix8 of 16 in topic

863. Fix the warmup schedule stuck at zero

EasyDebug and fix

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.

Functiondef 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.
Topics
Debug and fix
Similar questions
  • Fix the softmax that returns nanEasy
  • Fix the LayerNorm that normalises the wrong axisEasy
  • Fix the cross-entropy that returns infEasy
  • Fix the F1 score that crashes on empty predictionsEasy
All Debug and fix problems
Report
863 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
7
8
import math
​
def warmup_cosine_lr(step, base_lr, warmup, total):
if step < warmup:
return round(base_lr * ((step + 1) // warmup), 8)
progress = min(1, (step - warmup) // max(1, total - warmup))
return round(0.5 * base_lr * (1 + math.cos(math.pi * progress)), 8)
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 6 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS