Stochastic Gradient Descent › The Problem with Batch Gradient Descent
Language AI HandbookAll chapters 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 training examples and then update the parameters:
where:
- : the full parameter vector of the network
- : the loss on a single training example
- : the total number of training examples
- : the learning rate, a positive scalar controlling step size
- : the iteration index
The gradient 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 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:
The left side is the gradient on example , a cheap computation. It is a noisy estimate of the true gradient on the right, but its expectation equals the true gradient:
where the expectation is taken over a uniformly random choice of example index . 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.
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.