v9.1.1
Menu

StepLR Scheduler

easyFREELearning Rate SchedulersPyTorch Advanced

Problem Statement

Use StepLR to decay the learning rate by a factor every N epochs.

Background

StepLR(optimizer, step_size, gamma) multiplies LR by gamma every step_size epochs. It's the simplest scheduling strategy.

Your Task

The starter code creates a model and SGD optimizer. Create a StepLR scheduler that halves the learning rate every 3 epochs.

The epoch loop that records LR history is pre-filled.

Output Format

Returns a dictionary with "lr_history" (10 values), "initial_lr", and "final_lr".

Example

Input
None
Output
{'lr_history': [0.1, 0.1, 0.1, 0.05, 0.05, 0.05, 0.025, 0.025, 0.025, 0.0125], 'initial_lr': 0.1, 'final_lr': 0.0125}
Reasoning
  • The function takes no arguments. It starts from a learning rate η0=0.1\eta_0 = 0.1, a step size of 33 epochs and a decay factor γ=0.5\gamma = 0.5 and records the rate at the start of each of 10 epochs, e=0,…,9e = 0, \dots, 9.
  • The rate is constant within each block of 3 epochs and is multiplied by γ\gamma each time a block ends: ηe=0.1⋅0.5⌊e/3⌋\eta_e = 0.1 \cdot 0.5^{\lfloor e/3 \rfloor}.
  • Epochs 0–2 have exponent 0, so η=0.1\eta = 0.1. Epochs 3–5 have exponent 1, so η=0.05\eta = 0.05. Epochs 6–8 give 0.1⋅0.25=0.0250.1 \cdot 0.25 = 0.025, and epoch 9 gives 0.1⋅0.125=0.01250.1 \cdot 0.125 = 0.0125.
  • Each value is recorded before that epoch's decay is applied, which is why the first entry is the untouched 0.10.1. Epoch 9 opens the fourth block; the next halving would come at epoch 12, outside the window.
  • initial_lr is the first entry, 0.1, and final_lr is the last, 0.0125. Notice the history is a staircase, not a smooth curve: a step size of 2 would give five two-epoch plateaus.

Constraints

  • step_size=3, gamma=0.5
  • Record LR before each scheduler.step()
  • 10 epochs total

Learn this in

More from PyTorch Advanced

solution.py

Test results

Sign in to run your code against the test cases.