📘
StepLR Scheduler
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:
- We initialize the learning rate to 0.1 and create a
StepLRscheduler withstep_size=3andgamma=0.5, meaning the learning rate will be multiplied by 0.5 every 3 epochs. - For the first 3 epochs, the learning rate remains at 0.1, as the scheduler hasn't reached its first step.
- At epoch 3, the scheduler steps and multiplies the learning rate by 0.5, resulting in a new learning rate of 0.1⋅0.5=0.05, which remains for the next 2 epochs.
- This process repeats, with the learning rate being multiplied by 0.5 every 3 epochs, resulting in the sequence: 0.1, 0.1, 0.1, 0.05, 0.05, 0.05, 0.025, 0.025, 0.025, 0.0125.
Constraints:
- step_size=3, gamma=0.5
- Record LR before each scheduler.step()
- 10 epochs total
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.