SGD, Adam, and every optimizer share one critical knob: learning rate (η). It controls how far weights move along the gradient each step.
Too high → divergence or oscillation. Too low → glacial progress. Schedules tie learning rate to epochs and batch size.
Learning Objectives
By the end of this lesson, students should be able to:
- Interpret learning rate as step size in parameter space.
- Diagnose too-high vs too-low learning rate from loss curves.
- Use PyTorch learning rate schedulers (StepLR, CosineAnnealingLR).
- Run a simple learning rate range test before full training.
- Coordinate LR changes with batch size scaling rules.
Step Size Intuition
Gradient points downhill. Learning rate scales that direction: Δθ = −η∇L. In a valley, large η bounces between walls; tiny η creeps slowly.
The learning rate is a positive scalar (or per-group hyperparameter) multiplying gradients before weight updates. It is often the first hyperparameter tuned after fixing architecture and batch size.
| Symptom | Likely LR Issue | Fix |
|---|---|---|
| Loss NaN / explodes | Too high | Reduce lr 10×; check normalization |
| Loss flat from start | Too low or frozen graph | Increase lr; verify backward |
| Loss oscillates, no trend | Slightly too high | Lower lr or add warmup |
| Train ok, val poor | Not LR alone | See Dropout, data issues |
Schedulers in PyTorch
LR Range Test (Sketch)
Increase lr exponentially over a few hundred batches; plot loss vs lr. Choose lr just before loss spikes—classic diagnostic from Leslie Smith’s LR finder idea.
Reality: Decaying lr late in training fine-tunes weights in a flatter region. Cosine and step decay are standard for a reason.
Reality: Adam often uses 1e-3 to 3e-4; SGD on ImageNet may use 0.1 with momentum at batch 256. Compare optimizers separately.
Some schedulers step per epoch (scheduler.step() after epoch loop); others per batch (OneCycleLR). Wrong placement silently uses wrong lr schedule.
Knowledge Check
- Short Answer: Role of learning rate in SGD? Answer: Scales the gradient step size.
- True/False: Loss NaN often indicates lr too high. Answer: True.
- Multiple Choice: CosineAnnealingLR typically: (a) increases lr forever, (b) decays lr smoothly, (c) disables Adam, (d) doubles batch size. Answer: (b).
- Short Answer: Why warmup? Answer: Stabilize early updates when moments or large grads are noisy.
- True/False: param_groups[0]["lr"] shows current lr. Answer: True.
- Multiple Choice: Flat loss from epoch 1 suggests: (a) lr too low or broken grads, (b) perfect model, (c) too much dropout only, (d) eval mode. Answer: (a).
- Short Answer: Linear batch-lr scaling: double batch, do what to lr? Answer: Try doubling lr (heuristic).
- True/False: Learning rate is the only hyperparameter that matters. Answer: False.
- Multiple Choice: Next regularization lecture: (a) Dropout, (b) Forward Propagation, (c) Batch, (d) Perceptron. Answer: (a).
- Short Answer: Default Adam lr in PyTorch? Answer: 1e-3.
Key Takeaways
- Learning rate is step size—most impactful optimizer hyperparameter.
- Watch loss curves for diverge (high) vs stall (low).
- Schedulers decay lr over epochs for finer late training.
- Warmup helps large models and large effective batches.
- Next: Dropout — regularization beyond lr tuning.
Lab: Train same model with lr 1e-2, 1e-3, 1e-4; students classify curves into healthy / diverged / stalled.
Code review: Find scheduler.step() in wrong loop position in a buggy snippet.