Gradient Descent
Work out which way the loss falls, step a little that way, repeat. That single loop trains almost everything, and the step size decides whether it works.
Why Does This Exist?
Every model needs a way to turn a wrong answer into a slightly better set of weights. The loss scores how wrong the current weights are, and there is no formula that jumps straight to the best weights once the model has more than a handful of parameters. What exists instead is a slope at the current position, and a loop that follows it down.
Two terms carry the page. The loss maps weights to one number for how bad the fit is. The gradient is the vector of partial slopes, one per weight, pointing uphill toward higher loss. This page is the loop that consumes that vector. The vector itself is taught in backpropagation, which shows where each slope comes from. The per-row version of this loop is stochastic gradient descent, and the visual walkthrough is gradient descent.
Think of It Like This
Walking down a foggy hill with a cane
You stand on a foggy hillside and can only feel the ground right under your feet. You tap with the cane, feel which way tilts down hardest, and take one step that way. Then you tap again, because the slope at the new spot points somewhere slightly different.
The cane reading is the gradient: direction plus steepness. Your stride length is the learning rate. A toddler stride gets you down eventually but the fog lifts first. A leaping stride overshoots the gully and lands higher on the far side, again and again.
Where it stops: a hill has one connected slope and your legs tire the same way each step. A loss surface has saddles, plateaus, and millions of directions, and the cost of one step depends on batch size and hardware, none of which the hill knows about.
How It Actually Works
One update, in full:
Here is the weight vector at step , is the gradient of the loss at that point, and is the learning rate, one positive number. The minus sign is the whole idea: the gradient points uphill, so you step the other way. Then you recompute and repeat.
Worked example: w squared from 2.0 with rate 0.25
Take the one-weight loss , so the slope is . Start at with . Each step multiplies by :
Losses are the squares:
Four steps cut the loss from 4.00 to 0.02 because rounds to 0.06 at step three and rounds to 0.02 at step four. Halving each time is the signature of a well-chosen rate on a smooth bowl. The diagram draws exactly these five dots on one curve, with a crawling path and a diverging path from the same start for contrast.
Why the rate decides everything
Too small a rate crawls: with the factor is , so moves to , then , and the loss barely dents from 4.00 to 3.69 to 3.40. Too large a rate diverges: with the factor is , so jumps to with loss 4.84, then with loss 5.86, landing higher every step. Rising from the very first step is the too-large signature. Falling steeply then flattening far above zero is the too-small one. Learning rate scheduling builds on this reading by changing as the run proceeds, and loss functions decide what surface you descend in the first place.
Code
The point is the check: rebuild the halving run and confirm the five printed losses match the hand computation.
w: float = 2.0eta: float = 0.25losses: list[float] = [w * w]for _ in range(4): w = w - eta * 2.0 * w losses.append(w * w)
print([f"{v:.2f}" for v in losses]) # -> ['4.00', '1.00', '0.25', '0.06', '0.02']The printout matches the worked steps: start 4.00, then 1.00, 0.25, 0.06, 0.02. Change eta to 0.02 and the list barely moves. Change it to 1.05 and the values grow. That sensitivity is why the rate gets its own scheduling page.
Watch Out For
Loss climbs from step one, blamed on the rate alone
Symptom: the loss rises smoothly from the first update and keeps rising, and the reflex is to divide the rate by ten. Sometimes that is right, for the 1.05 case above.
Fix: check the sign first in any hand-rolled update. A plus where the minus belongs, or a gradient with flipped orientation, also climbs from step one, and lowering the rate then only slows the climb. Take one tiny step and confirm the loss moves down at all before tuning anything else.
Tuning the rate while features sit on wildly different scales
Symptom: no rate works. Small values barely move the loss, a slightly larger one diverges, and nothing sits between them.
Fix: scale the inputs first so one global can serve every direction. When one feature spans tens and another spans thousands, the surface is a narrow valley where any single step is too big across and too small along. Standardize, then retry the first rate.
The Quick Version
- Gradient descent steps opposite the gradient by the learning rate and repeats until the loss stops falling.
- On w squared from 2.0 with rate 0.25, weights halve 2.0 to 1.0 to 0.5 to 0.25 to 0.125.
- Those five weights give losses 4.00, 1.00, 0.25, 0.06 and 0.02, so four steps reach 0.02.
- Rate 0.02 crawls with factor 0.96 while rate 1.05 diverges with factor minus 1.10.
- Rising from step one means the rate is too high or the sign is wrong, while early flattening means it is too low.
- Adam adapts a per-weight rate on top of this same loop once the fixed rate works.