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 θ (all its weights, stacked into one vector) and a loss L(θ) that measures how wrong its predictions are on the training data. Training means finding:
For anything bigger than a toy, there is no formula for θ∗. What we can compute cheaply is the gradient ∇L(θ), 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

Every training run, however large, is this loop:
- Initialise the parameters, usually with small random values.
- Sample a batch of training examples.
- Forward pass: compute predictions and the loss on the batch.
- Backward pass: compute ∇L(θ) with backpropagation.
- Update the parameters with a small step against the gradient.
- Repeat until the loss stops improving.
Why the negative gradient?
Take a small step Δ from where you stand. A first-order Taylor expansion says how the loss changes:
To decrease the loss as much as possible, we want ∇L(θ)⊤Δ as negative as possible. By the Cauchy-Schwarz inequality, among all steps of a fixed length ∥Δ∥:
with equality exactly when Δ points opposite the gradient. So the steepest way down is −∇L(θ), and the update rule is:
Here η>0 is the learning rate, which controls the step size. The Taylor approximation only holds near θ, which is why the steps must stay small.
A worked example
Take the simplest possible loss, L(θ)=θ2, whose minimum is at θ=0. Its gradient is ∇L=2θ, so the update becomes:
Start at θ0=4 with η=0.25. The factor is 1−2(0.25)=0.5, so each step halves the distance to the minimum:
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 | Factor 1−2η | What happens |
| η=0.25 | 0.5 | Halves the distance each step. Smooth convergence. |
| η=0.5 | 0 | Lands exactly on the minimum in one step. |
| η=1 | −1 | θ flips between 4 and −4 forever. |
| η=1.25 | −1.5 | Each step overshoots further. 4,−6,9,−13.5,… Diverges. |
For this loss, gradient descent converges exactly when ∣1−2η∣<1, that is 0<η<1. In general the limit depends on curvature. Near a minimum, a loss behaves like a quadratic with Hessian H, and plain gradient descent converges only if:
where λmax is the largest eigenvalue, the steepest direction of curvature. For θ2 the second derivative is 2, which gives back η<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 N examples, so its gradient is too:
Computing all N terms for every step is expensive when N is in the millions. The practical answer is to estimate the gradient from a random subset B of size B:
This estimate is unbiased: on average it equals the true gradient. Its noise shrinks as B grows, roughly like 1/B.

- Batch gradient descent uses all N 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.

Momentum keeps a running average of past gradients, so consistent directions build up speed and oscillations cancel out:
with β 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 gt at step t:
The defaults β1=0.9, β2=0.999 and ϵ=10−8 work surprisingly often. The hats correct the bias from starting m and v 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, divergingKey takeaways
- The gradient points uphill, so stepping along −∇L is the locally steepest way down.
- The update rule is θ←θ−η∇L(θ), repeated until the loss stops improving.
- The learning rate must respect the curvature: η<2/λ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.