Gradient Descent
Gradient descent finds the bottom of the loss by taking small steps downhill: measure the slope at the current weights, then step a little the opposite way. The gradient points in the steepest uphill direction, so its negative is the steepest way down.
What the symbols mean
- Weights
All the model's weights and biases, stacked into one vector.
- Learning rate
A small positive number: how far to step per unit of gradient.
- Gradient of the loss
The vector of partial derivatives ∂L/∂wⱼ. It points uphill, steepest first.
What the symbols mean
- Direction
A unit vector: the direction you consider stepping in.
- Angle
The angle between u and the gradient. The rate is most negative at θ = π.
Explanation
Gradient descent is simply walking downhill on the loss landscape, one step at a time, by feel. The name says what it does: it follows the gradient (the vector of slopes in every weight direction) and descends. The update has three pieces: (where you're standing, all the weights at once), (which way is uphill and how steep it is) and (the learning rate, a small number saying how big a step to take). Subtract, and you've moved a little downhill. That one line, repeated millions of times, is how essentially every neural network is trained.
Picture standing on a foggy hillside, trying to reach the valley floor. You can't see the valley, but you can feel the ground under your feet. The sensible plan is to feel which way the ground slopes down most steeply, take a step that way, and repeat. That's the whole algorithm. The network can't see its loss landscape either; with millions of weights there's nothing to look at. But it can compute the slope where it stands.
One weight first. With a single weight the gradient is just the derivative . If , the loss rises to the right, so we should move left, and does exactly that. If , it moves right. Notice also that the step size is proportional to the slope: on a steep wall the steps are big, and near the flat bottom they shrink on their own. At the bottom and the update stops changing anything. So the places gradient descent can come to rest are exactly the critical points from calculus. There's an animation further down of a point rolling down a loss curve this way.
Many weights: why the negative gradient. In Directional Derivatives you saw that the rate of change of in a unit direction is
Think about what that says. The length is fixed by where you stand, so the only thing you control is the angle , and runs from to . The loss rises fastest at , when you head along , and falls fastest at , when you head exactly opposite it, at the rate . That's why we subtract the gradient. It also explains the shape of the paths: by Gradients and Normal Lines, is perpendicular to the level curves of , so each gradient step cuts straight across the contour lines.
Why a small step really lowers the loss. Use the linear approximation in several variables: for a small change . Put in the gradient step :
A squared length is never negative, and it's zero only at a critical point. So as long as the step is small enough for the approximation to hold, every step lowers the loss. How small is "small enough" is the subject of the next lesson.
The gradient for a real model. For the line with mean squared error, the chain rule gives
Read it as a vote. Every example pulls on the weights in proportion to its error, and an example that's already predicted perfectly doesn't pull at all. In a deep network the partial derivatives come out of backpropagation, the next unit, but the update is this same line.
What it can't do. Gradient descent only ever feels the slope right where it stands. It finds a point where , and that could be a local minimum or even a saddle point (see Relative Extrema). For a linear model with MSE the loss is a bowl with one bottom, so this never bites. For deep networks it's a real question, and the next lessons on noise and momentum are partly about getting through flat and awkward regions.
A common mistake. It's tempting to think the gradient points toward the minimum. It doesn't; it points in the locally steepest direction, perpendicular to the contour lines. In a stretched-out bowl, that's mostly across the valley rather than along it toward the bottom, which is why gradient descent paths bend and zig-zag. You can see the bend in the second animation below, and the zig-zag gets its own treatment in The Learning Rate and Convergence.
See it
Worked examples
Run three steps of gradient descent on from with learning rate .
- 1
Find the derivative first: . The is a constant, so it disappears.
For at the point , find the steepest descent direction and the rate the loss falls along it. Then take one gradient step with .
- 1
Take the partial derivatives: and .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Take one gradient descent step on from with . What is ?
Gradient descent on with has reached . Take one more step: what is ?
For , take one gradient step from with . Enter the new and .
At some weights the gradient of the loss is . Enter the unit vector of steepest descent.
At some weights the gradient of the loss is . At what rate does the loss change per unit of distance when you move in the steepest descent direction?
A model predicts a taxi fare as dollars for a trip of km. One training trip was km and cost $6, and the loss is the squared error . Starting from with , what is after one gradient step?
A one-weight model outputs , the sigmoid, and its loss is . Starting from with , what is after one gradient step?
Gradient descent with a small learning rate stops moving once it reaches which kind of point?
The hardest one here. Get it, and you've got this lesson.
Fit to the two points and using the mean squared error. Start at , and take one gradient step with . Enter the new , the new and the new MSE.
Where it's used
Every deep-learning library has an optimizer whose core is literally this line: for each weight, subtract the learning rate times its partial derivative. Recommendation systems, speech recognizers and image classifiers all train this way, and so do far older tools: a logistic regression for credit risk or the calibration of a sensor is fit by the same downhill walk. The gradient descent lab lets you place the start and watch the path.