StepLR and MultiStepLR help adjust the learning speed of a model during training. This makes the model learn better and faster by lowering the learning rate at certain points.
StepLR and MultiStepLR in PyTorch
Start learning this pattern below
Jump into concepts and practice - no test required
torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma=0.1, last_epoch=-1) torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones, gamma=0.1, last_epoch=-1)
StepLR reduces the learning rate every step_size epochs by multiplying it by gamma.
MultiStepLR reduces the learning rate at specific epochs listed in milestones.
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[3, 7, 10], gamma=0.1)
This code shows how StepLR reduces the learning rate every 3 epochs by half. You see the learning rate and loss printed each epoch.
import torch import torch.nn as nn import torch.optim as optim # Simple model model = nn.Linear(2, 1) # Optimizer optimizer = optim.SGD(model.parameters(), lr=0.1) # StepLR scheduler: reduce LR by 0.5 every 3 epochs scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.5) # Training loop simulation for 10 epochs for epoch in range(1, 11): # Dummy training step optimizer.zero_grad() inputs = torch.tensor([[1.0, 2.0]]) target = torch.tensor([[1.0]]) output = model(inputs) loss = (output - target).pow(2).mean() loss.backward() optimizer.step() # Step the scheduler scheduler.step() # Print current learning rate current_lr = optimizer.param_groups[0]['lr'] print(f"Epoch {epoch}: Learning Rate = {current_lr:.5f}, Loss = {loss.item():.5f}")
Always call scheduler.step() after optimizer.step() in each epoch.
Learning rate changes help the model avoid overshooting the best solution.
StepLR is simpler; MultiStepLR gives more control with specific milestones.
StepLR and MultiStepLR adjust learning rate during training to improve model learning.
StepLR reduces learning rate every fixed number of epochs.
MultiStepLR reduces learning rate at chosen epochs.
Practice
StepLR and MultiStepLR in PyTorch?Solution
Step 1: Understand
StepLRbehaviorStepLRreduces the learning rate by a factor every fixed number of epochs (step size).Step 2: Understand
MultiStepLRbehaviorMultiStepLRreduces the learning rate at specific epochs defined by a list of milestones.Final Answer:
StepLRdecreases learning rate at fixed intervals;MultiStepLRdecreases at specific epochs. -> Option AQuick Check:
StepLR fixed steps, MultiStepLR specific milestones [OK]
- Confusing increase vs decrease of learning rate
- Thinking StepLR changes learning rate randomly
- Mixing learning rate with batch size adjustments
StepLR scheduler in PyTorch that reduces learning rate every 5 epochs by a factor of 0.1?Solution
Step 1: Recall
StepLRparametersStepLRtakesstep_size(int) andgamma(decay factor).Step 2: Identify correct syntax
scheduler = StepLR(optimizer, step_size=5, gamma=0.1) usesstep_size=5andgamma=0.1, which matches the requirement.Final Answer:
scheduler = StepLR(optimizer, step_size=5, gamma=0.1) -> Option AQuick Check:
StepLR uses step_size, not milestones [OK]
- Using milestones parameter with StepLR
- Confusing MultiStepLR and StepLR syntax
- Passing step_size as a list
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
scheduler = MultiStepLR(optimizer, milestones=[3, 6], gamma=0.1)
for epoch in range(8):
scheduler.step()
print(f"Epoch {epoch}: lr = {optimizer.param_groups[0]['lr']}")Solution
Step 1: Understand milestones and gamma
Learning rate reduces by factor 0.1 at epochs 3 and 6.Step 2: Calculate learning rate at epoch 7
Initial lr=0.1; after epoch 3: 0.1*0.1=0.01; after epoch 6: 0.01*0.1=0.001; so at epoch 7 lr=0.001.Final Answer:
0.001 -> Option BQuick Check:
Two milestones reduce lr twice: 0.1 -> 0.01 -> 0.001 [OK]
- Forgetting to apply gamma at both milestones
- Assuming lr changes before first milestone
- Confusing StepLR with MultiStepLR behavior
StepLR:optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
scheduler = StepLR(optimizer, milestones=[10, 20], gamma=0.5)
for epoch in range(25):
scheduler.step()
print(optimizer.param_groups[0]['lr'])Solution
Step 1: Check StepLR parameters
StepLRexpectsstep_size, notmilestones.Step 2: Identify misuse of milestones
Passingmilestonescauses error; correct isstep_size=10for example.Final Answer:
StepLR does not accept milestones parameter; use step_size instead. -> Option CQuick Check:
StepLR uses step_size, not milestones [OK]
- Using milestones with StepLR
- Thinking Adam optimizer is incompatible
- Misunderstanding gamma parameter range
Solution
Step 1: Understand the requirement
Learning rate drops at epochs 10 and 20, then every 5 epochs after 20 (i.e., 25, 30).Step 2: Analyze scheduler options
MultiStepLR can handle fixed milestones (10, 20). StepLR can handle regular steps (every 5 epochs). Combining both after epoch 20 fits the requirement.Step 3: Evaluate options
Use MultiStepLR with milestones=[10, 20, 25, 30] and gamma=0.1 misses epochs after 20 beyond 25 and 30; Use StepLR with step_size=5 and gamma=0.1 drops every 5 epochs from start; Use StepLR with step_size=10 and gamma=0.1 drops every 10 epochs only; Use MultiStepLR with milestones=[10, 20] and gamma=0.1, then StepLR with step_size=5 after epoch 20 correctly combines both schedulers.Final Answer:
Use MultiStepLR with milestones=[10, 20] and gamma=0.1, then StepLR with step_size=5 after epoch 20 -> Option DQuick Check:
Combine MultiStepLR for early milestones + StepLR for regular steps after [OK]
- Trying to use only one scheduler for mixed schedule
- Misplacing milestones or step_size values
- Assuming StepLR can handle irregular milestones
