A · One neuron
Gradient Descent
Find the bottom of the loss by repeatedly taking a small step downhill, against the gradient.
Try it: A4 · Gradient Descent
Intuition
Imagine standing on a hillside in thick fog, wanting to reach the valley floor. You can't see the valley, but you can feel which way the ground slopes under your feet. So you take a step downhill, feel again, take another step, and repeat. That is gradient descent. The hillside is the loss, your position is the model's weights, and "which way is downhill" is the gradient.
For linear regression there is a formula for the bottom of the bowl, but for almost every other model there isn't. Gradient descent needs no formula, only the slope at the current point, so it works for any model whose loss you can differentiate: logistic regression, neural networks with billions of weights, all of them.
The math
The gradient
For a loss \(L(\mathbf{w})\) of several weights, the gradient collects the partial derivatives:
\[ \nabla L(\mathbf{w}) = \left(\frac{\partial L}{\partial w_1}, \dots, \frac{\partial L}{\partial w_d}\right) \]It points in the direction of steepest increase, and its length is how steep that is. Its negative is the steepest way down. Near any point, the loss looks like a tilted plane: \(L(\mathbf{w} + \Delta) \approx L(\mathbf{w}) + \nabla L^\top \Delta\), and among all small steps of a given length, the one opposite the gradient lowers it most.
The update
\[ \mathbf{w} \leftarrow \mathbf{w} - \eta\,\nabla L(\mathbf{w}) \]The learning rate \(\eta\) sets the step size. Steps are automatically large where the loss is steep and small near the bottom, where the gradient shrinks to zero.
The learning rate, exactly
Take the simplest bowl, \(L(w) = \tfrac{a}{2} w^2\), whose gradient is \(a w\). One step gives \(w \leftarrow w - \eta a w = (1 - \eta a)\,w\). So after \(k\) steps \(w_k = (1 - \eta a)^k w_0\), and:
- \(0 < \eta < 1/a\): the factor is between 0 and 1, and \(w\) slides smoothly to 0;
- \(1/a < \eta < 2/a\): the factor is negative but smaller than 1 in size, so \(w\) jumps across the bottom each step while still converging;
- \(\eta > 2/a\): the factor is larger than 1 in size, and \(w\) explodes.
The curvature \(a\) sets the safe step size. In many dimensions each direction has its own curvature, and the steepest one caps \(\eta\) for all of them, so a valley that is steep across and shallow along forces tiny steps along its floor. That is why plain gradient descent is slow in narrow valleys, and why momentum and Adam exist.
Worked example. Fit \(\hat{y} = w x\) to the single point \((x, y) = (2, 4)\) with \(L(w) = (y - wx)^2\). The gradient is \(\frac{dL}{dw} = -2x(y - wx) = -4(4 - 2w)\).
Start at \(w = 0\) with \(\eta = 0.05\). Gradient \(= -16\), so \(w = 0 + 0.05 \cdot 16 = 0.8\). Next, gradient \(= -4(4 - 1.6) = -9.6\), so \(w = 1.28\). Then \(1.568\), \(1.741\), … approaching \(w = 2\), the exact answer. The error shrinks by the same factor \(1 - 0.05 \cdot 8 = 0.6\) every step. With \(\eta = 0.3\) the factor is \(1 - 2.4 = -1.4\), and \(w\) flies off.
Variants
- Batch, stochastic, mini-batch. The true gradient averages over all \(n\) examples, which is slow for large \(n\). Stochastic gradient descent (SGD) uses one random example per step: noisy but cheap. In practice everyone uses mini-batches of 32 to a few thousand examples, which average out most of the noise and suit GPUs. A little noise even helps, shaking the weights out of sharp, poor minima.
- Learning-rate schedules. Start larger, then decay (step, exponential, cosine); often after a short warm-up with tiny steps.
- Momentum and adaptive methods. Momentum, RMSProp and Adam reshape each step from the history of gradients; see Optimizers.
- Second-order methods. Newton's method divides by the curvature too and jumps straight to the bottom of a bowl, but needs the matrix of second derivatives: \(d^2\) numbers, too many for a large network.
When it fails
- Learning rate. Too small: it crawls. Too big: it oscillates or diverges, and the loss turns into NaN. Plotting the loss per step is the first thing to check.
- Badly scaled inputs. If one feature is in thousands and another in fractions, the bowl is extremely elongated and progress along the shallow direction is glacial. Standardising inputs fixes most of it.
- Non-convex losses. A neural network's loss has many valleys and saddle points; gradient descent finds a low point, not necessarily the lowest. Surprisingly, for large networks most of the low points it finds are about equally good.
- Flat regions. Where the gradient is nearly zero, as on the flat ends of a sigmoid, the steps are nearly zero too and learning stalls; see Activation functions.
History
Augustin-Louis Cauchy described the method in 1847, to solve the equations of planetary orbits. Its stochastic version goes back to Herbert Robbins and Sutton Monro's 1951 stochastic approximation, and Rosenblatt's and Widrow and Hoff's learning rules (1958–1960) are SGD in disguise. Once backpropagation made gradients of whole networks cheap in the 1980s, gradient descent became the engine of all of deep learning.
Connections
Builds on
- Loss FunctionsGradient descent minimises a loss function: the loss is the landscape, gradient descent is the walk down it.
Leads to
- Logistic RegressionThere is no formula for its best weights, so gradient descent finds them.
- BackpropagationIt computes the gradient that gradient descent then follows.
- Optimizers: Momentum & AdamMomentum and Adam are gradient descent with memory: they still follow the gradient, but shape each step from its history.
- Regularization & OverfittingThe penalty is added to the loss, so gradient descent shrinks the weights at every step (weight decay).
Further reading
- Gradient descent, how neural networks learn (3Blue1Brown)The picture of rolling downhill on a loss surface, the same idea this Topic's animation draws.
- Linear regression: Gradient descent (Google Machine Learning Crash Course)The same model and loss as here, with the loss curve and when to stop.
- Linear regression: Hyperparameters (Google Machine Learning Crash Course)What a learning rate that's too small or too big does, as in the Challenge's experiments.