All articles

Gradient descent, felt rather than memorized

Build intuition for slopes, step sizes and why the loss sometimes explodes, before touching any framework.

Math foundationsBeginner1 min read

Training a model means adjusting numbers, its parameters, so that a loss gets smaller. The loss measures how wrong the model is. Gradient descent is the method almost every model uses to make it smaller.

Downhill in the dark

Picture standing on a hillside in thick fog. You cannot see the valley, but you can feel which way the ground slopes under your feet. The sensible move is to take a step in the steepest downhill direction, then feel again.

The gradient is that slope. For a single parameter xx it is the derivative dLdx\frac{dL}{dx}. Gradient descent repeats one update:

x←x−η dLdxx \leftarrow x - \eta \, \frac{dL}{dx}

where η\eta (eta) is the learning rate, the size of each step.

Three ways it can go

Take the simplest possible loss, L(x)=x2L(x) = x^2. Its gradient is 2x2x, so each update becomes x←x(1−2η)x \leftarrow x(1 - 2\eta).

Learning rateWhat happens to x each stepResult
0.05multiplied by 0.9Slow, steady progress
0.4multiplied by 0.2Fast convergence
0.9multiplied by −0.8Bounces across the minimum, still converges
1.1multiplied by −1.2Diverges, the loss grows every step

Try these yourself in the interactive demo on the home page.

Why real training is harder

Real losses have millions of parameters and are not neat bowls. Some directions are steep and others nearly flat, so one learning rate can be too big for one parameter and too small for another. Optimizers such as Adam keep a running estimate of each parameter’s gradient size and scale its step accordingly.

x, lr = -3.5, 0.4
for step in range(10):
    grad = 2 * x
    x -= lr * grad
    print(step, round(x, 4), round(x * x, 4))

Written by Thahira S

Tech futurist exploring how AI and machine learning can make everyday life healthier, more creative and more human. More about AIERA