← Back
FiledApril 22, 2026·21 min read·By The Editor

Gradient Descent, By Hand

One knob, one data point, one hill. Watch gradient descent find the right answer in five steps of plain arithmetic, with every symbol in the calculus translated into English.

optimizationintuitioncalculus

Every explanation of gradient descent shows you a marble rolling down a smooth bowl, and then the article ends. The marble is doing physics. Gradient descent is doing arithmetic — six numbers and a subtraction, repeated.

So let us do the arithmetic. One knob, one data point, five steps, every symbol decoded. By the end of this page you will have run the algorithm that trains every neural network on earth, on paper, and you will know exactly what each piece of the notation is asking for.

The whole idea, in one sentence

You are standing on a hillside in thick fog. You cannot see the bottom. But you can feel which way the ground tilts under your boots — so you step that way, and then you do it again.

Figure 1 · the whole idea
A walker in fog on a hillsideA hillside descending to the right into a flat basin. A walker stands partway up, inside a small circle of visibility, with an arrow pointing downhill. Fog hides the rest of the slope. downhill all you can feel: the tilt right here fog fog the bottom (you cannot see it)
You cannot see the bottom of the hill. You can feel the tilt of the ground under your feet. So you step downhill and repeat. That is gradient descent. The rest of this page is that same move, written in arithmetic.

That is it. That is gradient descent. Everything that follows — the squared loss, the derivative, the Greek letter — is bookkeeping for two questions:

  1. Which way is downhill? (that's the derivative)
  2. How big a step should I take? (that's the learning rate)

Answer those two, take the step, repeat.

Step 1 — We have exactly one knob

Our model is the simplest thing that can still be called a model: multiply the input by a number.

y^=w⋅x\hat{y} = w \cdot x
In plain English

Take the input x, multiply it by w, and call that your guess. The little hat on y^\hat{y} means guessed, as opposed to plain yy, which means actually true. That hat is the only difference between a prediction and a fact.

w is the knob. It is the only thing we are allowed to change. Training this model means finding the best number to put in that one slot.

Step 2 — One data point, one miss

Here is our entire dataset. One row:

x=2,y=5x = 2, \qquad y = 5

Someone gave us an input of 2 and told us the right answer is 5. We start the knob at w=1w = 1, because we have to start somewhere and 1 is a number.

y^=w⋅x=1⋅2=2\hat{y} = w \cdot x = 1 \cdot 2 = 2

We guessed 2. The truth was 5. We are off by 3.

Figure 2 · the miss
One data point and two candidate linesA scatter plot with a single point at x equals 2, y equals 5. A blue line for w equals 1 passes well below it; a green line for w equals 2.5 passes exactly through it. A dashed vertical bar of length 3 marks the miss. 1 2 3 4 5 6 7 1 2 3 x — the input y — the answer w = 2.5 → perfect w = 1 → our guess miss = 3 the one data point (2, 5) we predicted 2
One data point. One line through the origin whose steepness is the knob w. At w = 1 the line predicts 2 where the truth is 5, so the miss is 3. At w = 2.5 the line goes right through the point and the miss is 0. Gradient descent has to find that 2.5 without being told.

The line we are drawing is pinned at the origin, so the only thing ww controls is how steep it is. Turn ww up and the line swings up toward the point; turn it down and the line droops away. Somewhere in between, the line passes right through. Our job is to find that somewhere.

Step 3 — Wrongness is the miss, squared

We need a single number that says how badly we are doing. The obvious candidate — the miss, y−y^y - \hat{y} — has a problem: missing high gives a negative number and missing low gives a positive one, so two mistakes could cancel out and look like success.

Fix: square it.

L(w)=(y−y^)2=(y−wx)2=(5−2w)2L(w) = (y - \hat{y})^2 = (y - wx)^2 = (5 - 2w)^2
In plain English

L is the loss — our score for wrongness, where lower is better and zero is perfect. It is written L(w)L(w), "L of w", because the loss depends on where the knob is set. Move the knob, get a different loss.

Figure 3 · why we square it
Three squares showing miss squaredSquares of side 1, 2 and 3 drawn to scale, with areas 1, 4 and 9. Tripling the miss makes the penalty nine times larger. miss = 1 loss = 1 miss = 2 loss = 4 miss = 3 loss = 9 three times the miss, nine times the penalty
Squaring does two jobs. It throws away the sign, so missing high and missing low are both just wrong. And it punishes big misses far harder than small ones — which is exactly the priority you want while learning.

At our starting knob setting w=1w = 1:

L(1)=(5−2⋅1)2=32=9L(1) = (5 - 2 \cdot 1)^2 = 3^2 = 9

Our wrongness is 9. We would like it to be less.

Step 4 — Every knob setting has a wrongness. Draw them all.

Nothing stops us from asking: what would the loss be at w=0w = 0? At w=3w = 3? At every value in between? Plot the answers and you get a curve.

Figure 4 · the hill, drawn out
The loss curve for every setting of wA U-shaped curve of loss against w. It bottoms out at w equals 2.5 with loss 0. A marker sits at w equals 1 where the loss is 9, on the left wall of the U, with an arrow pointing downhill to the right. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are the answer: w = 2.5, loss 0 you are here w = 1, loss = 9 this way
Every possible setting of the knob, with its wrongness. This is the same hill as Figure 1 — only now it is a picture of arithmetic instead of scenery. We are standing at w = 1 on the left wall. We never get to see this curve. We only ever get the tilt at the exact spot we are standing on.

This is the hill from Figure 1, drawn honestly. And here is the important part, the part that makes the fog metaphor accurate rather than decorative:

The model never gets to see this curve. Drawing it required us to know the answer already. All the algorithm ever gets is the ground under its feet: the loss where it is standing, and the tilt where it is standing.

Step 5 — The tilt is the derivative, and the notation is not scary

The tilt of the curve at the exact point where you stand has a name. It is the derivative, and it is written like this:

dLdw\frac{dL}{dw}

That fraction intimidates people, so let us take it apart completely.

Reading dLdw\frac{dL}{dw} out loud
dd
a hair of change
LL
in the loss
(top)
/\big/
per
dd
a hair of change
ww
in the knob
(bottom)
Say it as: “nudge the knob a hair — how much does the wrongness move?” That is the whole meaning. The dd is not a number and it is not multiplying anything; it is shorthand for an infinitely small change in.
Figure 5 · the derivative is just this slope
The tangent line at w equals 1The same U-shaped loss curve with a straight line just touching it at w equals 1. A right triangle on that line shows that moving 0.5 to the right drops the line 6, giving a slope of minus 12. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are across 0.5 down 6 slope = −12 −6 ÷ 0.5 = −12 negative slope = the ground falls away to the right, so the bottom must be to the right
Lay a straight stick on the curve so it touches at one point. The steepness of that stick is the derivative. Here it falls 6 for every 0.5 you move right, so the slope is −12. That single number is the entire output of the calculus.

Geometrically it is dead simple: lay a straight stick on the curve so it touches at exactly one point. The steepness of that stick is the derivative. On our curve at w=1w = 1, moving right by 0.5 drops you by 6, so the tilt is −6÷0.5=−12-6 \div 0.5 = -12.

Now, where does that number come from without drawing anything? Two rules, applied once each.

Our loss is something squared: L=(5−2w)2L = (5 - 2w)^2. Give the inside its own name, u=5−2wu = 5 - 2w, so that L=u2L = u^2.

Rule one — the power rule. The derivative of u2u^2 is 2u2u. (Squares grow at twice their side length. Widen a square by a hair and you add two new strips of area, one on each open edge.)

dLdu=2u\frac{dL}{du} = 2u

Rule two — the chain rule. But uu itself moves when ww moves, so we need that rate too. u=5−2wu = 5 - 2w shrinks by 2 for every 1 that ww grows:

dudw=−2\frac{du}{dw} = -2

The chain rule says these rates simply multiply — if the loss moves twice as fast as uu, and uu moves twice as fast as ww in the other direction, then per unit of ww you get both effects at once:

dLdw=dLdu⋅dudw=2u⋅(−2)=−4u=−4(5−2w)\frac{dL}{dw} = \frac{dL}{du} \cdot \frac{du}{dw} = 2u \cdot (-2) = -4u = -4(5 - 2w)
In plain English

The chain rule is gear ratios. Turn gear A once, gear B turns twice, gear C turns three times per turn of B — so gear C turns six times per turn of A. You multiply the ratios along the chain. Deep learning calls this same move backpropagation; a hundred-layer network is a hundred gears, and the chain rule walks the ratios back through all of them.

Plug in our starting knob, w=1w = 1:

dLdw∣w=1=−4(5−2⋅1)=−4⋅3=−12\frac{dL}{dw}\Big|_{w=1} = -4(5 - 2 \cdot 1) = -4 \cdot 3 = -12

Same −12-12 we read off the picture. And the sign is the entire message: negative slope means the ground falls away to the right, so the bottom is to the right, so we should increase ww.

Step 6 — The forward pass and the backward pass

We just got that −12-12 with algebra. Real frameworks do not use algebra. They use a computation graph, and once you see it, backpropagation stops being a mystery — it is the chain rule with somewhere to put the numbers.

Write our model out as a graph of tiny operations, one node per arithmetic step:

Figure 6 · the computation graph
The computation graph, forward and backwardA left-to-right graph: x and w feed a multiply node producing the prediction 2; that and y feed a subtract node producing the miss 3; that feeds a squaring node producing the loss 9. Underneath, an orange lane runs right to left, starting at 1 and multiplying by each node's local slope: 6, then minus 1, then 2, arriving at the knob with minus 12. ŷ = 2 y = 5 miss = 3 x = 2input w = 1the knob y = 5the truth ×multiply −subtract ( )²square L = 9the loss FORWARD PASS → guess, then score it start: 1 × 2u = 6 carrying 6 × (−1) carrying −6 × x = 2 carrying −12 ← BACKWARD PASS slopes flow left, multiplying as they go dL/dw = 6 × (−1) × 2 = −12 the same −12 as the tangent line and the algebra
The forward pass walks left to right and produces one number: the loss. The backward pass walks the same graph right to left, carrying a running slope and multiplying by each node’s local slope as it goes past. It reaches the knob holding −12 — exactly the number gradient descent asked for.

The forward pass

Feed the numbers in on the left and let them flow right. Multiply xx by ww to get the prediction. Subtract that from yy to get the miss. Square the miss to get the loss. You come out the far end holding one number: L=9L = 9.

In plain English

The forward pass is just "run the model and see how badly it did." Every value on every wire gets computed and kept. No learning has happened yet — this is also exactly what runs when you are simply using a trained model.

The backward pass

Now walk the same graph backwards. Start at the loss carrying the number 1 — "how much does the loss change when the loss changes" — and every time you pass a node, multiply by that node's local slope.

passing backward throughthe node's local slopewhat you are now carrying
the loss itself—11
the squaring nodedLdu=2u=6\dfrac{dL}{du} = 2u = 666
the subtract nodedudy^=−1\dfrac{du}{d\hat{y}} = -1−6-6
the multiply nodedy^dw=x=2\dfrac{d\hat{y}}{dw} = x = 2−12\mathbf{-12}

You arrive at the knob holding −12-12. Same number we read off the tangent line in Figure 5, same number the algebra gave us. Three routes, one answer — which is exactly the point.

In plain English

The backward pass is backpropagation. It is not a different idea from the chain rule; it is the chain rule with an order of operations and a place to keep the partial results. Start at the loss holding a 1, walk backwards, multiply by each local slope, and whatever you are holding when you reach a knob is that knob’s gradient.

Why frameworks do it this way

Three reasons, and all three are why loss.backward() exists at all.

Each node only needs to know its own slope. The squaring node knows one fact: "my slope is 2u2u." It knows nothing about ww, nothing about the loss, nothing about what comes next. That is why you can invent a new kind of layer, teach it one local rule, and drop it into a network that already works.

The forward pass has to be remembered. Look again at the squaring node's local slope, 2u2u — it needs the value of uu that came through on the way forward. Every node is like this. That is why training a model eats several times the memory of running one: the backward pass feeds on the forward pass's leftovers, so every intermediate value has to be kept alive until its gradient is taken.

One backward walk serves every knob at once. Our graph has a single knob, so the saving is invisible here. Give it a billion knobs and a backward pass still costs roughly what a forward pass costs — about the same work again, not a billion times more. The obvious alternative, nudging each knob a hair and re-running the model to see what happened, would cost a billion forward passes per step. That one fact is why deep learning is possible at all.

In plain English

Two words that get used interchangeably and should not be. Backpropagation computes the slopes. Gradient descent spends them. Swap gradient descent out for Adam or momentum and backpropagation does not change by one line — only what you do with its answer changes.

So the training loop, in the order it actually happens:

  1. Forward — push a batch of data through the graph, land on a loss.
  2. Backward — walk back through the same graph, collecting one slope per knob.
  3. Step — apply w←w−ηdLdww \leftarrow w - \eta \frac{dL}{dw} to every knob, using the slopes you just collected.
  4. Repeat — next batch, a few hundred thousand times.

Backpropagation is step 2, and only step 2. It computes slopes and never moves a single knob. Gradient descent is step 3: it moves the knobs and does no calculus. The two get said in the same breath so often that people assume they are one thing. They are two, and the graph is what hands the first one's answer to the second.

Step 7 — The update rule, symbol by symbol

Here is the rule that does the work. It is one line, and once decoded there is nothing left in it.

wnew=wold−η dLdww_{\text{new}} = w_{\text{old}} - \eta \, \frac{dL}{dw}
The update rule, symbol by symbol
wneww_{\text{new}}
the knob
after the step
==
woldw_{\text{old}}
the knob
right now
−-
go the opposite way to the slope
η\eta
"eta" — the learning rate, how far you trust one step
×\times
dLdw\frac{dL}{dw}
the slope under your feet
Say it as: “new knob = old knob, minus a small fraction of the slope.” The minus sign is the only clever part: the slope points uphill, and uphill is where we do not want to go.

The minus sign trips everybody, so state it plainly: the derivative points uphill. A positive slope means the loss rises as ww rises. We want the loss to fall, so we move against the slope — hence subtract. When the slope is negative, subtracting a negative moves us right. The sign handles itself; you never have to think about the direction again.

Step 8 — Do the arithmetic

We have every number we need. Set the learning rate to η=0.1\eta = 0.1, which is a guess, because it always is.

wnew=1−0.1⋅(−12)=1+1.2=2.2w_{\text{new}} = 1 - 0.1 \cdot (-12) = 1 + 1.2 = 2.2

The knob moves from 1 to 2.2. Check the new wrongness:

L(2.2)=(5−2⋅2.2)2=(5−4.4)2=0.62=0.36L(2.2) = (5 - 2 \cdot 2.2)^2 = (5 - 4.4)^2 = 0.6^2 = 0.36
Figure 7 · the step, taken
One step of gradient descentThe loss curve with a marker moving from w equals 1, loss 9, along a curved arrow to w equals 2.2, loss 0.36, close to the bottom of the U. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are one step: w moves +1.2 was: loss 9 now: loss 0.36 w = 2.2
Nine down to 0.36, in one move, using nothing but a subtraction and a multiplication. Not magic: the wall was steep and the bottom was close.

Nine down to 0.36, in one step. Not because anything clever happened — because the wall was steep there and the bottom was close. Do not expect this from a real model.

Step 9 — Do it four more times

Same three moves, over and over: measure the tilt, multiply by 0.1, subtract. Click through the steps.

Step 0 of 5
Gradient descent after 0 stepsThe loss curve with the marker at w equals 1, where the loss is 9. Earlier positions are shown faded. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are step 0 w = 1
knobw = 1
prediction2
miss3
loss9
slope dL/dw−12
next move+1.2
Where we start. The slope is steep (−12), so the first step is a big one.
Step 1 of 5
Gradient descent after 1 stepThe loss curve with the marker at w equals 2.2, where the loss is 0.36. Earlier positions are shown faded. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are step 1 w = 2.2
knobw = 2.2
prediction4.4
miss0.6
loss0.36
slope dL/dw−2.4
next move+0.24
One step in. The miss has shrunk from 3 to 0.6 — a fifth of what it was.
Step 2 of 5
Gradient descent after 2 stepsThe loss curve with the marker at w equals 2.44, where the loss is 0.0144. Earlier positions are shown faded. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are step 2 w = 2.44
knobw = 2.44
prediction4.88
miss0.12
loss0.0144
slope dL/dw−0.48
next move+0.048
The wall is gentler here, so the step is smaller. Same rule, no change.
Step 3 of 5
Gradient descent after 3 stepsThe loss curve with the marker at w equals 2.488, where the loss is 0.000576. Earlier positions are shown faded. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are step 3 w = 2.488
knobw = 2.488
prediction4.976
miss0.024
loss0.000576
slope dL/dw−0.096
next move+0.0096
The miss is down to 0.024. We are essentially standing in the basin.
Step 4 of 5
Gradient descent after 4 stepsThe loss curve with the marker at w equals 2.4976, where the loss is 2.304e-05. Earlier positions are shown faded. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are step 4 w = 2.4976
knobw = 2.4976
prediction4.9952
miss0.0048
loss2.304×10−5
slope dL/dw−0.0192
next move+0.00192
Loss is now about two hundred-thousandths. Nobody would keep going.
Step 5 of 5
Gradient descent after 5 stepsThe loss curve with the marker at w equals 2.49952, where the loss is 9.216e-07. Earlier positions are shown faded. 9 18 27 36 0 1 2 3 4 5 w — the one knob we can turn L — how wrong we are step 5 w = 2.49952
knobw = 2.49952
prediction4.99904
miss0.00096
loss9.216×10−7
slope dL/dw−0.00384
next move+0.000384
w = 2.49952 against a true answer of 2.5. Close enough is the point.

And the same five steps as a ledger:

stepknob wprediction 2wmiss 5−2wloss Lslope dL/dwmove −η·slope
01239−121.2
12.24.40.60.36−2.40.24
22.444.880.120.0144−0.480.048
32.4884.9760.0240.000576−0.0960.0096
42.49764.99520.00482.304×10−5−0.01920.00192
52.499524.999040.000969.216×10−7−0.003840.000384

Two things are worth staring at in that table.

The steps shrink on their own. Nobody told them to. The step size is the slope times 0.1, and the slope flattens as you approach the bottom — so the algorithm brakes automatically as it arrives. The first step moved the knob by 1.2; the fifth moved it by 0.000384.

The miss shrinks by the same factor every time. 3, then 0.6, then 0.12, then 0.024 — each one is a fifth of the last. That factor is set entirely by η\eta, which is the next section.

Step 10 — The learning rate is the whole ballgame

η\eta (eta) is the only number in this algorithm that you have to choose, and nothing tells you what it should be. Here is the same hill, the same rule, the same starting knob, with three different values.

Figure 8 · the learning rate
η = 0.01 — too small
η = 0.01 — too smallFive tiny steps that barely move along the loss curve. 0 2 4 6 answer
Each step shaves only 8% off the miss. You need about 41 steps to get the loss under 0.01. It works. You just wait.
η = 0.1 — about right
η = 0.1 — about rightFour steps that march neatly down into the bottom of the curve. 0 2 4 6 answer
Each step cuts the miss to a fifth. 3 steps to get the loss under 0.01, and it never overshoots.
η = 0.3 — too big
η = 0.3 — too bigThree steps that leap across the valley, each one landing further up the opposite wall and then off the chart. 0 2 4 6 off the chart → answer
Each step jumps clean over the bottom and lands higher than it started. The miss grows 40% per step until the numbers overflow.
Same hill, same rule, same starting point. The only thing that changed is how far you trust one step. Too small and you crawl. Too big and you fall up the other side. There is no formula for the right value — everyone guesses, then checks.

For our particular toy problem the arithmetic is clean enough to state exactly. Each step multiplies the remaining miss by (1−8η)(1 - 8\eta), which tells you everything:

  • η=0.125\eta = 0.125 — the miss is multiplied by zero. One step lands on the exact answer. (Nobody gets this lucky twice.)
  • η<0.125\eta < 0.125 — you undershoot every time and creep in. Safe.
  • 0.125<η<0.250.125 < \eta < 0.25 — you overshoot, land on the far wall, and bounce back in. Ugly but it converges.
  • η=0.25\eta = 0.25 — the miss is multiplied by −1-1. You ping-pong between two points forever.
  • η>0.25\eta > 0.25 — the miss grows every step. The model diverges into NaN and your training run is over.
In plain English

Too small and training takes a week that should have taken an hour. Too big and the loss climbs instead of falling. In practice you try a few values, watch whether the loss curve goes down, and adjust — which is why "the loss went to NaN" is the most common sentence in machine learning.

Step 11 — What changes in a real model

Everything you just did is what actually runs inside a large model. Four things get bigger; none of them get different.

More knobs

One knob gave us a curve. Two knobs give a landscape, and the tilt becomes a pair of numbers — how steep along knob 1, how steep along knob 2. That list of slopes, one per knob, is the gradient, written ∇L\nabla L.

Figure 9 · more than one knob
A loss surface with two knobs, drawn as contour ringsConcentric tilted ellipses like a topographic map, with a zig-zagging path of arrows stepping inward from the outer ring to the centre. start the bottom knob 1 knob 2 rings = equal wrongness (a topographic map of the loss)
With two knobs the hill becomes a landscape and the slope becomes a pair of numbers: how steep along knob 1, how steep along knob 2. That pair is the gradient. GPT-class models do this with hundreds of billions of knobs. The list gets longer. The move does not change.

A production language model has hundreds of billions of knobs, so the gradient is a list hundreds of billions of numbers long. The update rule does not change by a single character; it just runs on every entry at once.

Lumpier hills

Our bowl had exactly one bottom, which made downhill and correct the same direction. Real loss landscapes do not.

Figure 10 · real hills are lumpy
A bumpy, non-convex loss landscapeA wavy curve with several dips of different depths. A marker sits in a shallow dip on the left with an arrow showing it rolled in and stopped; a second marker sits in the much deeper dip further right. another dip stuck here flat underfoot, but not the bottom the real bottom the knob → how wrong we are
Our bowl had exactly one bottom, so downhill and correct were the same direction. Real networks have landscapes like this, full of dips that are flat underfoot but nowhere near the best answer. Downhill still works — it just stops being a promise. In practice the dips in a big model turn out to be about equally good, which is why the method survives.

More than one data point

We had one row. Real training sets have millions, so the loss is the average over examples, and the gradient is the average of their gradients. Averaging over all of them for every step is too slow — so you average over a random handful (a minibatch) and accept a noisy estimate of the tilt. That is the "stochastic" in stochastic gradient descent. Noisy tilt, cheap step, far more steps per hour.

Fancier steps

Momentum, Adam, learning-rate schedules, weight decay — every one of them is a refinement of the same two questions from the top of this page. Momentum keeps a bit of your last step so you coast through flat patches. Adam gives every knob its own learning rate based on how jumpy that knob has been. None of them replace the rule; they decorate it.

The cheat sheet

ww
The knob. A number the model is allowed to change.
y^=wx\hat{y} = w x
The prediction. The hat means guessed, not observed.
y−y^y - \hat{y}
The miss. How far the guess landed from the truth.
L=(y−y^)2L = (y - \hat{y})^2
The loss. The miss, squared, so sign stops mattering and big misses hurt most.
dLdw\frac{dL}{dw}
The slope. Nudge the knob a hair; this is how much the loss moves.
η\eta
The learning rate. How much of that slope you act on. A guess you tune.
w←w−ηdLdww \leftarrow w - \eta \frac{dL}{dw}
The whole algorithm. Repeat until the loss stops falling.
∇L\nabla L
The gradient. The same thing when there is more than one knob: one slope per knob, in a list.

Three sentences worth keeping

  1. The derivative tells you the direction; the learning rate is a guess. One is computed exactly, the other is chosen by a human who will change it later.
  2. The steps shrink by themselves. Flat ground means small slope means small step, which is why gradient descent settles instead of rattling around the bottom.
  3. Scale changes the bookkeeping, not the move. Whether it is one knob on paper or four hundred billion on a cluster: measure the tilt, take a small step against it, repeat.

That is the whole algorithm. You just ran it.

← Archive— end of essay —Front page →