Gradient descent in 3D

A translucent bowl with a blue cross-section curve and amber steps descending toward the minimum, each halving the distance.Drag to turn

Gradient descent is an iterative method that adjusts a model's parameters by stepping in the direction that most quickly reduces a loss function. The loss surface is the graph of that loss plotted against the parameters.

What a loss surface shows

A loss function is a number that measures how badly a model fits its data. A model with two parameters has a loss value for every pair of parameter values. Plotting that value as height above the parameter plane gives a loss surface.

Low points on the surface are good parameter choices. The goal of training is to find a low point, ideally the lowest one. With millions of parameters the surface cannot be drawn, but the same idea applies.

How the update rule works

The gradient is the vector of partial derivatives of the loss with respect to each parameter. It points in the direction of steepest increase. Gradient descent therefore moves the opposite way.

The update rule is: new parameter = old parameter minus learning rate times gradient. The learning rate is a small positive number that sets the step size. The process repeats until the loss stops falling or a step limit is reached.

Worked example with one parameter

Take the loss L(w) = (w - 3)^2, whose minimum is at w = 3. The gradient is 2(w - 3). Start at w = 0 with a learning rate of 0.25.

Step 1: gradient = 2(0 - 3) = -6, so w = 0 - 0.25 x (-6) = 1.5. Step 2: gradient = 2(1.5 - 3) = -3, so w = 1.5 - 0.25 x (-3) = 2.25. Step 3: gradient = 2(2.25 - 3) = -1.5, so w = 2.25 + 0.375 = 2.625.

Each step halves the distance to 3. The loss falls from 9 to 2.25 to 0.5625 to 0.140625. With a learning rate of 1.1 the same problem would overshoot and diverge, since each step multiplies the distance by -1.2.

Common misconceptions

Gradient descent does not guarantee the global minimum. On a surface with several valleys, it can settle in a local minimum or stall on a flat region. For convex losses, such as the squared error of linear regression, any local minimum is also global.

A smaller learning rate is not simply better. Very small steps make training slow, while large ones can bounce across a valley or diverge. Also, the gradient points uphill locally, not toward the minimum from far away.

Where it shows up

Gradient descent trains linear and logistic regression, and it is the basis of training neural networks, where backpropagation computes the gradient. In practice, stochastic gradient descent estimates the gradient from a small batch of data rather than the full set. Variants such as momentum and Adam adjust the step using past gradients.

Key terms

Loss function
A number that measures how far a model's predictions are from the target values.
Loss surface
The graph of the loss plotted against the model's parameters.
Gradient
The vector of partial derivatives of the loss, pointing toward steepest increase.
Learning rate
The positive factor that scales each parameter update.
Local minimum
A point where the loss is lower than at nearby points but not necessarily the lowest overall.
Stochastic gradient descent
Gradient descent that estimates the gradient from a random subset of the data at each step.

Common questions

Why does gradient descent step against the gradient?

The gradient points toward the fastest local increase in loss. Moving in the opposite direction gives the fastest local decrease, provided the step is small enough.

How do you know when to stop?

Common stopping rules are a gradient close to zero, a loss that improves by less than a set tolerance, or a fixed number of steps. Practitioners often also watch loss on held-out data.

Can gradient descent get stuck?

Yes. It can stop near a local minimum, a saddle point or a flat region where the gradient is close to zero. Stochastic noise and momentum often help it move on.

Learn this with a tutor

Sidechair's AI tutor will teach this topic in 3D. Join the waitlist for early access.

We'll only email you about Sidechair. Read our privacy policy.