Gradient Descent


Gradient Descent

Simon
Today we're learning Gradient Descent, an algorithm for finding parameter values that make a model fit data well. In linear regression, those parameters are the intercept and slope. But the same basic strategy can optimize many other things, including the curve in logistic regression and clusters in t-SNE.

Anna
So the line is our teaching example, but the bigger lesson is an optimization strategy that applies far beyond straight lines.

Simon
Exactly. Imagine a data set with weight on the x-axis and height on the y-axis. Once we fit a line, someone with a weight of 1.5 can be assigned a predicted height of about 1.9 using that line. Our goal is to find the intercept and slope that produce the best-fitting line.

Anna
And we're going to make the problem easier at first by estimating only the intercept with Gradient Descent, while plugging in the least-squares slope, 0.64.

Simon
Right. We begin with a random starting value for the intercept. In this example, the starting intercept is 0, but any number could be used. With the slope fixed at 0.64 and the intercept set to 0, we have a starting line.

Anna
How do we decide whether that starting line is good or bad?

Simon
We use the sum of the squared residuals. A residual is the observed height minus the predicted height, where the predicted height is the point on the line. In machine-learning language, the sum of squared residuals is one example of a loss function: a number that evaluates how well the model fits the data.

Anna
Let's calculate one. The first person has weight 0.5 and observed height 1.4. Plugging 0.5 into the starting line gives a predicted height of 0.32, so the residual is 1.4 minus 0.32, which is about 1.1.

Simon
For the other two people, the residuals are 0.4 and 1.3. After squaring the residuals and adding them, the sum of squared residuals is 3.1 for an intercept of 0.

Anna
If we calculate that loss for many possible intercepts, we can plot intercept values on the x-axis and sums of squared residuals on the y-axis. The plot forms a curve, and the best intercept is at the bottom, where the loss is smallest.

Simon
A slow approach would be to keep plugging in more and more intercept values until we happened to get very close to the minimum. Gradient Descent is more efficient: it takes relatively big steps when it is far from the minimum and progressively smaller, baby steps as it gets closer.

Anna
So how does it know whether to move left or right, and how large the step should be?

Simon
We first write the sum of squared residuals as an equation in the intercept. For the three observations, the predicted heights come from the line equation. The first uses weight 0.5, the second uses weight 2.3, and the third uses weight 2.9. Once those values are included, we can plug in any intercept and calculate the corresponding loss.

Anna
Then we take the derivative of that loss function with respect to the intercept. The derivative tells us the slope of the loss curve at the current intercept.

Simon
To differentiate each squared residual, we use the chain rule. The square moves to the front as a factor of 2, and we multiply by the derivative of the expression inside the parentheses. Terms that do not contain the intercept disappear because they act like constants. Applying this to all three residuals gives the derivative of the total loss.

Anna
And this derivative is useful because its value tells us how far we are from the bottom. Near the optimal intercept, the slope of the loss curve is close to zero. Far from the optimum, the slope tends to be farther from zero.

Simon
Starting with an intercept of 0, we plug 0 into the derivative and get a slope of negative 5.7. We convert that slope into a step size by multiplying it by a small number called the learning rate. Using a learning rate of 0.1 gives a step size of negative 0.57.

Anna
The update is important: the new parameter equals the old parameter minus the step size. So the new intercept is 0 minus negative 0.57, which equals 0.57. The negative step points us toward a larger intercept.

Simon
After that first large move, the residuals shrink. We plug the new intercept, 0.57, into the derivative and get a slope of negative 2.3. Multiplying by the learning rate gives a step size of negative 0.23, so the next intercept is about 0.8.

Anna
The steps keep getting smaller. At an intercept of 0.8, the derivative is about negative 0.9, giving a step of negative 0.09 and a new intercept of 0.89. Continuing the updates gives intercepts around 0.92, 0.94, and finally 0.95.

Simon
That shrinking pattern is the key behavior of Gradient Descent. The estimate reaches an intercept of about 0.95 after six steps, which matches the least-squares estimate. Least squares can find the solution by setting the derivative equal to zero, but Gradient Descent can approach the minimum through repeated steps even when solving that equation directly is not possible.

Anna
How does Gradient Descent know when to stop if it does not already know the correct answer?

Simon
It stops when the step size is very close to zero, because that means the derivative is close to zero and the algorithm is near the minimum. A practical threshold might be a step size of 0.001 or smaller. For example, a slope of 0.009 multiplied by a learning rate of 0.1 gives a step size of 0.0009, so the algorithm would stop.

Anna
There is also a safety limit on the number of steps. In practice, the maximum might be 1,000 steps or more, so the algorithm stops even if the step size has not become small enough.

Simon
So the one-parameter procedure is: choose a loss function, take its derivative, choose a random initial intercept, evaluate the derivative there, calculate a learning-rate-adjusted step, update the intercept, and repeat until the step is tiny or the maximum number of steps is reached.

Anna
Now let's remove the shortcut and estimate both parameters: the intercept and the slope.

Simon
The loss is still the sum of squared residuals, but now it depends on two parameters. If we plotted it, we would have a three-dimensional surface: the intercept on one axis, the slope on another, and the loss on the vertical axis. We want to descend to the lowest point on that surface.

Anna
That means taking two derivatives, one with respect to the intercept and one with respect to the slope.

Simon
Correct. When differentiating with respect to the intercept, we treat the slope as a constant, so its derivative is zero. That produces the same type of intercept derivative as before. When differentiating with respect to the slope, we treat the intercept as a constant, and the weight values remain in the derivative: 0.5 for the first person, 2.3 for the second, and 2.9 for the third.

Anna
So the two derivatives together are called the gradient. The gradient tells us how the loss changes in relation to both parameters, and Gradient Descent uses it to move toward the lowest point.

Simon
We start again with random parameter values. This time, the intercept is 0 and the slope is 1. We plug those values into both derivatives, multiply each resulting slope by the learning rate, and get two step sizes.

Anna
Then we update both parameters at once: new intercept equals old intercept minus its step size, and new slope equals old slope minus its step size. That produces a new line, and we repeat the process.

Simon
For this two-parameter example, a learning rate of 0.01 is used. The larger learning rate of 0.1 that worked in the intercept-only example does not work well here; even after many steps, Gradient Descent does not reach the correct answer. This shows that Gradient Descent can be sensitive to the learning rate.

Anna
A learning rate that is too large can make the steps overshoot or fail to reach the right answer, while a smaller rate gives more controlled updates. In practice, a reasonable rate can be determined automatically by starting relatively large and making it smaller with each step.

Simon
Repeating the two-parameter updates until the step sizes are tiny, or until the maximum number of steps is reached, produces the best-fitting line. Its intercept is about 0.95 and its slope is about 0.64, matching the least-squares values.

Anna
And if a model has more than two parameters, the idea does not change. We take one derivative for every parameter, form the gradient, calculate a step for each parameter, update them, and repeat.

Simon
The loss function also does not have to be the sum of squared residuals. That is just one kind of loss function. Other types of data can call for other loss functions, but Gradient Descent follows the same general process.

Anna
Let's state that process clearly. Step one: take the derivative of the loss function with respect to every parameter—in machine-learning language, calculate the gradient. Step two: choose starting values for the parameters.

Simon
Step three: plug the current parameter values into the gradient. Step four: calculate the step sizes using the learning rate. Step five: calculate the new parameters by subtracting the step sizes from the old parameters. Then return to step three and continue until the steps are very small or the maximum number of steps is reached.

Anna
There is one practical problem: with millions of data points, calculating the full loss derivative at every step can take a long time.

Simon
That is where Stochastic Gradient Descent comes in. Instead of using the entire data set at every step, it uses a randomly selected subset of the data. The derivatives then take less time to calculate, which can make the optimization faster for very large data sets.

Anna
So the central picture is simple: measure the loss, use the gradient to determine the direction and relative size of the move, take a learning-rate-controlled step, and repeat until the movement is negligible or the step limit is reached.

Simon
Exactly. That strategy lets Gradient Descent find good parameter values for a line and extends naturally to curves, clusters, and many other optimization problems in statistics, machine learning, and data science.

Ask anything
We've read this whole study set, so questions about it get the best answers.