Model Pipeline - StepLR and MultiStepLR
This pipeline shows how learning rate schedulers StepLR and MultiStepLR adjust the learning rate during training to help the model learn better and faster.
Jump into concepts and practice - no test required
This pipeline shows how learning rate schedulers StepLR and MultiStepLR adjust the learning rate during training to help the model learn better and faster.
Loss
1.0 |*
0.9 | *
0.8 | *
0.7 | *
0.6 | *
0.5 | *
0.4 | *
0.3 | *
+----------------
1 2 3 4 5 6 7 8 9 10 Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.85 | 0.60 | Initial training with learning rate 0.1 |
| 2 | 0.70 | 0.68 | Loss decreased, accuracy improved |
| 3 | 0.60 | 0.72 | Learning rate unchanged for StepLR, decreased for MultiStepLR |
| 4 | 0.55 | 0.75 | Model continues to improve |
| 5 | 0.50 | 0.78 | StepLR reduces learning rate by gamma=0.5 here |
| 6 | 0.45 | 0.80 | Lower learning rate helps fine-tune weights |
| 7 | 0.42 | 0.82 | MultiStepLR reduces learning rate at this milestone |
| 8 | 0.40 | 0.83 | Training stabilizes with smaller learning rate |
| 9 | 0.38 | 0.84 | Model converges further |
| 10 | 0.36 | 0.85 | Final epoch with lowest learning rate |
StepLR and MultiStepLR in PyTorch?StepLR behaviorStepLR reduces the learning rate by a factor every fixed number of epochs (step size).MultiStepLR behaviorMultiStepLR reduces the learning rate at specific epochs defined by a list of milestones.StepLR decreases learning rate at fixed intervals; MultiStepLR decreases at specific epochs. -> Option AStepLR scheduler in PyTorch that reduces learning rate every 5 epochs by a factor of 0.1?StepLR parametersStepLR takes step_size (int) and gamma (decay factor).step_size=5 and gamma=0.1, which matches the requirement.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']}")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'])StepLR expects step_size, not milestones.milestones causes error; correct is step_size=10 for example.