Skip to content
mentoor

Topic 1 of 5 · 4 min read

Gradient descent steps by hand

Goal: Perform three gradient descent steps by hand on a one-parameter model and predict the loss after each.

Gradient descent changes a weight a little in the direction that lowers the loss, and repeats: new weight = old weight minus learning rate times gradient. Every neural network, from a linear regression to a large language model, trains with this one rule. Doing it by hand once removes the mystery.

Check yourself · 1 of 3

What is the gradient descent update rule for a weight w?

Choose one answer

Why it matters

When a training run misbehaves, you debug with this rule. If you know what the gradient and the learning rate each do to one weight, a loss curve that explodes or crawls stops being magic. You can also catch an agent that writes a training loop with the sign wrong.

The setup

Take the smallest model: prediction = w * x, one weight. One training example: x = 2, true answer y = 6. The loss is the squared error, (w*x - y)^2. The best weight is 3.

  • Gradient. The slope of the loss with respect to w. By the chain rule it is 2 * (w*x - y) * x. A negative slope means raising w lowers the loss.
  • Learning rate (lr). How far to move along the slope. Use 0.05.
  • Update. w = w - lr * gradient.

Three steps

Start at w = 0. The prediction is 0, the loss is 36, and the gradient is 2 * (-6) * 2 = -24. Update: w = 0 - 0.05 * (-24) = 1.2. New loss: (2.4 - 6)^2 = 12.96.

Step 2: gradient 2 * (-3.6) * 2 = -14.4, so w = 1.2 + 0.72 = 1.92, loss 4.67. Step 3: gradient -8.64, w = 2.352, loss 1.68.

Notice the pattern. Each step shrinks the error by the same factor, 0.6, so the loss shrinks by 0.36. The steps also get smaller as the gradient flattens near the minimum. The weight approaches 3 and never needs a special stopping rule.

In code

w, x, y, lr = 0.0, 2.0, 6.0, 0.05
for step in range(3):
    grad = 2 * (w * x - y) * x
    w = w - lr * grad
    print(step + 1, round(w, 3), round((w * x - y) ** 2, 3))

Before and after

A weak loop writes w = w + lr * grad. That climbs the loss: from w = 0 it moves to -1.2 and the loss rises from 36 to 70.56. The fix is the minus sign. We subtract the gradient because the gradient points uphill.

What goes wrong

  • Wrong sign. Adding the gradient maximises the loss.
  • Gradient from a stale weight. Computing the gradient once and reusing it across steps gives steps that no longer match the loss.
  • Confusing the model with the data. The weights move; the data and labels stay fixed. If the loss never changes, check that the weight actually received the update.

Examples

Hand check of a training loop

A colleague's agent wrote a loop that prints loss 36.0, then 70.56. You run the one-step calculation by hand with lr = 0.05 and expect 12.96. The mismatch points to the update line: it adds the gradient. A five-minute hand check beats an hour of staring at curves.

Why the steps shrink

At w = 0 the gradient is -24 and the step is 1.2. At w = 2.352 the gradient is about -5.2 and the step is about 0.26. Same learning rate, much smaller move, because the slope is flatter near the minimum.

Key points

  • Gradient descent updates a weight with w = w - lr * gradient, over and over.
  • The gradient points uphill, so you subtract it to go downhill.
  • In the one-weight example the loss falls from 36 to 12.96 to 4.67 to 1.68, a factor of 0.36 per step.
  • Steps shrink on their own as the gradient flattens near the minimum.
  • A flipped sign in the update raises the loss instead of lowering it.

Common mistakes

  • Adding the gradient instead of subtracting it.
  • Forgetting the factor x from the chain rule when computing the gradient by hand.
  • Expecting the loss to reach exactly zero in a few steps; it shrinks geometrically.
  • Reusing an old gradient after the weight has changed.

Check yourself · 2 of 3

A model is prediction = w * x with x = 2, y = 6 and loss (w*x - y)^2. At w = 0 the gradient is -24. What does one step with learning rate 0.05 give for w?

Choose one answer

Check yourself · 3 of 3

In a training loop the loss goes up on every step while the code reads w = w + lr * grad. What is the most likely fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here