MLCrack

    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

    OptimizerAdaptive LRBest ForGeneralization
    SGD + MomentumโŒStrong baseline for many CNN tasksTask-dependent
    AdaGradโœ… Per-paramSparse featuresโš ๏ธ LR decays to 0
    Adamโœ… Per-paramPrototyping, transformersGood
    AdamWโœ… Per-paramCommon in LLM trainingValidate weight decay and task quality

    SGD + Momentum

    vt=ฮผvtโˆ’1+โˆ‡L,wt=wtโˆ’1โˆ’ฮฑvtv_t = \mu v_{t-1} + \nabla L, \quad w_t = w_{t-1} - \alpha v_t

    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.

    python
    # 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.