TL;DR
Gradient descent is a way to make a function smaller by repeating one rule: look at the slope where you stand, then take a small step in the direction that goes most steeply downhill. The slope is the Gradient, and the step size is a number you choose, called the learning rate. Repeat until the steps stop helping.
Training a model is almost always this procedure applied to a loss function, so gradient descent is the engine behind nearly every modern neural network.
Where you'll see it: in the training loop of any neural network, in fitting a linear model when a closed-form answer is too expensive, and in the optimisers (SGD, Adam) that are variations on the same basic step.
Intuition
Walking down in fog
Imagine you are on a hillside in thick fog. You cannot see the valley, but you can feel the ground under your feet. Feel which way is downhill, take a step that way, and feel again. Each step uses only local information, the slope under your current spot, and yet a sequence of such steps leads you into a valley.
That is the whole method. The fog is the function you want to minimise. The slope under your feet is the gradient, which points uphill, so you move the opposite way. The size of your stride is the learning rate. Too short and the walk is slow. Too long and you overshoot the valley and may end up higher than where you started.
Why use this instead of solving for the lowest point directly? For a function with millions of inputs, such as the weights of a neural network, there is usually no formula for the minimum. Even when a formula exists, computing the slope at one point is far cheaper than solving a huge system. Gradient descent trades exactness for something that scales.
- f(x) = (x - 3)^2
flowchart LR
A[Start at a point] --> B[Compute the gradient]
B --> C[Step against the gradient]
C --> D{Stopped improving?}
D -- no --> B
D -- yes --> E[Return the point]
The math
The update rule
Let be a function of a vector of inputs . Gradient descent builds a sequence of points by
Here is the current guess after steps, is the Gradient of at that guess, and is the learning rate. The minus sign is what makes it descend, because the gradient points uphill.
Why a small step against the gradient lowers f
Near a point , a smooth function is approximated by a plane. Write the step as . Then
The change in is approximately . That is negative for any nonzero gradient and any , so a small enough step always decreases . The approximation is good only when the step is small, which is why the learning rate matters.
Where it stops
At a point where the approximation says nothing changes to first order, so the algorithm stops moving. Such a point is a critical point: a minimum, a maximum, or a saddle. For a convex function every critical point is a global minimum, which is why gradient descent is guaranteed to reach the best answer there. For the non-convex functions in deep learning, it finds a point where the slope is flat, which is usually good enough in practice.
Step size on a quadratic
Take the simple function . Its derivative is , so one step gives
The distance to the minimum is multiplied by at every step. The sequence converges exactly when , which means . With the first step lands exactly on the minimum. With the distance grows and the method diverges. This is the one-dimensional picture of the learning-rate tradeoff.
Worked examples
Let , start at , and use . The gradient is .
- Step 0: . Gradient . Update .
- Step 1: . Gradient . Update .
- Step 2: . Gradient . Update .
- Check with the formula : the distances from 3 are , each times the last.
- Values of : , then , then , then . They fall every step.
Interpretation: the walk moves quickly while the slope is steep and slows near 3, shrinking the gap by a fixed fraction each step.
Use the same and , with three learning rates.
- : the factor is . Step 1 gives , the exact minimum.
- : the factor is , so the gap shrinks slowly, as in the previous example.
- : the factor is . Step 1 gives , a gap of on the other side. Step 2 gives , a gap of .
- The gap multiplies by each step, so its size grows without limit.
Interpretation: the same algorithm converges, lands, or explodes depending only on . For a quadratic like this one, the safe range is .
This is a realistic case: fit to the points , , by minimising the mean squared error . The gradients are and , where .
- Start at , . Residuals are , and the loss is .
- Gradient in : .
- Gradient in : .
- Step with : , .
- New predictions are , , . The loss falls from to about .
- The exact least-squares answer is , . Repeated steps move toward it, each step using only the gradient.
Interpretation: one step cut the loss by a factor of five. Real models repeat this same arithmetic across millions of weights.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Learning rate too large | Loss oscillates or explodes | Lower , or use a schedule that decays it |
| Learning rate too small | Training takes forever | Raise , or use momentum |
| Long narrow valley | Steps zigzag across the valley and crawl along it | Momentum or an adaptive method such as [[adam-optimizer |
| Huge dataset | One full-data gradient per step is too slow | [[stochastic-gradient-descent |
| Vanishing gradients in deep nets | Gradients near zero in early layers, so they barely learn | Better activations, normalisation, or residual connections (see Vanishing Gradient Problem) |
| Minimising training loss only | Model memorises the data and fails on new data | Regularisation and validation (see Overfitting) |
The core limitation is that gradient descent uses only first-order information, the slope. It cannot tell a valley from a saddle point without extra care, and on badly scaled problems it needs many steps. Second-order methods use curvature, but they are too expensive for most large models.
History
The idea of moving against the gradient is old. In 1847 the French mathematician Augustin-Louis Cauchy proposed a method of steepest descent for solving systems of equations. For a long time it was a tool of numerical analysis rather than machine learning.
In 1951 Herbert Robbins and Sutton Monro studied stochastic approximation, which is gradient descent using noisy estimates of the gradient. In 1986 David Rumelhart, Geoffrey Hinton and Ronald Williams popularised backpropagation, which computes gradients efficiently through many layers of a neural network. That made gradient descent the practical default for training them. It caught on because it needs only derivatives, and those can be computed automatically for any differentiable model.
Check yourself
QuestionIn one sentence, what does one gradient descent step do?Show answer
It computes the gradient of the function at the current point and moves a distance times that gradient in the opposite direction. The gradient points uphill, so the step goes downhill, and the learning rate sets how far.
QuestionFor with , starting at , compute .Show answer
The gradient at 0 is . The update is . The distance to 3 went from 3 to 1.5, which matches the factor .
QuestionWhy does a learning rate that is too large make the loss go up rather than down?Show answer
The first-order approximation is only accurate for small steps. A large step leaves the region where the slope describes the function, and the curvature can make the function rise. In the quadratic example, makes the distance to the minimum grow every step.
QuestionYou are training a neural network on a dataset of ten million images. Why might you use mini-batches instead of the full dataset to compute each gradient?Show answer
The full gradient needs a pass over all ten million examples before any single step, which makes each update very expensive. A mini-batch gradient averages over a small random sample, so each step is cheap, and the noise in the estimate often helps escape flat regions. The steps are noisier but far more numerous per unit of compute.
QuestionA model's loss shrinks steadily on the training set but its validation loss starts rising. Does gradient descent need to change, or is something else wrong?Show answer
Gradient descent is doing its job: it minimises the training loss. The rising validation loss signals overfitting, meaning the model is fitting noise in the training data. The fix lies in the model and data, such as regularisation, more data, early stopping, or a simpler model, rather than in the optimiser.