Skip to content

Optimization and Gradient Descent

Quick overview Training machine learning models means solving an optimization problem. This article systematically breaks down the gradient descent family — batch/stochastic/minibatch, momentum, Adam/AdamW, learning rate scheduling — plus troubleshooting vanishing/exploding gradients, local optima, and non-converging loss.

Optimization and Gradient Descent ​

Concept Definition: Training = Minimizing Loss ​

The essence of machine learning training is solving an optimization problem: find parameters θ that minimize the loss function L(θ). Taking linear regression as an example, L(w,b) = 1/n·Σ(yᵢ - ŷᵢ)², and training means gradually adjusting w and b along the "fastest direction of loss decrease" until the loss stops dropping.

This method of "walking in the fastest direction of decrease" is gradient descent — using the gradient (derivative vector) of the loss with respect to parameters to indicate direction, stepping in the negative gradient direction each time. The gradient is "the rate at which loss changes with parameters," and the negative gradient is "the fastest direction of decrease."

θ ← θ - η·∇L(θ)
      ↑    ↑
   Parameter  Learning rate (step size) × Gradient

Three Variants of Gradient Descent ​

Divided by "how much data is used to compute the gradient each time":

VariantUsed Per UpdateCharacteristicsSuitable For
Batch Gradient Descent (BGD)All samplesAccurate gradient, but traverses the full dataset each step — slow, memory-intensiveSmall datasets
Stochastic Gradient Descent (SGD)1 sampleFast per step, stochastic noise can escape local optima, but noisy and oscillatingMedium-large datasets
Mini-batch Gradient DescentA small batch (32/64/128…)Compromise: more accurate gradient + vectorized acceleration + controllable noiseThe default for deep learning

Mini-batch is the practice standard: batch size is an important hyperparameter — too small means high noise and slow convergence, too large means accurate gradients but prone to sharp minima and high GPU memory consumption.

Why "noise" is a feature

SGD's noise lets it escape local minima (local optima); larger batches produce smoother gradients and more easily settle at sharp local minima (which often generalize worse). In practice, it's commonly observed that models trained with small batches generalize better. This is an instance of "noise as regularization."

Learning Rate: The Most Important Hyperparameter ​

The learning rate η determines how far to step each time — it is the first hyperparameter for almost every model:

Learning rate too large → Oscillation and divergence (loss rises / NaN)
Learning rate appropriate → Fast convergence
Learning rate too small → Snail's pace convergence, prone to being stuck in local optima

Learning Rate Scheduling ​

Fixed learning rate is rarely optimal. Common scheduling strategies:

StrategyApproachCharacteristics
Step DecayMultiply by 0.1 every N roundsSimple, needs tuning of decay timing
Exponential DecayMultiply by γ<1 each roundSmooth decay
Cosine AnnealingCosine curve down to near 0Fine convergence in late training, works well with warmup
Warmup + DecayLinearly ramp up in the first few rounds, then decayStandard for large models (prevents initial large gradients from destroying pre-trained weights)

How to Find the Initial Learning Rate ​

  • Empirical ranges: start from 1e-3 (Adam) or 1e-2 (SGD+momentum) by default;
  • Learning rate finder: run a few batches from very small with exponential growth, plot the loss-learning rate curve, and pick "the start of the steepest loss descent segment";
  • Large model training: warmup to a peak in the 1e-4~1e-3 range.

Optimizers: From SGD to the Adam Family ​

Momentum: an improvement on SGD — the update direction adds the historical gradient direction (like a rolling ball accumulating momentum), accelerating convergence, suppressing oscillations, and allowing escape from local minima. Nesterov momentum further "looks one step ahead before moving."

Adaptive learning rate: each parameter gets its own learning rate, with frequently updated parameters getting small steps and sparse parameters getting large steps:

OptimizerCore MechanismCharacteristicsSuitable For
AdagradCumulative gradient squared as denominatorGood for sparse features, but learning rate monotonically decays to zeroSparse features
RMSPropSliding average of gradient squaredFixes Adagrad's decay problemGeneral-purpose
AdamMomentum + RMSProp (first- and second-order moment estimates)Default choice, fast and stable convergence, nearly zero-tuning requiredThe deep learning default
AdamWAdam + decoupled weight decayMore correct regularization, standard for large models/TransformersLarge models / Transformers
SGD+MomentumMomentum + fixed learning rateOften better generalization, but requires careful learning rate tuningSmall models, classic CV

Two things to keep in mind about Adam

  1. Adam isn't a free lunch: it converges fast but often stops at "wider minima," and generalization can sometimes be worse than well-tuned SGD+Momentum;
  2. Weight decay ≠ L2 regularization: in Adam, the gradient of L2 gets disrupted by second-order normalization. The correct approach is decoupled weight decay (AdamW) — essential knowledge for training Transformers.

Vanishing and Exploding Gradients ​

During backpropagation, gradients multiply layer by layer, and deep networks are highly prone to:

  • Vanishing gradient: gradients approach 0, shallow-layer parameters barely update — deep networks are hard to train (the root cause before 2015);
  • Exploding gradient: gradients grow exponentially — parameters diverge, loss becomes NaN.

Causes and Countermeasures ​

CauseCountermeasure
Saturating activation functions (sigmoid/tanh have derivatives ≈ 0 at both ends)Switch to ReLU/GELU and other non-saturating activations
Network too deepResidual connections (ResNet), normalization, skip connections
Improper initializationXavier/He initialization, paired with activation functions
Learning rate too largeReduce learning rate, use warmup
Exploding gradientsGradient clipping: scale down if exceeding a threshold, standard for Transformer/LLM

Diagnostic method: print the gradient norm of each layer during training and check if they decrease/increase exponentially.

Loss Non-Convergence Troubleshooting Checklist ​

The most common troubleshooting scene in training: "loss stuck / jumping around / NaN," checked by priority:

1. Check data: NaN/infinite values? Messed-up labels? Shuffled?
2. Check learning rate: too large (NaN/oscillation) or too small (stuck)?
3. Check loss function: is the implementation correct? First try to overfit 1 batch to verify implementability
4. Check normalization: is input standardized? Unnormalized data is the #1 source of gradient issues
5. Check initialization: are Xavier/He used?
6. Check model: any bugs in forward/backward? Test by overfitting a single sample
7. Check gradients: print gradient norms, vanishing or exploding?

The fastest trick to verify model correctness

Try to overfit on a single batch (or even a single sample): if the model can't even fit one sample (loss doesn't drop near 0), it means there's a bug in the model/loss/data pipeline, not an optimization problem. This is the first move in deep learning debugging.

Interaction with Regularization ​

Optimization and regularization are a pair:

  • Early stopping: monitor the validation set and stop training, itself a form of "implicit regularization" (limiting effective steps);
  • Weight decay (L2): add a penalty term in the loss, equivalent to constraining parameter norm, and also stabilizes optimization;
  • Noise as regularization: SGD's randomness, Dropout's random dropping, all inject noise into the optimization path in exchange for better generalization;
  • Learning rate and generalization: overly high learning rate converges fast but the model "doesn't memorize" details well; small learning rates in late cosine decay often bring better generalization.

Tradeoffs ​

  • Adam vs SGD: use Adam for quick deployment and peace of mind; use SGD+Momentum for optimal generalization when you have time to tune; use AdamW for large models/Transformers.
  • Batch size: within GPU memory limits, large batches train faster but may sacrifice generalization; mitigate with learning rate warmup.
  • Training speed vs convergence quality: cosine annealing + warmup sacrifices a bit of early speed for later accuracy — the fact-of-life standard for large model training.
  • Automatic differentiation frameworks: PyTorch/TensorFlow have automated gradient computation; what you need to manage is these four knobs: learning rate, optimizer, scheduler, and clipping.

Further Reading ​

References ​