Step LR schedulerEasyoptimizationlearning-rateschedulertraining
Step LR scheduler
Background
A learning-rate scheduler changes the learning rate as training proceeds. StepLR is the simplest: it holds the rate constant, then multiplies it by a factor every fixed number of epochs. Large early steps make fast progress; the periodic decay then lets the model settle into a minimum.
Problem statement
Implement a StepLRScheduler class with:
__init__(self, initial_lr, step_size, gamma)get_lr(self, epoch)returning the learning rate at a given (0-indexed) epoch:
where is step_size. Round the result to 4 decimals.
Input
initial_lr—float, the starting learning rate .step_size—int, the decay period (rate drops everystep_sizeepochs).gamma—float, the multiplicative decay factor (e.g. 0.5 halves the rate).epoch—int, the current epoch (0-indexed) passed toget_lr.
Output
get_lr(epoch) returns a float learning rate, rounded to 4 decimals.
Examples
Example 1
Input: StepLRScheduler(initial_lr=0.1, step_size=5, gamma=0.5)
get_lr(0), get_lr(4), get_lr(5), get_lr(9), get_lr(10)
Output: 0.1, 0.1, 0.05, 0.05, 0.025
Explanation: the rate stays 0.1 for epochs 0–4 (), drops to at epoch 5, and to at epoch 10.
Constraints
- The number of decays is the floor of
epoch / step_size(integer division). - The decay is multiplicative: raised to the number of decays.
- Round the returned learning rate to 4 decimals.
Notes
- StepLR is a step function of the epoch: flat plateaus separated by sudden drops at multiples of
step_size. - The smooth alternative is ExponentialLR, which decays every epoch by rather than in discrete steps.
Python
Loading...
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example sequence
- •Rate is constant within a step window
- •Decays by gamma at each step boundary
- •gamma = 1 keeps the rate fixed