Recommended resources
The freely available resources this lesson will direct you to.
- Gradient Descent, Step-by-Stepwww.youtube.com
- Stanford CS229: Machine Learning - Linear Regression and Gradient Descent | Lecture 2 (Autumn 2018)www.youtube.com
- An overview of gradient descent optimization algorithmswww.ruder.io
- 3.5. Gradient descent and its convergence analysismmids-textbook.github.io
In our last lesson, we defined and compared the core loss functions used in machine learning, establishing the "what" we aim to minimize. We now have a clear objective: to find the set of model parameters that results in the lowest possible value of a cost function . Today, we tackle the "how."
This lesson introduces gradient descent, the fundamental iterative optimization algorithm that powers much of modern machine learning. Our goal is to apply gradient descent to iteratively minimize a loss function, explaining the role of the learning rate and convergence criteria. We will start with the core intuition, derive the update rule, explore its crucial components, and finally examine the variants that are used in practice for large-scale problems. This is the workhorse algorithm for finding the optimal parameters for everything from simple linear regression to complex neural networks.
The Intuition: Descending a Mountain in the Fog
Imagine you are standing on the side of a mountain in a thick fog. Your goal is to reach the lowest point in the valley, but you can only see the ground right at your feet. What is your strategy? A sensible approach would be to feel the slope of the ground where you are, identify the direction of steepest descent, and take a step in that direction. You would then repeat this process—sense the slope, take a step—until you find yourself on flat ground, hopefully at the bottom of the valley.
This is precisely the intuition behind gradient descent. The "mountain" is the loss surface, a high-dimensional landscape where each point's location is defined by the model parameters and its elevation is given by the cost function . Our goal is to find the parameters that correspond to the lowest point on this surface.

In calculus, the tool that tells us the direction of steepest ascent at any point is the gradient, denoted as . The gradient is a vector containing the partial derivative of the cost function with respect to each model parameter:
Since the gradient points "uphill," to go "downhill" we simply move in the direction of the negative gradient.
This leads us to the core iterative update rule of gradient descent:
Let's break this down:
- : The vector of our model's parameters (e.g., the intercept and slope in linear regression).
- : The gradient of the cost function with respect to the parameters. It tells us the direction of steepest ascent.
- : The learning rate, a small positive number. This is a crucial new concept that determines the size of the step we take in the downhill direction.
The entire algorithm consists of starting with an initial guess for and repeatedly applying this update rule.
A Step-by-Step Walkthrough
The best way to understand gradient descent is to see it in action. The following video from StatQuest provides an exceptionally clear, step-by-step walkthrough, first optimizing a single parameter and then two parameters simultaneously for a linear regression model.
Gradient Descent, Step-by-Step
This video builds the gradient descent algorithm from the ground up, starting with a simple case and progressively adding complexity. It provides clear visualizations and detailed derivations.
Please watch the following segments: The Initial Setup: This part introduces the problem of finding the best intercept for a line and visualizes the loss function (sum of squared residuals) as a curve we want to minimize. Deriving the Gradient: Here, the video walks through the calculus of taking the derivative of the loss function with respect to the intercept. This is the formal calculation of the gradient for our simple, one-parameter case. The Iterative Process: This is the core of the algorithm. It shows how to use the derivative (gradient) and a new concept, the learning rate, to iteratively take steps toward the minimum. Notice how the steps get smaller as we approach the bottom of the curve. Convergence Criteria: This section explains how gradient descent knows when to stop, introducing the idea of a minimum step size threshold and a maximum number of iterations. Optimizing Multiple Parameters: Finally, the video extends the concept to optimize both the intercept and the slope, introducing the formal term gradient for the vector of multiple partial derivatives.
After watching, you should have a solid intuitive and practical grasp of the entire process. Let's now formalize some of the key pieces.
The Learning Rate: The Most Important Step
As the video highlighted, the learning rate is a critical hyperparameter. It controls the size of each step we take. Choosing the right learning rate is crucial for successful optimization.

- Learning Rate Too Low: The algorithm will take tiny steps, making convergence painfully slow. While it will eventually reach the minimum, it might take an impractical amount of time and computation.
- Learning Rate Too High: The algorithm might overshoot the minimum. Instead of settling in the valley, it jumps from one side to the other, causing the loss to oscillate or even increase.
- Learning Rate Much Too High: The steps can be so large that the algorithm diverges completely, with the loss value rapidly increasing towards infinity.
- A "Good" Learning Rate: The algorithm makes significant progress with each step, converging to the minimum in a reasonable number of iterations.
In practice, finding the best learning rate is often an empirical process of trying a few different values (e.g., 0.1, 0.01, 0.001) and seeing which one works best. We will discuss more advanced techniques for automatically adapting the learning rate in a later lesson.
Formalizing the Algorithm and its Convergence
Let's synthesize what we've learned into a formal description and delve into the theory of its convergence. The resource below provides a rigorous mathematical treatment.
3.5. Gradient descent and its convergence analysis
This textbook chapter formalizes the gradient descent algorithm and provides a theoretical analysis of its convergence properties. This will connect the intuitive ideas we've discussed with the mathematical guarantees that underpin them.
First, read the section "Gradient descent" (part 1). This section formally proves that the negative gradient is the direction of steepest descent and presents the general iterative update formula. Next, please read the section "Convergence analysis," focusing on the first part titled "Smooth case" (part 2). Pay close attention to the definition of an L-smooth function and the Convergence of Gradient Descent in the Smooth Case theorem. This theorem provides a quantitative guarantee: it tells us how many steps we need to take to ensure the gradient's magnitude is below a certain threshold \epsilon.
The key takeaways from this theoretical analysis are:
- Steepest Descent: The negative gradient, , is mathematically proven to be the direction that decreases the function's value most rapidly for an infinitesimally small step.
- Convergence Guarantee: For a sufficiently well-behaved (specifically, -smooth) function, gradient descent with a properly chosen step size () is guaranteed to converge to a point where the gradient is zero (a stationary point). The convergence rate is for the squared gradient norm, meaning to get the gradient norm below , you need roughly steps. This is a powerful result, as it provides a formal assurance that our "hill-climbing" analogy is mathematically sound.
Practical Variants: Batch, Mini-Batch, and Stochastic GD
The version of gradient descent we've discussed so far, where the gradient is computed using the entire training dataset for every single parameter update, is formally known as Batch Gradient Descent.
- Pros: It computes the true gradient of the cost function, leading to a stable and direct path towards the minimum (for convex problems).
- Cons: It is computationally massive. If you have a dataset with millions of examples, you must process all of them to take just one step. This makes it infeasible for large-scale machine learning.
To address this, two other variants are used in practice. Andrew Ng explains the trade-offs in this clip from his Stanford course.
Stanford CS229: Machine Learning - Linear Regression and Gradient Descent | Lecture 2 (Autumn 2018)
In this segment, Andrew Ng compares Batch Gradient Descent with its most common alternative, Stochastic Gradient Descent, explaining the practical motivations and performance differences.
Please watch the segment from this point. Focus on the distinction he draws between how each algorithm uses the data to compute updates and the resulting difference in the optimization path and computational cost.
Here is a summary of the three main variants:
-
Batch Gradient Descent:
- Update Rule:
- Gradient: Computed over the entire dataset.
- Behavior: Smooth, direct convergence but very slow updates on large datasets.
-
Stochastic Gradient Descent (SGD):
- Update Rule:
- Gradient: Computed for one training example at a time.
- Behavior: Very fast, but noisy updates. The loss function fluctuates heavily. It makes rapid progress initially but may oscillate around the minimum without ever fully converging.
-
Mini-Batch Gradient Descent:
- Update Rule:
- Gradient: Computed over a small batch of data (e.g., 32, 64, or 256 examples).
- Behavior: This is the de-facto standard in modern machine learning. It combines the best of both worlds: it's far more computationally efficient than batch GD and its updates are much less noisy than SGD, leading to stable convergence. It also allows for efficient, vectorized implementations that take advantage of modern hardware (GPUs).
You can get a quick overview of these three variants in the following reading.
An overview of gradient descent optimization algorithms
This article by Sebastian Ruder is a classic and highly-cited overview of optimization algorithms.
Please read the introductory section "Gradient descent variants" (the end of the section). This provides a concise summary and pseudo-code for each of the three approaches we've just discussed.
Conclusion
In this lesson, we have demystified gradient descent, the engine of optimization in machine learning. We started with the simple analogy of descending a mountain and formalized it into a powerful, iterative algorithm.
Here are the key takeaways:
- Gradient descent iteratively minimizes a loss function by taking steps in the direction of the negative gradient.
- The update rule is .
- The learning rate () is a critical hyperparameter that controls the step size. Its value determines the speed and stability of convergence.
- Convergence is typically determined by reaching a maximum number of iterations or by observing that the change in parameters or loss has become negligibly small.
- There are three main variants: Batch, Stochastic (SGD), and Mini-Batch. Mini-Batch Gradient Descent is the standard for training most large-scale models today due to its balance of computational efficiency and update stability.
We have now established not only what to optimize (the loss function) but also how to optimize it (gradient descent). However, our mountain analogy hides a crucial complexity. The simple, bowl-shaped valley we imagined is not always what the loss surface looks like. In our next lesson, we will explore the profound difference between convex and non-convex optimization problems, which determines whether gradient descent is guaranteed to find the single best solution or merely one of many possible "valleys."