Introduction
In our last lesson, we established that the Mean Squared Error (MSE) loss function for linear regression forms a perfect, convex "bowl" or paraboloid. This shape is a powerful guarantee: any local minimum we find is also the global minimum. We now have a map of the terrain (the loss surface) and we know the location of our destination (the single lowest point). Today, we'll learn about the vehicle and the navigation system used to get there: the Gradient Descent algorithm.
This lesson will trace the exact mechanics of this fundamental optimization algorithm. We will dissect how it uses the gradient of the loss function as a compass to guide its journey and how the learning rate determines the size of each step. By the end, you will be able to visualize and explain the iterative process of descending a 2D loss surface to find the optimal parameters for a model.
The Core Idea: Following the Steepest Path Downhill
Imagine you are standing on the side of a foggy mountain, and your goal is to reach the lowest point in the valley. You can't see the bottom, but you can feel the slope of the ground beneath your feet. The most straightforward strategy would be to identify the direction of steepest descent at your current location and take a step in that direction. After taking the step, you re-evaluate the slope and repeat the process. This is the essence of gradient descent.
In the context of machine learning, the "mountain" is the loss surface, your "location" is defined by the current values of your model's parameters (e.g., weight w and bias b), and the "altitude" is the loss. The "slope" is given by a mathematical object we've already encountered: the gradient.
As we saw previously, the gradient of a function, denoted , is a vector of its partial derivatives that points in the direction of the steepest ascent. To go downhill, we simply move in the opposite direction: the negative gradient.

This fundamental idea is one of the most important concepts in modern machine learning. The following video from 3Blue1Brown, one of your preferred resources, provides a superb visual intuition for how this works.
Gradient descent, how neural networks learn Chapter 2, Deep learning
This video explains the concept of gradient descent by starting with a simple 1D function and building up to the high-dimensional parameter space of a neural network.
Watch the segment explaining how to find a minimum for a simple 1D function by stepping opposite to the slope (1D case). Notice the analogy of a ball rolling down a hill. Next, see how this idea extends to a 2D function, where the gradient vector points in the direction of steepest ascent, and the negative gradient points downhill (2D case). Finally, understand how this same logic applies to the vast parameter space of a machine learning model (ML context). The negative gradient vector tells us how to "nudge" all parameters to decrease the cost.
The Gradient Descent Update Rule
The video provides the core intuition. Now, let's formalize the algorithm's single most important equation: the update rule. At each step of the algorithm, we update every parameter in our model according to this rule. For a single parameter, like a weight w, the update looks like this:
Let's break this down:
- is the parameter's current value.
- is the partial derivative of the loss
Lwith respect tow. This is one component of the gradient vector and tells us the slope of the loss surface along thewaxis. - (the Greek letter eta) is the learning rate. This is a small positive number (e.g., 0.01) that we choose. It's a "hyperparameter" that controls the size of our step.
- The minus sign is crucial: we are moving in the opposite direction of the gradient component.
We perform this update simultaneously for all parameters. For a linear model with weight w and bias b, the gradient is , and the update rule in vector form is:
This simple, iterative process is the engine that drives a vast number of machine learning models.
Tracing the Algorithm Step-by-Step
To make this concrete, let's trace the algorithm's execution. The following video from StatQuest provides an exceptionally clear, step-by-step numerical example.
Gradient Descent, Step-by-Step
This video walks through the entire gradient descent process, first for a single parameter and then for two, showing the exact calculations at each step.
First, watch the detailed walkthrough for optimizing a single parameter (the intercept) (single-parameter update). Pay close attention to how the step size is calculated by multiplying the derivative (slope) by the learning rate, and how the steps get smaller as we approach the minimum. Next, see how the algorithm stops, either when the step size becomes very small or a maximum number of iterations is reached (stopping criteria). Finally, watch how the process extends to two parameters (intercept and slope) (two-parameter update). This introduces the 3D loss surface and the gradient (a vector of two partial derivatives) that guides the descent.
The video demonstrates two key behaviors:
- Direction: If the slope for a parameter is positive, the update subtracts a positive value, decreasing the parameter. If the slope is negative, the update subtracts a negative value, increasing the parameter. In both cases, the parameter is nudged towards the minimum.
- Magnitude: As the algorithm approaches the minimum, the gradient (slope) gets closer to zero. This naturally causes the step sizes to shrink, allowing the algorithm to settle gracefully into the bottom of the valley instead of overshooting it.
For a textual walkthrough that you can study at your own pace, the following article provides an excellent supplement.
Intuition and Mathematics behind Gradient Descent Algorithm
This article breaks down the gradient descent algorithm with clear explanations, calculations, and visualizations, mirroring the structure of the StatQuest video.
First, read the section on a function with one parameter. This reinforces the logic of the single-parameter update rule you saw in the video, showing how the sign of the derivative determines the direction of the update. Next, study the section on a function with two parameters. Focus on how partial derivatives are used to update each parameter independently and how this corresponds to moving downhill on the 3D loss surface. The use of contour plots here is especially helpful for visualizing the 2D path.
Visualizing the Full Path
After many iterations, the sequence of small steps traces a path down the loss surface, leading directly to the global minimum. The following image perfectly captures this process.

This path illustrates the algorithm's effectiveness. Starting from a random point in the parameter space, it reliably navigates the landscape to find the optimal set of parameters that minimize the model's error.
The entire process can be summarized as a simple loop:
- Initialize: Pick random starting values for your parameters and .
- Loop (for a set number of iterations or until convergence):
a. Compute Gradient: Calculate the gradient of the loss function, , at the current parameter values.
b. Update Parameters: Take a small step downhill by applying the update rule:params = params - learning_rate * gradient. - Terminate: The final parameter values are the optimal ones for the model.
This simple, powerful loop is the core of how many machine learning models "learn" from data.
Conclusion
In this lesson, you have dissected the mechanics of the gradient descent algorithm. You've gone from the high-level intuition of "rolling down a hill" to the precise, step-by-step update rule that governs the process.
Key Takeaways:
- Gradient descent is an iterative algorithm that minimizes a loss function.
- The gradient () of the loss function is a vector that points in the direction of the steepest increase in loss.
- The algorithm takes steps in the direction of the negative gradient to move "downhill" towards a minimum.
- The learning rate () is a hyperparameter that controls the size of each step.
- The core of the algorithm is the update rule:
parameters = parameters - learning_rate * gradient, which is applied repeatedly until the algorithm converges.
We have seen how gradient descent works. In our next lesson, we will explore the mathematical justification for why it works, using Taylor approximations to prove that taking a small step opposite the gradient is indeed the best local move you can make. We will also analyze the critical role of the learning rate in more detail, diagnosing what happens when it's chosen poorly.