Technopedia

The technologist’s encyclopedia

Gradient Descent

Given a differentiable loss f:Rd→Rf: \mathbb{R}^d \to \mathbb{R}, gradient descent iterates:

θt+1=θt−η∇f(θt)\theta_{t+1} = \theta_t - \eta \nabla f(\theta_t)

where η>0\eta > 0 is the learning rate. For ff μ\mu-strongly convex and LL-smooth (Lipschitz gradient), η=1/L\eta = 1/L gives linear convergence: f(θt)−f∗≤(1−μ/L)t(f(θ0)−f∗)f(\theta_t) - f^* \leq (1 - \mu/L)^t (f(\theta_0) - f^*). 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: ∇f=1n∑i∇ℓi(θ)\nabla f = \frac{1}{n}\sum_i \nabla \ell_i(\theta). One pass over the data is an epoch. Cost per step: O(nd)O(nd). For μ\mu-strongly convex, LL-smooth ff, the convergence rate is f(θt)−f∗≤(1−μ/L)t(f(θ0)−f∗)f(\theta_t) - f^* \leq (1 - \mu/L)^t (f(\theta_0) - f^*).

Details

The learning rate: the step size η\eta controls the tradeoff between speed and stability.

The epoch: in practice, data is processed in passes. One epoch is one complete pass over all nn training examples. The total number of gradient steps is epochs×⌈n/b⌉\text{epochs} \times \lceil n/b \rceil where bb is the batch size.

Example: minimizing f(x)=5x2f(x) = 5x^2 from x0=1x_0 = 1 with η=0.1\eta = 0.1 (the optimal 1/L1/L where L=10L = 10): x1=1−0.1×10=0x_1 = 1 - 0.1 \times 10 = 0. Converged in one step.

Example problem:

Problem

Minimize f(x,y)=x2+10y2f(x, y) = x^2 + 10y^2 by batch gradient descent from (x0,y0)=(1,1)(x_0, y_0) = (1, 1) with η=0.1\eta = 0.1. Compute the first two steps and identify the convergence bottleneck.

Solution

∇f=(2x,20y)\nabla f = (2x, 20y).

  • Step 1: (x1,y1)=(1−0.1×2,1−0.1×20)=(0.8,−1)(x_1, y_1) = (1 - 0.1 \times 2, 1 - 0.1 \times 20) = (0.8, -1)
  • Step 2: (x2,y2)=(0.8−0.1×1.6,−1−0.1×(−20))=(0.64,1)(x_2, y_2) = (0.8 - 0.1 \times 1.6, -1 - 0.1 \times (-20)) = (0.64, 1)

The yy-coordinate oscillates (sign flips each step) because η=0.1\eta = 0.1 is at the boundary of stability for the yy-direction (Ly=20L_y = 20, so η<2/20=0.1\eta < 2/20 = 0.1 is required; η=0.1\eta = 0.1 is exactly at the boundary). The xx-direction converges smoothly. This ill-conditioning (ratio L/μ=10L/\mu = 10) is the bottleneck: the narrow valley in the yy-direction forces a small step, which slows progress in the xx-direction. Momentum (below) addresses this.

Minibatch SGD

Definition: minibatch stochastic gradient descent (SGD) uses a random subset of size bb: ∇^=1b∑i∈B∇ℓi(θ)\hat{\nabla} = \frac{1}{b}\sum_{i \in B} \nabla \ell_i(\theta). 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:

Learning rate schedule: in practice, η\eta is decayed over training (e.g., step decay, cosine annealing). A large initial η\eta explores the landscape; a small final η\eta 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 n=100,000n = 100{,}000 training examples with d=512d = 512 features. Compare the per-epoch cost of batch GD vs. minibatch SGD with b=256b = 256.

Solution
  • Batch GD: one gradient step per epoch, cost O(nd)=100,000×512=5.12×107O(nd) = 100{,}000 \times 512 = 5.12 \times 10^7 FLOPs.
  • Minibatch SGD: ⌈100,000/256⌉=391\lceil 100{,}000/256 \rceil = 391 steps per epoch, each costing O(bd)=256×512=1.31×105O(bd) = 256 \times 512 = 1.31 \times 10^5 FLOPs. Total per epoch: 391×1.31×105≈5.12×107391 \times 1.31 \times 10^5 \approx 5.12 \times 10^7 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 vt=βvt−1+(1−β)∇f(θt)v_t = \beta v_{t-1} + (1-\beta)\nabla f(\theta_t) and updates θt+1=θt−ηvt\theta_{t+1} = \theta_t - \eta v_t. 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 η/(1−β)\eta/(1-\beta). For β=0.9\beta = 0.9, the effective learning rate is 10η10\eta. This allows a larger nominal η\eta without instability, because the oscillatory components are damped.

Nesterov momentum: a refinement that looks ahead: vt=βvt−1+(1−β)∇f(θt−ηvt−1)v_t = \beta v_{t-1} + (1-\beta)\nabla f(\theta_t - \eta v_{t-1}). 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 f(x)=5x2f(x) = 5x^2 from x0=1x_0 = 1 with momentum β=0.9\beta = 0.9 and η=0.2\eta = 0.2. Compute x1x_1 and x2x_2.

Solution

∇f(x)=10x\nabla f(x) = 10x.

  • Step 1: v1=0.9×0+0.1×10=1v_1 = 0.9 \times 0 + 0.1 \times 10 = 1. x1=1−0.2×1=0.8x_1 = 1 - 0.2 \times 1 = 0.8.
  • Step 2: v2=0.9×1+0.1×8=1.7v_2 = 0.9 \times 1 + 0.1 \times 8 = 1.7. x2=0.8−0.2×1.7=0.46x_2 = 0.8 - 0.2 \times 1.7 = 0.46.

Without momentum (β=0\beta = 0), the same η=0.2\eta = 0.2 would give: x1=1−0.2×10=−1x_1 = 1 - 0.2 \times 10 = -1 (diverges! The stability condition is η<2/L=0.2\eta < 2/L = 0.2, and η=0.2\eta = 0.2 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 mt=β1mt−1+(1−β1)gtm_t = \beta_1 m_{t-1} + (1-\beta_1) g_t and a second moment vt=β2vt−1+(1−β2)gt2v_t = \beta_2 v_{t-1} + (1-\beta_2) g_t^2, then updates θt+1=θt−η m^t/(v^t+ε)\theta_{t+1} = \theta_t - \eta \, \hat{m}_t / (\sqrt{\hat{v}_t} + \varepsilon) where m^t=mt/(1−β1t)\hat{m}_t = m_t/(1-\beta_1^t) and v^t=vt/(1−β2t)\hat{v}_t = v_t/(1-\beta_2^t) 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: β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ε=10−8\varepsilon = 10^{-8}, η=10−3\eta = 10^{-3} to 10−410^{-4}. 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 β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ε=0\varepsilon = 0, and constant gradient gt=1g_t = 1 for all tt, compute the Adam update at t=1t = 1 and t=100t = 100.

Solution

At t=1t = 1: m1=0.1×1=0.1m_1 = 0.1 \times 1 = 0.1, v1=0.001×1=0.001v_1 = 0.001 \times 1 = 0.001. Bias-corrected: m^1=0.1/0.1=1\hat{m}_1 = 0.1/0.1 = 1, v^1=0.001/0.001=1\hat{v}_1 = 0.001/0.001 = 1. Update: η×1/1=η\eta \times 1/\sqrt{1} = \eta.

At t=100t = 100: m100≈1m_{100} \approx 1 (converged), v100≈1v_{100} \approx 1 (converged). Bias-corrected: m^100≈1\hat{m}_{100} \approx 1, v^100≈1\hat{v}_{100} \approx 1. Update: η×1/1=η\eta \times 1/\sqrt{1} = \eta.

For a constant gradient, Adam reduces to plain gradient descent with step size η\eta. The adaptive mechanism only matters when the gradient magnitude varies across coordinates or over time.

Loss landscape

Definition: for non-convex ff, 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 ff, every local minimum is global, and gradient descent with η=1/L\eta = 1/L converges linearly. For non-convex ff (neural networks), the landscape is complex:

The valley structure: in ill-conditioned problems (large L/μL/\mu 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 κ=L/μ=100\kappa = L/\mu = 100. Compare the number of iterations for batch GD (optimal η\eta) vs. momentum GD (β=0.9\beta = 0.9) to reduce the loss by a factor of 10−610^{-6}.

Solution
  • Batch GD: convergence rate ρ=1−μ/L=1−1/100=0.99\rho = 1 - \mu/L = 1 - 1/100 = 0.99. Need 0.99t<10−60.99^t < 10^{-6}, so t>log⁡(10−6)/log⁡(0.99)≈1382t > \log(10^{-6})/\log(0.99) \approx 1382 iterations.
  • Momentum GD: the effective condition number is reduced. With β=0.9\beta = 0.9, the effective rate is approximately ρeff≈1−μ/L=1−0.1=0.9\rho_{\text{eff}} \approx 1 - \sqrt{\mu/L} = 1 - 0.1 = 0.9. Need 0.9t<10−60.9^t < 10^{-6}, so t>log⁡(10−6)/log⁡(0.9)≈132t > \log(10^{-6})/\log(0.9) \approx 132 iterations.

Momentum reduces the iteration count by a factor of κ=10\sqrt{\kappa} = 10, 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:

FLOPs (forward):

Backpropagation costs 2× the forward FLOPs: 1× forward for activation gradients (error signals) + 1× forward for weight gradients. Total training step = 3× forward ≈ 610,000610{,}000 FLOPs.