Octonote AI
Make your own →

Gradient descent, from intuition to update rule

Sunil Tiwari · Oct 12, 2026 · 6 min read

Why stepping against the gradient works, how the learning rate changes everything, and a 10-line implementation.

Gradient descent is how almost every model learns, from a two-parameter line fit to a language model with hundreds of billions of weights. The idea is simple: stand somewhere on a loss surface, find which way is downhill, take a small step, and repeat. This page builds the update rule from first principles, works an example by hand, and ends with the optimisers people actually use.

The setup

A model has parameters θ\theta (all its weights, stacked into one vector) and a loss L(θ)L(\theta) that measures how wrong its predictions are on the training data. Training means finding:

θ∗=arg⁡min⁡θL(θ)\theta^{*} = \arg\min_{\theta} L(\theta)

For anything bigger than a toy, there is no formula for θ∗\theta^{*}. What we can compute cheaply is the gradient ∇L(θ)\nabla L(\theta), the vector of partial derivatives. It points in the direction in which the loss rises fastest. Gradient descent uses only that local information, over and over.

The loop

The gradient descent loop
The gradient descent loop

Every training run, however large, is this loop:

  1. Initialise the parameters, usually with small random values.
  2. Sample a batch of training examples.
  3. Forward pass: compute predictions and the loss on the batch.
  4. Backward pass: compute ∇L(θ)\nabla L(\theta) with backpropagation.
  5. Update the parameters with a small step against the gradient.
  6. Repeat until the loss stops improving.

Why the negative gradient?

Take a small step Δ\Delta from where you stand. A first-order Taylor expansion says how the loss changes:

L(θ+Δ)≈L(θ)+∇L(θ)⊤ΔL(\theta + \Delta) \approx L(\theta) + \nabla L(\theta)^{\top}\Delta

To decrease the loss as much as possible, we want ∇L(θ)⊤Δ\nabla L(\theta)^{\top}\Delta as negative as possible. By the Cauchy-Schwarz inequality, among all steps of a fixed length ∥Δ∥\lVert \Delta \rVert:

∇L(θ)⊤Δ  ≥  −∥∇L(θ)∥ ∥Δ∥\nabla L(\theta)^{\top}\Delta \;\ge\; -\lVert \nabla L(\theta) \rVert \, \lVert \Delta \rVert

with equality exactly when Δ\Delta points opposite the gradient. So the steepest way down is −∇L(θ)-\nabla L(\theta), and the update rule is:

θt+1=θt−η ∇L(θt)\theta_{t+1} = \theta_{t} - \eta \, \nabla L(\theta_{t})

Here η>0\eta > 0 is the learning rate, which controls the step size. The Taylor approximation only holds near θ\theta, which is why the steps must stay small.

A worked example

Take the simplest possible loss, L(θ)=θ2L(\theta) = \theta^2, whose minimum is at θ=0\theta = 0. Its gradient is ∇L=2θ\nabla L = 2\theta, so the update becomes:

θt+1=θt−η⋅2θt=(1−2η) θt\theta_{t+1} = \theta_t - \eta \cdot 2\theta_t = (1 - 2\eta)\,\theta_t

Start at θ0=4\theta_0 = 4 with η=0.25\eta = 0.25. The factor is 1−2(0.25)=0.51 - 2(0.25) = 0.5, so each step halves the distance to the minimum:

θ: 4→2→1→0.5→0.25→⋯→0\theta:\ 4 \to 2 \to 1 \to 0.5 \to 0.25 \to \cdots \to 0

The loss falls from 16 to 4 to 1 to 0.25. Notice that the steps shrink on their own: the gradient gets smaller as we approach the bottom, so a fixed learning rate still slows down near the minimum.

The learning rate decides everything

The same loss, the same starting point, three learning rates:

Learning rate on L(θ) = θ², starting at θ = 4
Learning rate on L(θ) = θ², starting at θ = 4
Learning rateFactor 1−2η1 - 2\etaWhat happens
η=0.25\eta = 0.250.50.5Halves the distance each step. Smooth convergence.
η=0.5\eta = 0.500Lands exactly on the minimum in one step.
η=1\eta = 1−1-1θ\theta flips between 4 and −4-4 forever.
η=1.25\eta = 1.25−1.5-1.5Each step overshoots further. 4,−6,9,−13.5,…4, -6, 9, -13.5, \dots Diverges.

For this loss, gradient descent converges exactly when ∣1−2η∣<1\lvert 1 - 2\eta \rvert < 1, that is 0<η<10 < \eta < 1. In general the limit depends on curvature. Near a minimum, a loss behaves like a quadratic with Hessian HH, and plain gradient descent converges only if:

η<2λmax⁡(H)\eta < \frac{2}{\lambda_{\max}(H)}

where λmax⁡\lambda_{\max} is the largest eigenvalue, the steepest direction of curvature. For θ2\theta^2 the second derivative is 2, which gives back η<1\eta < 1. Too small a learning rate wastes compute. Too large a learning rate diverges. Real training runs often warm the rate up at the start and decay it towards the end.

Batch, stochastic and mini-batch

The training loss is an average over NN examples, so its gradient is too:

∇L(θ)=1N∑i=1N∇ℓi(θ)\nabla L(\theta) = \frac{1}{N} \sum_{i=1}^{N} \nabla \ell_i(\theta)

Computing all NN terms for every step is expensive when NN is in the millions. The practical answer is to estimate the gradient from a random subset B\mathcal{B} of size BB:

∇L(θ)≈1B∑i∈B∇ℓi(θ)\nabla L(\theta) \approx \frac{1}{B} \sum_{i \in \mathcal{B}} \nabla \ell_i(\theta)

This estimate is unbiased: on average it equals the true gradient. Its noise shrinks as BB grows, roughly like 1/B1/\sqrt{B}.

Three ways to estimate the gradient
Three ways to estimate the gradient
  • Batch gradient descent uses all NN examples. Exact, but one step costs a full pass over the data.
  • Stochastic gradient descent (SGD) uses one example. Cheap steps, very noisy path.
  • Mini-batch SGD uses a batch of tens to hundreds. It balances noise against cost and keeps GPUs busy. When people say "SGD" today, they almost always mean this.

The noise is not only a cost. It helps the optimiser rattle out of sharp, narrow minima and saddle points.

Beyond plain gradient descent

Plain gradient descent struggles in long, narrow valleys: it zig-zags across the steep walls while creeping along the gentle floor. Two ideas fix most of this, and modern optimisers combine them.

From gradient descent to Adam
From gradient descent to Adam

Momentum keeps a running average of past gradients, so consistent directions build up speed and oscillations cancel out:

vt+1=βvt+∇L(θt),θt+1=θt−η vt+1v_{t+1} = \beta v_t + \nabla L(\theta_t), \qquad \theta_{t+1} = \theta_t - \eta\, v_{t+1}

with β\beta typically 0.9. Nesterov momentum evaluates the gradient at the point momentum is about to carry you to, which reacts a little sooner when the slope changes.

Per-parameter step sizes (AdaGrad, then RMSProp) divide each parameter's step by the size of its recent gradients. Parameters with large, noisy gradients take smaller steps. Rarely updated parameters take larger ones.

Adam combines both. With gradient gtg_t at step tt:

mt=β1mt−1+(1−β1) gt,vt=β2vt−1+(1−β2) gt2m_t = \beta_1 m_{t-1} + (1 - \beta_1)\, g_t, \qquad v_t = \beta_2 v_{t-1} + (1 - \beta_2)\, g_t^2
m^t=mt1−β1t,v^t=vt1−β2t,θt=θt−1−η m^tv^t+ϵ\hat m_t = \frac{m_t}{1 - \beta_1^{t}}, \qquad \hat v_t = \frac{v_t}{1 - \beta_2^{t}}, \qquad \theta_{t} = \theta_{t-1} - \eta\, \frac{\hat m_t}{\sqrt{\hat v_t} + \epsilon}

The defaults β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999 and ϵ=10−8\epsilon = 10^{-8} work surprisingly often. The hats correct the bias from starting mm and vv at zero. AdamW applies weight decay directly to the parameters instead of folding it into the gradient, and it is the default for training Transformers.

Ten lines of code

def gradient_descent(grad, theta, lr=0.1, steps=1000, tol=1e-8):
    for _ in range(steps):
        step = lr * grad(theta)
        theta = theta - step
        if abs(step) < tol:          # stop when updates become negligible
            break
    return theta

gradient_descent(lambda t: 2 * t, theta=4.0, lr=0.25)    # ~0.0, converges
gradient_descent(lambda t: 2 * t, theta=4.0, lr=1.25, steps=10)   # ~230, diverging

Key takeaways

  • The gradient points uphill, so stepping along −∇L-\nabla L is the locally steepest way down.
  • The update rule is θ←θ−η∇L(θ)\theta \leftarrow \theta - \eta \nabla L(\theta), repeated until the loss stops improving.
  • The learning rate must respect the curvature: η<2/λmax⁡\eta < 2 / \lambda_{\max}, or the iterates oscillate and diverge.
  • Mini-batches make each step cheap, and their noise is mostly harmless and sometimes helpful.
  • Momentum smooths the direction, adaptive methods scale the step per parameter, and Adam does both.
Make a page like this. Free.Notes and diagrams side by side, written by you or by Claude Code and Codex. Publish it in one click.
Make your own, free