Back to Tutorials
Deep Learning
Beginner

Gradient Descent, Intuitively

How a model learns, shown as a ball rolling downhill. Set the step size, drop the ball, and see why one number decides whether training works.

10 minInteractive: drag, click and try things as you read


Every AI model you’ve heard of, from spam filters to ChatGPT, learned the same way: by making a guess, measuring how wrong it was, and nudging itself to be a little less wrong. Billions of times. That nudge is gradient descent, and you can see the whole idea with one ball and one hill.

Learning is walking downhill#

A model is a big pile of numbers called weights. Change the weights and you change what the model predicts. To train it, you need a way to score how wrong its predictions are on the examples you have. That score is called the loss: high means very wrong, zero means perfect.

Now picture a single weight, w. For every value it could take, there’s a loss. Plot them and you get a landscape. Training means finding the lowest point of that landscape without being able to see it: you only ever know the ground right under your feet.

So you do what you’d do on a dark hillside. Feel which way the ground slopes, and take a step downhill. The slope is the gradient. Stepping against it, again and again, is gradient descent.

Try it
The loss here is w², the simplest bowl there is, so its slope at any point is 2w. The yellow line shows the slope the ball feels. Drag on the chart to move the starting point.

Every step uses the same rule, printed under the chart: new w = w − learning rate × slope. Where the slope is steep, the ball takes a big step. As it nears the bottom the ground flattens, the slope shrinks and the steps get smaller on their own. That’s why the ball slows down and settles instead of rolling past.

The one number that decides everything#

The learning rate is how far to step for a given slope. It’s the single most important setting in training, and you just met its trade-off if you tried the presets: timid steps take forever, and bold ones overshoot.

Predict first

Start at w = −3.5 and set the learning rate to exactly 1. What will the ball do?

On this particular bowl you can work out the perfect learning rate by hand: at 0.5, one step lands exactly on the bottom. Real landscapes never give you that. In practice you try a few learning rates, watch the loss, and pick the biggest one that still goes down smoothly.

Not every landscape is a bowl#

Real loss landscapes have more than one dip. The ball only knows the ground under it, so it settles in whichever valley it happens to roll into, even if a deeper one sits right next door.

Try it
Two valleys: a shallow one on the right and a deeper one on the left. The ball starts on the right. Drag the start across the ridge, or try a bigger step from the far right edge.

Where you start decides where you finish. Training runs start from random weights, which is one reason two runs of the same model can end up slightly different.

Millions of knobs at once#

With two weights, the landscape becomes a surface, and the slope becomes an arrow pointing downhill on it. Gradient descent steps against that arrow, adjusting both weights at once. A real model does the same with every weight at the same time.

That creates a new problem. Some directions are steep and some are gentle, but plain gradient descent uses one learning rate for all of them. Here’s a long, narrow valley, viewed from above. The lowest point is in the middle.

Race
Both start in the same place. The rings are contour lines, like on a hiking map: each ring is one height. Then push the plain step size above 0.050 and race again.

Plain gradient descent (rose) zigzags across the steep walls while it crawls along the gentle floor. Its step size is stuck: anything bigger and the steep direction overshoots and blows up, anything smaller and the gentle direction barely moves. The steepest direction sets the speed limit for every weight.

Adam (green) fixes that. It keeps a running average of each weight’s recent slopes and scales each weight’s step by how big its slopes have been. Weights on steep walls take careful steps, weights on gentle slopes take confident ones, and the zigzags cancel out. It’s the default optimizer for most deep learning today.

One more name you’ll hear: SGD, stochastic gradient descent. Instead of measuring the slope on all the training data before every step, it uses a small random batch of examples. Each step is a bit noisy, but you get many more of them for the same effort, and in practice that wins.

See it in code#

The ball in the first figure is six lines of Python. No libraries needed:

w = -3.5    # where the ball starts
lr = 0.2    # the learning rate

for step in range(20):
    slope = 2 * w           # the slope of loss = w**2
    w = w - lr * slope      # one step downhill
    print(step + 1, round(w, 4))

In a real project you never work out the slope by hand. PyTorch does it for you, for every weight in the model, with loss.backward(). Here is the same bowl:

import torch

w = torch.tensor(-3.5, requires_grad=True)
optimizer = torch.optim.SGD([w], lr=0.2)   # try torch.optim.Adam too

for step in range(20):
    loss = w ** 2
    optimizer.zero_grad()
    loss.backward()      # works out the slope
    optimizer.step()     # w = w - lr * slope

print(w.item())

Swap the one line, w ** 2, for “how wrong is the model on this batch of examples”, and you have the training loop behind every neural network.

What to remember

  • Training means lowering the loss, a score of how wrong the model is, by adjusting its weights.
  • Gradient descent feels the slope and steps downhill: new w = w − learning rate × slope. Repeat.
  • The learning rate decides everything: too small crawls, too big diverges. Adam gives every weight its own step size, which is why it’s the default.

Keep going

Sign in to save your place and keep a learning streak. Free, with Google.