Gradient Descent, Visually Explained

ML
Optimization
See how the learning rate shapes the path to a minimum with an interactive chart you can adjust.
Author

MLStation

Published

September 26, 2026

Almost every modern model, from logistic regression to large language models, is trained by some variant of gradient descent. The idea is simple: walk downhill.

The update rule

Given a loss \mathcal{L}(\theta), we repeatedly step against the gradient:

\theta_{t+1} = \theta_t - \eta \, \nabla_\theta \mathcal{L}(\theta_t)

Here \eta is the learning rate. Too small and training crawls; too large and it overshoots or diverges.

Try it yourself

Move the slider to change \eta and watch 15 steps on f(x) = x^2, starting from x_0 = 4.

ImportantWhat to notice

For f(x) = x^2 the update is x_{t+1} = (1 - 2\eta)\,x_t. It converges only when |1 - 2\eta| < 1, meaning 0 < \eta < 1. Push the slider past 1.0 and the steps fly apart.

In code

def gradient_descent(grad, theta0, lr=0.1, steps=100, tol=1e-8):
    theta = theta0
    for t in range(steps):
        step = lr * grad(theta)
        theta -= step
        if abs(step) < tol:
            break
    return theta

gradient_descent(lambda x: 2 * x, theta0=4.0, lr=0.3)   # → ~0.0

Variants you will meet

Variant Gradient computed on Typical use
Batch GD Full dataset Small, convex problems
Stochastic GD One example Online learning
Mini-batch GD A small batch Deep learning (default)
Adam Mini-batch + adaptive moments Transformers, most modern nets
Back to top