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

Cosine Annealing LR Scheduler

Optimization
Easy

Cosine annealing smoothly decreases the learning rate following a half-cosine curve. Unlike linear decay, it slows down the rate of decrease near the start and end, spending more training time at moderate learning rates. This schedule is widely used in vision and language model training.

Given a base learning rate, a minimum learning rate, total steps, and the current step, compute the learning rate.

Formula

lr=min_lr+12(base_lr−min_lr)(1+cos⁡(π⋅current_steptotal_steps))lr = min\_lr + \frac{1}{2}(base\_lr - min\_lr)\left(1 + \cos\left(\frac{\pi \cdot current\_step}{total\_steps}\right)\right)lr=min_lr+21​(base_lr−min_lr)(1+cos(total_stepsπ⋅current_step​))

At step 0 the cosine term equals 1, so lr = base_lr. At step = total_steps the cosine term equals -1, so lr = min_lr.

Loading visualization...

Examples

Input: base_lr = 0.1, min_lr = 0, total_steps = 100, current_step = 0

Output: 0.1

Explanation: At step zero, the cosine factor selects the base learning rate.

Input: base_lr = 0.1, min_lr = 0, total_steps = 100, current_step = 100

Output: 0.0

Hint 1

Convert the step fraction into an angle by multiplying it by pi.

Hint 2

Scale one plus the cosine between the base and minimum rates.

Requirements

  • Apply the cosine annealing formula exactly as specified
  • Support a non-zero minimum learning rate
  • Return base_lr at step 0 and min_lr at step = total_steps

Constraints

  • base_lr > min_lr >= 0
  • total_steps > 0, 0 <= current_step <= total_steps
  • Return a single float
  • Time limit: 300 ms
Try Similar Problems
Linear Lr SchedulerWarmup Decay LrAdam OptimizerAdamw OptimizerNadam Optimizer

Sign in to take notes on this problem

Case 1
Case 2

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Cosine Annealing LR Scheduler

Optimization
Easy

Cosine annealing smoothly decreases the learning rate following a half-cosine curve. Unlike linear decay, it slows down the rate of decrease near the start and end, spending more training time at moderate learning rates. This schedule is widely used in vision and language model training.

Given a base learning rate, a minimum learning rate, total steps, and the current step, compute the learning rate.

Formula

lr=min_lr+12(base_lr−min_lr)(1+cos⁡(π⋅current_steptotal_steps))lr = min\_lr + \frac{1}{2}(base\_lr - min\_lr)\left(1 + \cos\left(\frac{\pi \cdot current\_step}{total\_steps}\right)\right)lr=min_lr+21​(base_lr−min_lr)(1+cos(total_stepsπ⋅current_step​))

At step 0 the cosine term equals 1, so lr = base_lr. At step = total_steps the cosine term equals -1, so lr = min_lr.

Loading visualization...

Examples

Input: base_lr = 0.1, min_lr = 0, total_steps = 100, current_step = 0

Output: 0.1

Explanation: At step zero, the cosine factor selects the base learning rate.

Input: base_lr = 0.1, min_lr = 0, total_steps = 100, current_step = 100

Output: 0.0

Hint 1

Convert the step fraction into an angle by multiplying it by pi.

Hint 2

Scale one plus the cosine between the base and minimum rates.

Requirements

  • Apply the cosine annealing formula exactly as specified
  • Support a non-zero minimum learning rate
  • Return base_lr at step 0 and min_lr at step = total_steps

Constraints

  • base_lr > min_lr >= 0
  • total_steps > 0, 0 <= current_step <= total_steps
  • Return a single float
  • Time limit: 300 ms
Try Similar Problems
Linear Lr SchedulerWarmup Decay LrAdam OptimizerAdamw OptimizerNadam Optimizer

Sign in to take notes on this problem

Case 1
Case 2

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.