Introduction
In our last lesson, we established a crucial piece of the puzzle: the gradient vector, , always points in the direction of the steepest ascent. This is like finding the direction straight up a mountain from any point on its slope.
However, in machine learning, we're rarely trying to climb to a peak. Instead, we want to find the bottom of a valley—the point where an error or "loss" function is at its absolute minimum. If the gradient points uphill, it's logical that the direction of steepest descent must be the exact opposite: .
This simple but powerful idea is the foundation of gradient descent, the most important optimization algorithm in modern AI. Today's lesson is designed to help you master the mechanics of this algorithm. Our goal is to apply gradient descent to find the minimum of a simple function on paper. You will learn the step-by-step update rule and perform the calculations manually, building a solid, theoretical understanding before we automate this process in Python in our next lesson.
1. The Core Concept and Algorithm
Imagine you're standing on a foggy hill and want to get to the lowest point. You can't see the whole valley, but you can feel the slope right under your feet. The most straightforward strategy is to:
- Check the direction of the steepest downhill slope.
- Take a small step in that direction.
- Repeat the process from your new position.
Gradient descent is the mathematical formalization of this exact process. It's an iterative algorithm that takes successive steps "downhill" on a function's surface to find a minimum.

The algorithm itself is defined by a simple update rule. To find the next position, , we start from our current position, , and subtract the scaled gradient.
To understand the formal steps and the components of this rule, let's turn to a clear textual explanation.
Gradient Descent Algorithm — a deep dive
The article "Gradient Descent Algorithm — a deep dive" clearly outlines the core mechanics. It introduces the update formula and explains the critical role of the learning rate.
Please read section 4, 'Gradient Descent Algorithm'. Focus on understanding the update formula and the definition of the 'learning rate'.
As the article explains, the update rule for a single-variable function is:
Let's break down the components:
- is our current position at step .
- is the next position we are calculating.
- is the derivative (gradient) of the function evaluated at our current position. This tells us the direction of the steepest slope.
- (the Greek letter 'eta') is the learning rate. It's a small positive number (e.g., 0.1, 0.01) that controls how big of a step we take. This is a crucial parameter you'll encounter constantly in machine learning.
The overall process is:
- Choose a random starting point, .
- Choose a learning rate, .
- Repeat for a set number of iterations:
- Calculate the derivative at the current point: .
- Update the position using the rule: .
2. A Worked Example: Step-by-Step Calculation
Now that you understand the theory, let's apply it. We'll walk through the first few iterations of gradient descent for a simple quadratic function. This hands-on process is exactly what this lesson is about.
The following video provides a clear, step-by-step calculation for finding the minimum of a function.
How Gradient Descent Works. Simple Explanation
The video 'How Gradient Descent Works. Simple Explanation' by Data Science Garage is an excellent, concise demonstration of the algorithm in action.
Watch the video from 00:28 to 04:27. The presenter will define a function and then manually calculate the first two iterations of gradient descent. Follow along with their calculations.
Let's formalize the steps from the video on paper to ensure the process is clear.
Problem Setup:
- Function: . By looking at the function, we know the minimum occurs when the squared term is zero, i.e., at . Let's see if gradient descent can find it.
- Derivative: Using the chain rule, .
- Initial Point: .
- Learning Rate: .
Iteration 1:
-
Calculate the gradient at :
A positive gradient means the slope is upwards, so we need to move to the left (decrease x) to go downhill.
-
Apply the update rule:
After one step, we've moved from to , which is closer to the true minimum of -5.
Iteration 2:
-
Calculate the gradient at our new point, :
The gradient is still positive but slightly smaller, meaning the slope is less steep.
-
Apply the update rule:
After two steps, we are at , even closer to the minimum. We would continue this process until the change in becomes very small, indicating we've arrived at the bottom of the valley.
3. Practice and the Importance of the Learning Rate
Let's try another example to solidify your understanding.
Gradient Descent Algorithm — a deep dive
The article 'Gradient Descent Algorithm — a deep dive' also contains a great hands-on example with a simple quadratic function.
Read section 5, 'Example 1 — a quadratic function'. It works through the first three steps for the function f(x) = x². Pay attention to how the step size changes as the algorithm gets closer to the minimum.
As you've seen, the choice of the learning rate, , is critical.
- If is too small: The algorithm will take tiny steps and converge very slowly, potentially running out of time (or iterations) before reaching the minimum.
- If is too large: The algorithm might "overshoot" the minimum, jumping from one side of the valley to the other. In worse cases, it can cause the steps to get larger and larger, making the algorithm diverge completely.
To visualize this effect, let's explore a resource that demonstrates what happens with different learning rates.
Complete Step-by-Step Gradient Descent Algorithm from Scratch
The article 'Complete Step-by-Step Gradient Descent Algorithm from Scratch' has excellent visualizations showing how different learning rates affect the convergence path.
Read the section 'Gradient Descent (the Easy Way)'. You don't need to worry about the Python code. Focus on the four scenarios presented and the corresponding plots that show what happens with a good, large, small, and diverging learning rate.
Finding the right learning rate is a key part of training machine learning models and is often done through experimentation.
Conclusion
You have now successfully performed the single most fundamental optimization procedure in AI. By working through the calculations on paper, you've built a robust mental model of how a system can "learn" to find the best configuration by minimizing an error function.
Key Takeaways:
- Gradient Descent is an iterative optimization algorithm used to find the minimum of a function.
- The core principle is to take small, repeated steps in the direction opposite to the gradient ().
- The update rule for a single-variable function is .
- The learning rate () is a critical hyperparameter that controls step size. A value that is too large can cause divergence, while one that is too small can lead to slow convergence.
Preview of the Next Lesson:
Manually calculating these steps is excellent for understanding the process, but it's not practical for the thousands or millions of iterations needed to train a real model. In the next lesson, "Implement gradient descent in Python for a single-variable function," we will take the exact algorithm you learned today and write a program to do the work for us. This will be your first step in translating foundational AI theory into practical, working code.