Gradient Descent §
Given a differentiable loss , gradient descent iterates:
where is the learning rate. For -strongly convex and -smooth (Lipschitz gradient), gives linear convergence: . Gradient descent is the workhorse algorithm for training neural networks, fitting regression models, and solving any differentiable optimization problem at scale.
Authoritative sources
Batch gradient descent §
Definition: batch GD uses the full dataset: . One pass over the data is an epoch. Cost per step: . For -strongly convex, -smooth , the convergence rate is .
Details
The learning rate: the step size controls the tradeoff between speed and stability.
- too large: the iteration overshoots the minimum and diverges (violates the contraction condition ).
- too small: convergence is slow (the contraction factor is close to 1).
- : the optimal choice for strongly convex problems; gives the fastest linear convergence.
The epoch: in practice, data is processed in passes. One epoch is one complete pass over all training examples. The total number of gradient steps is where is the batch size.
Example: minimizing from with (the optimal where ): . Converged in one step.
Example problem:
Problem
Minimize by batch gradient descent from with . Compute the first two steps and identify the convergence bottleneck.
Solution
.
- Step 1:
- Step 2:
The -coordinate oscillates (sign flips each step) because is at the boundary of stability for the -direction (, so is required; is exactly at the boundary). The -direction converges smoothly. This ill-conditioning (ratio ) is the bottleneck: the narrow valley in the -direction forces a small step, which slows progress in the -direction. Momentum (below) addresses this.
Minibatch SGD §
Definition: minibatch stochastic gradient descent (SGD) uses a random subset of size : . The gradient is unbiased but noisy; the noise acts as a regularizer and helps escape sharp minima and saddle points.
Details
Why noise helps: in high-dimensional non-convex landscapes, saddle points dominate local minima (Dauphin et al., 2014). The stochastic noise in SGD provides the perturbation needed to escape saddle points, which deterministic GD cannot do (it gets stuck at any critical point).
Batch size tradeoff:
- (pure SGD): maximum noise, fastest per-step, but high variance in the gradient estimate.
- (batch GD): zero noise, slowest per-step, but a precise gradient.
- (typical): a practical compromise. The noise is small enough for stable training but large enough to provide regularization.
Learning rate schedule: in practice, is decayed over training (e.g., step decay, cosine annealing). A large initial explores the landscape; a small final fine-tunes the solution. This is analogous to simulated annealing in physics.
Connection to Regression: the train/validation split and overfitting diagnostics from regression apply directly to SGD training. The stochastic noise in SGD acts as an implicit regularizer, often reducing the need for explicit weight decay.
Example problem:
Problem
You have training examples with features. Compare the per-epoch cost of batch GD vs. minibatch SGD with .
Solution
- Batch GD: one gradient step per epoch, cost FLOPs.
- Minibatch SGD: steps per epoch, each costing FLOPs. Total per epoch: FLOPs.
The total FLOPs per epoch are the same (you still touch every example once). The difference is in the number of parameter updates: SGD makes 391 updates per epoch vs. 1 for batch GD. More updates means faster progress toward the minimum (each update moves the parameters), at the cost of noisier gradients.
Momentum §
Definition: momentum (Polyak, 1964) maintains a velocity and updates . It damps oscillations in narrow valleys and accelerates progress in consistent directions.
Details
Intuition: imagine a ball rolling in a landscape. Without momentum, the ball stops at every small bump. With momentum, the ball builds up speed in consistent directions and plows through small bumps. In a narrow valley (high curvature in one direction, low in another), the oscillations in the high-curvature direction cancel out (the velocity averages to zero), while the progress in the low-curvature direction accumulates.
The effective learning rate: with momentum, the effective step size in a consistent direction is . For , the effective learning rate is . This allows a larger nominal without instability, because the oscillatory components are damped.
Nesterov momentum: a refinement that looks ahead: . The gradient is evaluated at the predicted next position, not the current one. This gives a small but consistent improvement in convergence rate.
Example problem:
Problem
Minimize from with momentum and . Compute and .
Solution
.
- Step 1: . .
- Step 2: . .
Without momentum (), the same would give: (diverges! The stability condition is , and is at the boundary). With momentum, the effective step is damped in the oscillatory direction, so the iteration remains stable and converges.
Adam §
Definition: Adam (Kingma & Ba, 2015) maintains a first moment and a second moment , then updates where and are bias-corrected estimates. The per-coordinate adaptive step size makes Adam robust to sparse gradients.
Details
Why adaptive step sizes help: in neural networks, different parameters have very different gradient scales. Embedding table entries for rare tokens have sparse, large gradients; bias terms have small, dense gradients. Adam normalizes each coordinate by its historical gradient magnitude, so all parameters receive comparable step sizes.
Typical hyperparameters: , , , to . These defaults work well across most problems, which is why Adam became the default optimizer for deep learning.
Convergence: Adam does not converge for all problems (Reddi et al., 2018 showed a counterexample). In practice, it works well for non-convex deep learning objectives. For convex problems, SGD with a proper learning rate schedule is often preferable.
Connection to Neural Networks: Adam is the default optimizer for training neural networks. The compute section of that page covers how the gradients are computed (backpropagation) and how they are applied in parallel on GPUs.
Example problem:
Problem
For a single parameter with , , , and constant gradient for all , compute the Adam update at and .
Solution
At : , . Bias-corrected: , . Update: .
At : (converged), (converged). Bias-corrected: , . Update: .
For a constant gradient, Adam reduces to plain gradient descent with step size . The adaptive mechanism only matters when the gradient magnitude varies across coordinates or over time.
Loss landscape §
Definition: for non-convex , the landscape has saddle points, local minima, and plateaus. In high dimensions, saddle points dominate local minima, so SGD’s noise helps escape them. The geometry of the loss landscape determines which optimizer and learning rate schedule will work.
Details
Convex vs. non-convex: for convex , every local minimum is global, and gradient descent with converges linearly. For non-convex (neural networks), the landscape is complex:
- Saddle points: points where the gradient is zero but the Hessian has both positive and negative eigenvalues. In high dimensions, these are far more common than local minima.
- Plateaus: regions where the gradient is very small but the point is not a minimum. Adam’s adaptive step size helps traverse plateaus.
- Sharp vs. flat minima: sharp minima (high Hessian eigenvalues) generalize poorly; flat minima (low Hessian eigenvalues) generalize well. SGD’s noise implicitly prefers flat minima.
The valley structure: in ill-conditioned problems (large ratio), the loss landscape has narrow valleys. Gradient descent oscillates across the valley while slowly descending. Momentum and adaptive methods (Adam) address this by damping the oscillatory component.
Example problem:
Problem
A 1000-parameter neural network has a loss landscape with condition number . Compare the number of iterations for batch GD (optimal ) vs. momentum GD () to reduce the loss by a factor of .
Solution
- Batch GD: convergence rate . Need , so iterations.
- Momentum GD: the effective condition number is reduced. With , the effective rate is approximately . Need , so iterations.
Momentum reduces the iteration count by a factor of , which is the well-known acceleration from the condition number.
Quiz §
Problem: You have a 2-layer MLP with 784 inputs, 128 hidden units (ReLU), and 10 outputs (softmax). Compute the number of trainable parameters and the FLOPs for one forward pass on a single example. Then estimate the FLOPs for one full training step (forward + backward).
Solution
Parameters:
- Layer 1: → weights + biases = .
- Layer 2: → weights + biases = .
- Total: parameters.
FLOPs (forward):
- Layer 1: (multiply-accumulate counts as 2 FLOPs).
- Layer 2: .
- Total: FLOPs per example.
Backpropagation costs 2× the forward FLOPs: 1× forward for activation gradients (error signals) + 1× forward for weight gradients. Total training step = 3× forward ≈ FLOPs.