Skip to main content
Home › Categories › Artificial Intelligence & Computing › Gradient Descent Optimizers: SGD, Momentum, Adam, AdamW, and Learning Rate Schedules

Gradient Descent Optimizers: SGD, Momentum, Adam, AdamW, and Learning Rate Schedules

Author: ZUVDEX Team 4 min read
gradient descent vs adamw optimizer stochastic gradient descent momentum adam vs adamw weight decay cosine annealing warmup schedule muon shampoo second order optimizers gradient clipping batch size scaling

Training modern neural networks means minimizing a non-convex objective across millions or trillions of parameters using noisy mini-batch gradient estimates. Optimization algorithms—from classical Stochastic Gradient Descent (SGD) with Nesterov momentum to decoupled adaptive moment estimation (AdamW) and matrix-orthogonalized updates—govern how rapidly training converges, how much GPU memory optimizer states consume, and how flatly the final solution generalizes.

Key Highlights

  • Mini-batch SGD injects beneficial gradient noise and requires minimal memory, remaining competitive on convolutional vision architectures when paired with momentum.
  • Adam maintains exponential moving averages of first moments (mean gradient) and second moments (uncentered variance), adapting step sizes per coordinate—essential for Transformers whose embedding, attention, and MLP layers exhibit vastly different gradient scales.
  • AdamW decouples L2 weight decay from adaptive gradient rescaling, dramatically improving generalization over original L2-penalized Adam.
  • Linear learning-rate warmup followed by Cosine Annealing or Warmup–Stable–Decay (WSD) schedules stabilizes early attention variance and settles weights into wide, well-generalizing basins.

Why Transformers Need AdamW While CNNs Thrive on SGD

Along narrow, ill-conditioned ravines of a loss surface, vanilla gradient descent oscillates wildly across steep walls while crawling slowly along the valley floor. Momentum dampens transverse oscillations by accumulating velocity along consistent descent directions. Yet in Transformer language models, rare vocabulary embeddings and query-key projections exhibit heavy-tailed, heterogeneous gradient magnitudes where a single scalar step size stalls learning; Adam normalizes each parameter’s update by the root-mean-square of its recent gradients.

Ilya Loshchilov and Frank Hutter demonstrated in 2019 that adding standard L2 penalty inside Adam’s gradient accidentally shrinks weight decay on coordinates with large historical gradients. Decoupling weight decay directly onto the parameter update—AdamW—restored clean regularization and became universal across foundation-model pretraining.

Optimizer Extra State per Param Strengths Primary Trade-Off
SGD + Momentum 1 moment buffer (4B FP32) Low VRAM, strong flat-minima generalization on ResNets Slow or unstable on Transformers without heavy tuning
AdamW 2 moment buffers m & v (8B FP32) Robust default across LLMs, ViTs, Diffusion & RL Doubles/triples weight memory unless sharded (ZeRO) or 8-bit
8-Bit AdamW / Adafactor Quantized or factored states (~2B) Cuts optimizer VRAM by up to 75% on single-GPU fine-tuning Slight quantization overhead; standard in QLoRA workflows
Muon / Shampoo Matrix preconditioners / orthogonal steps Faster token-efficiency per step on dense hidden matrices Applied selectively to 2D hidden matrices alongside AdamW on embeddings

Warmup, Cosine vs WSD Schedules, and Batch-Size Scaling

At step zero of Transformer training, second-moment variance estimates v_t contain almost no sample history, causing dangerously large initial steps that destabilize LayerNorm and softmax attention. Ramping the learning rate linearly from zero over the first 0.5% to 2% of tokens allows variance buffers to equilibrate cleanly before peak learning rates engage.

While Cosine Annealing smoothly decays the learning rate toward ~10% of peak at a predetermined token horizon, Warmup–Stable–Decay (WSD) holds a constant plateau across the bulk of pretraining and applies a sharp cooldown only at the end—allowing teams to extend training budgets continuously from intermediate checkpoints without restarting.

  • Clip global gradient L2 norm at 1.0 before applying optimizer updates during LLM pretraining and fine-tuning.
  • When increasing global batch size by a factor of k (via gradient accumulation or data parallelism), scale peak learning rate by √k (for AdamW) or k (linear rule up to critical batch size).
  • Exclude 1D bias vectors and LayerNorm/RMSNorm gain parameters from weight decay.

FAQs

Why is β2 often set to 0.95 instead of 0.999 when pretraining Large Language Models with AdamW?

Large pretraining runs experience occasional loss spikes; β2 = 0.95 gives a shorter exponential memory (~20 steps instead of ~1,000), allowing the second-moment denominator to adapt and recover quickly after transient gradient spikes.

How does Gradient Accumulation simulate large batch sizes on small GPUs?

Instead of stepping the optimizer after every micro-batch, gradients from N consecutive micro-batches are summed (averaged) in place before executing a single weight update—mathematically matching an N-times larger batch size (aside from BatchNorm statistics).

References & Sources

  • Kingma, Diederik P.; Ba, Jimmy (2015) — Adam: A Method for Stochastic Optimization, ICLR.
  • Loshchilov, Ilya; Hutter, Frank (2019) — Decoupled Weight Decay Regularization (AdamW), ICLR.
  • Hu, Shengding et al. (2024) — MiniCPM: Unveiling the Potential of Small Language Models with Scalable Training Strategies (WSD Schedule), COLM.