Skip to content
Mostly Figured Out
Go back

A Simple Mental Model for Gradient Descent

Gradient descent becomes easier to reason about with a simple linear regression example.

Suppose we have several points on an xx-yy graph and want to find the straight line that best fits them.

For a candidate line, we can measure how well it fits using the sum of squared residuals:

L=∑i(yi−y^i)2L = \sum_i (y_i - \hat{y}_i)^2

The smaller the loss, the better the fit.

From a line to a loss curve

To keep the example simple, assume the slope of the line is fixed and we only want to find the best y-intercept, bb.

We can try different values of bb, calculate the loss for each one, and plot:

For linear regression, this gives us a bowl-shaped loss curve. Our goal is to find the point at the bottom of that curve.

The derivative tells us which way to move

At any point on the loss curve, the derivative tells us its slope.

dLdb=0\frac{dL}{db} = 0

Gradient descent updates the intercept in the opposite direction of the gradient:

bnew=bold−ηdLdbb_{\text{new}} = b_{\text{old}} - \eta \frac{dL}{db}

Here, η\eta is the learning rate, which controls how large each step is.

When the loss curve is steep, the gradient is larger, so the update tends to be larger. As we get closer to the minimum, the gradient becomes smaller, so the steps become smaller too.

The mental model

Gradient descent can be thought of as:

Look at the slope where you currently are, take a controlled step downhill, and repeat.

The loss function defines the landscape, the gradient tells us which direction is uphill, and gradient descent moves in the opposite direction toward lower loss.


Share this post:

Previous Post
Learning Machine Learning After Years of Backend Engineering