TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Learning Rate Scheduler (Linear Decay)

Optimization
Medium

Implement a learning-rate schedule with linear warmup followed by linear decay. The step index is zero-based, and the rate remains at the final value after training ends.

LR⁡(t)={η0tW,W>0 and t<Wη0+t−WT−W(ηf−η0),W≤t<Tηf,t≥T\operatorname{LR}(t) = \begin{cases} \eta_0 \dfrac{t}{W}, & W > 0 \text{ and } t < W \\ \eta_0 + \dfrac{t-W}{T-W}(\eta_f-\eta_0), & W \le t < T \\ \eta_f, & t \ge T \end{cases}LR(t)=⎩⎨⎧​η0​Wt​,η0​+T−Wt−W​(ηf​−η0​),ηf​,​W>0 and t<WW≤t<Tt≥T​

Here, ttt is step, WWW is warmup_steps, TTT is total_steps, η0\eta_0η0​ is initial_lr, and ηf\eta_fηf​ is final_lr. When total_steps is zero, return final_lr. Return the learning rate as a Python float.

Loading visualization...

Examples

Input: step = 0, total_steps = 100, initial_lr = 0.001, final_lr = 0.0, warmup_steps = 10

Output: 0.0

Explanation: The first warmup step uses zero percent of the initial learning rate.

Input: step = 10, total_steps = 100, initial_lr = 0.001, final_lr = 0.0, warmup_steps = 10

Output: 0.001

Input: step = 50, total_steps = 100, initial_lr = 0.001, final_lr = 0.0, warmup_steps = 10

Output: 0.000556

Hint 1

Handle warmup, decay, and completed training as separate branches.

Hint 2

For decay, use (step - warmup_steps) / (total_steps - warmup_steps) as the interpolation fraction.

Requirements

  • Increase linearly from 0 to initial_lr during warmup
  • Decay linearly from initial_lr to final_lr after warmup
  • Return final_lr at and after total_steps
  • Return a Python float

Constraints

  • step, total_steps, and warmup_steps are nonnegative integers
  • If total_steps is positive, warmup_steps is smaller than total_steps
  • initial_lr and final_lr are nonnegative floats
Try Similar Problems
Cosine Annealing LrWarmup Decay LrAdam OptimizerNadam OptimizerAdamw Optimizer

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Learning Rate Scheduler (Linear Decay)

Optimization
Medium

Implement a learning-rate schedule with linear warmup followed by linear decay. The step index is zero-based, and the rate remains at the final value after training ends.

LR⁡(t)={η0tW,W>0 and t<Wη0+t−WT−W(ηf−η0),W≤t<Tηf,t≥T\operatorname{LR}(t) = \begin{cases} \eta_0 \dfrac{t}{W}, & W > 0 \text{ and } t < W \\ \eta_0 + \dfrac{t-W}{T-W}(\eta_f-\eta_0), & W \le t < T \\ \eta_f, & t \ge T \end{cases}LR(t)=⎩⎨⎧​η0​Wt​,η0​+T−Wt−W​(ηf​−η0​),ηf​,​W>0 and t<WW≤t<Tt≥T​

Here, ttt is step, WWW is warmup_steps, TTT is total_steps, η0\eta_0η0​ is initial_lr, and ηf\eta_fηf​ is final_lr. When total_steps is zero, return final_lr. Return the learning rate as a Python float.

Loading visualization...

Examples

Input: step = 0, total_steps = 100, initial_lr = 0.001, final_lr = 0.0, warmup_steps = 10

Output: 0.0

Explanation: The first warmup step uses zero percent of the initial learning rate.

Input: step = 10, total_steps = 100, initial_lr = 0.001, final_lr = 0.0, warmup_steps = 10

Output: 0.001

Input: step = 50, total_steps = 100, initial_lr = 0.001, final_lr = 0.0, warmup_steps = 10

Output: 0.000556

Hint 1

Handle warmup, decay, and completed training as separate branches.

Hint 2

For decay, use (step - warmup_steps) / (total_steps - warmup_steps) as the interpolation fraction.

Requirements

  • Increase linearly from 0 to initial_lr during warmup
  • Decay linearly from initial_lr to final_lr after warmup
  • Return final_lr at and after total_steps
  • Return a Python float

Constraints

  • step, total_steps, and warmup_steps are nonnegative integers
  • If total_steps is positive, warmup_steps is smaller than total_steps
  • initial_lr and final_lr are nonnegative floats
Try Similar Problems
Cosine Annealing LrWarmup Decay LrAdam OptimizerNadam OptimizerAdamw Optimizer

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.