Search

Search papers and lessons

Gradient Descent

Level 2 · 10 min read

Gradient descent lowers a function by repeatedly stepping opposite its gradient, and it is how almost every model is trained.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Linear algebra
  • Losses & regularization
  • Optimizers
  • Discrete math
  • Sign in to see your progress

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.

−10123450246810xf(x)
  • f(x) = (x - 3)^2
Each dot is one gradient descent step with learning rate 0.1, starting at x = 0. The steps get smaller as the slope flattens near the minimum at x = 3.
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 ff be a function of a vector of inputs x\mathbf{x}. Gradient descent builds a sequence of points x0,x1,x2,…\mathbf{x}_0, \mathbf{x}_1, \mathbf{x}_2, \dots by

xt+1=xt−η ∇f(xt).\mathbf{x}_{t+1} = \mathbf{x}_t - \eta \, \nabla f(\mathbf{x}_t).

Here xt\mathbf{x}_t is the current guess after tt steps, ∇f(xt)\nabla f(\mathbf{x}_t) is the Gradient of ff at that guess, and η>0\eta \gt 0 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\mathbf{a}, a smooth function is approximated by a plane. Write the step as h=−η∇f(a)\mathbf{h} = -\eta \nabla f(\mathbf{a}). Then

f(a+h)≈f(a)+∇f(a)⋅h=f(a)−η∥∇f(a)∥2.f(\mathbf{a} + \mathbf{h}) \approx f(\mathbf{a}) + \nabla f(\mathbf{a}) \cdot \mathbf{h} = f(\mathbf{a}) - \eta \lVert \nabla f(\mathbf{a}) \rVert^2.

The change in ff is approximately −η∥∇f∥2-\eta \lVert \nabla f \rVert^2. That is negative for any nonzero gradient and any η>0\eta \gt 0, so a small enough step always decreases ff. The approximation is good only when the step is small, which is why the learning rate matters.

Where it stops

At a point where ∇f(x)=0\nabla f(\mathbf{x}) = \mathbf{0} 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 f(x)=(x−3)2f(x) = (x - 3)^2. Its derivative is f′(x)=2(x−3)f'(x) = 2(x - 3), so one step gives

xt+1−3=(xt−3)−η⋅2(xt−3)=(1−2η)(xt−3).x_{t+1} - 3 = (x_t - 3) - \eta \cdot 2(x_t - 3) = (1 - 2\eta)(x_t - 3).

The distance to the minimum is multiplied by 1−2η1 - 2\eta at every step. The sequence converges exactly when ∣1−2η∣<1|1 - 2\eta| \lt 1, which means 0<η<10 \lt \eta \lt 1. With η=1\eta = 1 the first step lands exactly on the minimum. With η>1\eta \gt 1 the distance grows and the method diverges. This is the one-dimensional picture of the learning-rate tradeoff.

Worked examples

Example
Minimising (x - 3) squared

Let f(x)=(x−3)2f(x) = (x - 3)^2, start at x0=0x_0 = 0, and use η=0.1\eta = 0.1. The gradient is f′(x)=2(x−3)f'(x) = 2(x - 3).

  1. Step 0: x0=0x_0 = 0. Gradient =2(0−3)=−6= 2(0 - 3) = -6. Update x1=0−0.1×(−6)=0.6x_1 = 0 - 0.1 \times (-6) = 0.6.
  2. Step 1: x1=0.6x_1 = 0.6. Gradient =2(0.6−3)=−4.8= 2(0.6 - 3) = -4.8. Update x2=0.6+0.48=1.08x_2 = 0.6 + 0.48 = 1.08.
  3. Step 2: x2=1.08x_2 = 1.08. Gradient =2(−1.92)=−3.84= 2(-1.92) = -3.84. Update x3=1.08+0.384=1.464x_3 = 1.08 + 0.384 = 1.464.
  4. Check with the formula (1−2η)(1 - 2\eta): the distances from 3 are 3,2.4,1.92,1.5363, 2.4, 1.92, 1.536, each 0.80.8 times the last.
  5. Values of ff: 99, then 5.765.76, then 3.68643.6864, then 2.35932.3593. 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.

Example
Learning rate: converging, landing, and diverging

Use the same f(x)=(x−3)2f(x) = (x - 3)^2 and x0=0x_0 = 0, with three learning rates.

  1. η=0.5\eta = 0.5: the factor is 1−2(0.5)=01 - 2(0.5) = 0. Step 1 gives x1=0−0.5×(−6)=3x_1 = 0 - 0.5 \times (-6) = 3, the exact minimum.
  2. η=0.1\eta = 0.1: the factor is 0.80.8, so the gap shrinks slowly, as in the previous example.
  3. η=1.1\eta = 1.1: the factor is 1−2.2=−1.21 - 2.2 = -1.2. Step 1 gives x1=0+1.1×6=6.6x_1 = 0 + 1.1 \times 6 = 6.6, a gap of 3.63.6 on the other side. Step 2 gives x2=6.6−1.1×7.2=−1.32x_2 = 6.6 - 1.1 \times 7.2 = -1.32, a gap of −4.32-4.32.
  4. The gap multiplies by −1.2-1.2 each step, so its size grows without limit.

Interpretation: the same algorithm converges, lands, or explodes depending only on η\eta. For a quadratic like this one, the safe range is 0<η<10 \lt \eta \lt 1.

Example
Fitting a line to three points

This is a realistic case: fit y^=wx+b\hat{y} = wx + b to the points (1,2)(1, 2), (2,3)(2, 3), (3,5)(3, 5) by minimising the mean squared error L=13∑(y^i−yi)2L = \frac{1}{3}\sum (\hat{y}_i - y_i)^2. The gradients are ∂L∂w=23∑rixi\frac{\partial L}{\partial w} = \frac{2}{3}\sum r_i x_i and ∂L∂b=23∑ri\frac{\partial L}{\partial b} = \frac{2}{3}\sum r_i, where ri=y^i−yir_i = \hat{y}_i - y_i.

  1. Start at w=0w = 0, b=0b = 0. Residuals are −2,−3,−5-2, -3, -5, and the loss is L=38/3≈12.67L = 38/3 \approx 12.67.
  2. Gradient in ww: 23((−2)(1)+(−3)(2)+(−5)(3))=23(−23)≈−15.33\frac{2}{3}\big((-2)(1) + (-3)(2) + (-5)(3)\big) = \frac{2}{3}(-23) \approx -15.33.
  3. Gradient in bb: 23(−2−3−5)=23(−10)≈−6.67\frac{2}{3}(-2 - 3 - 5) = \frac{2}{3}(-10) \approx -6.67.
  4. Step with η=0.05\eta = 0.05: w1=0+0.05×15.33≈0.767w_1 = 0 + 0.05 \times 15.33 \approx 0.767, b1=0+0.05×6.67≈0.333b_1 = 0 + 0.05 \times 6.67 \approx 0.333.
  5. New predictions are 1.101.10, 1.871.87, 2.632.63. The loss falls from 12.6712.67 to about 2.572.57.
  6. The exact least-squares answer is w=1.5w = 1.5, b=1/3b = 1/3. Repeated steps move (w,b)(w, b) 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

SituationWhat goes wrongBetter choice
Learning rate too largeLoss oscillates or explodesLower η\eta, or use a schedule that decays it
Learning rate too smallTraining takes foreverRaise η\eta, or use momentum
Long narrow valleySteps zigzag across the valley and crawl along itMomentum or an adaptive method such as [[adam-optimizer
Huge datasetOne full-data gradient per step is too slow[[stochastic-gradient-descent
Vanishing gradients in deep netsGradients near zero in early layers, so they barely learnBetter activations, normalisation, or residual connections (see Vanishing Gradient Problem)
Minimising training loss onlyModel memorises the data and fails on new dataRegularisation 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 η\eta times that gradient in the opposite direction. The gradient points uphill, so the step goes downhill, and the learning rate η\eta sets how far.

QuestionFor f(x)=(x−3)2f(x) = (x - 3)^2 with η=0.25\eta = 0.25, starting at x0=0x_0 = 0, compute x1x_1.Show answer

The gradient at 0 is f′(0)=2(0−3)=−6f'(0) = 2(0 - 3) = -6. The update is x1=0−0.25×(−6)=1.5x_1 = 0 - 0.25 \times (-6) = 1.5. The distance to 3 went from 3 to 1.5, which matches the factor 1−2η=0.51 - 2\eta = 0.5.

QuestionWhy does a learning rate that is too large make the loss go up rather than down?Show answer

The first-order approximation f(a+h)≈f(a)+∇f⋅hf(\mathbf{a} + \mathbf{h}) \approx f(\mathbf{a}) + \nabla f \cdot \mathbf{h} 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, η>1\eta \gt 1 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.

Sources