Stochastic Gradient Descent › The Problem with Batch Gradient Descent

Michael BrenndoerferApril 30, 20253 min read
Language AI HandbookAll chapters
Part 10 of 67Neural Network Foundations
Chapter 66Stochastic Gradient Descent › The Problem with Batch Gradient Descent
Reading section 2 of 16 · The Problem with Batch Gradient Descent · 3 minSection 2 of 16 · 3 min

Book contents

Section 2 of 16 · 3 min read

Stochastic Gradient Descent › The Problem with Batch Gradient DescentOne section · Sections in this chapter2 / 16 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

To understand why SGD matters, we first need to see what goes wrong with full-batch gradient descent on large datasets.

Full-Batch Gradient Descent

In standard gradient descent, we compute the loss averaged over all NN training examples and then update the parameters:

L(θ)=1N∑i=1Nℓ(θ;xi,yi)\mathcal{L}(\boldsymbol{\theta}) = \frac{1}{N} \sum_{i=1}^{N} \ell(\boldsymbol{\theta}; \mathbf{x}_i, y_i) θt+1=θt−η∇θL(θt)\boldsymbol{\theta}_{t+1} = \boldsymbol{\theta}_t - \eta \nabla_{\boldsymbol{\theta}} \mathcal{L}(\boldsymbol{\theta}_t)

where:

  • θ\boldsymbol{\theta}: the full parameter vector of the network
  • ℓ(θ;xi,yi)\ell(\boldsymbol{\theta}; \mathbf{x}_i, y_i): the loss on a single training example (xi,yi)(\mathbf{x}_i, y_i)
  • NN: the total number of training examples
  • η\eta: the learning rate, a positive scalar controlling step size
  • tt: the iteration index

The gradient ∇θL\nabla_{\boldsymbol{\theta}} \mathcal{L} is an exact average over the full training set. This is mathematically clean: the gradient points precisely downhill in the loss landscape. But it has three serious problems in practice.

Computational cost. Modern NLP datasets contain millions of examples. Each forward-backward pass is expensive. Waiting until you have seen all NN examples before updating means you update parameters just once per epoch. With a dataset of 100 million sentences, you might wait hours between updates.

Memory cost. Full-batch gradient descent requires holding the gradients from all examples in memory before computing the average. For large datasets, this can exceed the available GPU memory by orders of magnitude.

Escape from saddle points. Neural network loss landscapes are high-dimensional and filled with saddle points (more on this later in this chapter). The exact gradient at a saddle point is exactly zero, which means batch gradient descent stalls. A little noise can kick the optimizer free.

The Stochastic Alternative

The key insight of SGD is that we don't need the exact gradient. We need a gradient estimate that points, on average, in the right direction. A single randomly chosen training example provides exactly that:

∇θℓ(θ;xi,yi)≈∇θL(θ)\nabla_{\boldsymbol{\theta}} \ell(\boldsymbol{\theta}; \mathbf{x}_i, y_i) \approx \nabla_{\boldsymbol{\theta}} \mathcal{L}(\boldsymbol{\theta})

The left side is the gradient on example ii, a cheap computation. It is a noisy estimate of the true gradient on the right, but its expectation equals the true gradient:

Ei∼Uniform(1,…,N)[∇θℓ(θ;xi,yi)]=∇θL(θ)\mathbb{E}_{i \sim \text{Uniform}(1, \ldots, N)}\left[\nabla_{\boldsymbol{\theta}} \ell(\boldsymbol{\theta}; \mathbf{x}_i, y_i)\right] = \nabla_{\boldsymbol{\theta}} \mathcal{L}(\boldsymbol{\theta})

where the expectation is taken over a uniformly random choice of example index ii. This is the mathematical foundation of SGD: stochastic gradients are unbiased estimators of the true gradient. Each individual step is noisy, but over many steps the noise averages out.

Stochastic Gradient Descent

SGD is an optimization algorithm that estimates the gradient using a single randomly selected training example (or small batch) at each iteration, rather than computing the exact gradient over the entire dataset. Because the estimate is unbiased, parameters converge toward a minimum despite the noise in individual updates.