DEEP LEARNING ยท INTERVIEW QUESTION
Compare Adam, SGD with momentum, and AdaGrad. When would you use each?
๐ก intermediate ยท Training & Optimization
Hint: Think about adaptive learning rates, convergence properties, and generalization.
Answer
Optimizers control how model weights are updated during training. The choice significantly impacts convergence speed and final performance.
Comparison
| Optimizer | Adaptive LR | Best For | Generalization |
|---|---|---|---|
| SGD + Momentum | โ | Strong baseline for many CNN tasks | Task-dependent |
| AdaGrad | โ Per-param | Sparse features | โ ๏ธ LR decays to 0 |
| Adam | โ Per-param | Prototyping, transformers | Good |
| AdamW | โ Per-param | Common in LLM training | Validate weight decay and task quality |
SGD + Momentum
Constant LR with momentum for acceleration. Can generalize well with proper LR scheduling; compare against tuned adaptive optimizers on the target task.
Adam
Combines momentum (1st moment) and adaptive LR (2nd moment). Fast convergence, less sensitive to LR choice.
# Prototyping
optimizer = torch.optim.AdamW(model.parameters(), lr=3e-4, weight_decay=0.01)
# Alternative to benchmark on the target task
optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=100)Practical approach: start with a published recipe for the architecture, then compare time-to-quality and validation performance. Switching from AdamW to SGD is not a universal improvement.