Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 21

Understanding Gradient Descent

Recommended resources

Hello! Welcome to the fifth lesson in our module on Calculus for Optimization.

In our last lesson, we learned how to find the minimum of a function analytically. We found the points where the derivative is zero and used the second derivative test to classify them. This is a powerful method, but it relies on our ability to solve the equation . What happens when the function is so complex that we can't solve this equation? This is almost always the case in machine learning, where our functions can have millions of parameters.

Today, we're going to learn about an iterative approach to find the minimum: gradient descent. This algorithm is one of the most important concepts in modern data science and AI, forming the backbone of how most machine learning models are trained. Understanding it thoroughly is crucial for your GATE preparation and beyond.

Our learning outcome for this lesson is to explain the concept of gradient descent for function optimization.

1. The Core Idea: Walking Downhill

Imagine you are standing on a hilly landscape in thick fog and you want to get to the lowest point. You can't see the whole landscape, but you can feel the steepness of the ground right where you are. What's the best strategy?

You would probably:

  1. Feel the ground to find the direction of the steepest slope downhill.
  2. Take a small step in that direction.
  3. Repeat the process.

This is the exact intuition behind gradient descent.

Gradient Descent Visualization and Definition
This image illustrates the core idea of gradient descent. Starting from an initial point, the algorithm takes iterative steps in the direction of the negative gradient to find the minimum of a cost function.

In our previous lessons, we established that the gradient () of a function points in the direction of the steepest ascent (uphill). Therefore, to go downhill as fast as possible, we need to move in the direction opposite to the gradient.


2. The Components of Gradient Descent

Gradient descent is an iterative optimization algorithm. To make it work, we need three key components:

  1. A Loss Function, : This is the function we want to minimize. In machine learning, this function measures how "wrong" our model's predictions are. Our goal is to find the model parameters () that make this error as small as possible. A common example is the Mean Squared Error (MSE).
  2. The Gradient of the Loss Function, : This tells us the direction of the steepest ascent at our current position. For a function with multiple parameters , the gradient is a vector of partial derivatives: .
  3. The Update Rule: This is the formula that tells us how to take a step from our current position to a new, lower position.

The update rule is the heart of the algorithm:

Let's break this down:

  • is our current set of parameters.
  • is the gradient of the loss function at our current position.
  • The minus sign is crucial: we move in the direction opposite to the gradient to go downhill.
  • (the Greek letter 'eta') is the learning rate. This is a small positive number (e.g., 0.01) that controls how big our steps are.

3. Gradient Descent in Action: A Step-by-Step Walkthrough

The best way to understand gradient descent is to see it in action. The following video provides a fantastic, step-by-step explanation, starting with a simple case and building up.

Gradient Descent, Step-by-Step

Let's watch this excellent video 'Gradient Descent, Step-by-Step' from the StatQuest channel. It first demonstrates the process for a single parameter (the intercept of a line) before expanding to two parameters.

Watch from 02:09 to 14:39. This is the core of the lesson. Pay close attention to these key stages: The Loss Function (02:09 - 05:38): Understand how the Sum of Squared Residuals is used as a loss function and how it creates a curve we want to find the bottom of. Calculating the Derivative (07:18 - 09:00): See how the derivative (the gradient in 1D) is calculated. This connects back to our previous calculus lessons. The Iterative Process (09:00 - 14:39): This is the main part. Observe how the algorithm uses the derivative and a learning rate to calculate a 'step size' and update the parameter, getting closer to the minimum with each step.

Gradient Descent Path on Loss Surface
This 3D plot visualizes gradient descent for two parameters (like slope and intercept). The surface is the loss function, and the red path shows the algorithm 'walking' down the surface to find the optimal solution at the bottom.

4. The Crucial Role of the Learning Rate ()

As you saw in the video, we have to choose a value for the learning rate, . This choice is critical.

  • If is too small: The algorithm will take tiny steps and converge to the minimum very slowly.
  • If is too large: The algorithm might overshoot the minimum and bounce around, or even diverge, with the loss getting worse and worse.

This next video gives a fantastic visual demonstration of how different learning rates affect the algorithm's behavior.

Gradient Descent From Scratch in Python - Visual Explanation

The video 'Gradient Descent From Scratch in Python' from NeuralNine provides a great animated visualization of the effect of the learning rate.

Watch from 13:19 to 15:09. Observe what happens when the learning rate is very small, just right, and too large (causing oscillation and divergence).

Test your understanding!

Suppose you are using gradient descent to minimize a function . You start at . You calculate the derivative at this point and find that . Your learning rate is .

What will be your new position for after one step of gradient descent? Will you move to the left or to the right on the number line? Why does this make sense?

Show answer

The update rule is .

Plugging in the values:

You will move to the right (from 5 to 5.2).

This makes sense because the derivative is negative (), which means the function is sloping downwards as you move to the right. Therefore, to get closer to the minimum, you should continue moving to the right.


5. Gradient Descent with Multiple Parameters

In most real-world scenarios, like fitting a line in linear regression (), we need to optimize multiple parameters at once. The principle remains exactly the same. We just calculate the partial derivative for each parameter and update all of them simultaneously.

The set of all partial derivatives is the gradient, which is why the algorithm is called gradient descent.

Let's return to the StatQuest video to see how this works.

Gradient Descent, Step-by-Step

This section of the StatQuest video extends the concept to simultaneously optimize both the slope and the intercept of a line.

Watch from 15:49 to 21:55. Notice how the loss function becomes a 3D surface and the gradient is now a vector of two partial derivatives. The update step is performed for both parameters at the same time.

For a deeper dive into the specific math for deriving the gradients for Linear Regression's MSE cost function, you can review the following resource. Given your calculus background, you should be able to follow the application of the chain rule.

Gradient Descent Derivation

The article 'Gradient Descent Derivation' by Chris McCormick provides a detailed mathematical derivation for the partial derivatives of the MSE cost function. This is a great way to connect the abstract algorithm to a concrete application.

Read the sections 'Gradient Descent of MSE' and 'Final Update Rules'. Focus on how the partial derivatives with respect to heta_0 and heta_1 are calculated using the rules of differentiation we've learned.


6. A Practical View: Gradient Descent in Code

Since you have an interest in coding, let's see how this all translates to Python. The video from NeuralNine, which we used before, also implements gradient descent from scratch. Watching this will solidify your understanding by connecting the mathematical theory to a working program.

Gradient Descent From Scratch in Python - Visual Explanation

Let's watch the implementation part of the 'Gradient Descent From Scratch in Python' video. This will help you see the update loop in code.

Watch from 08:18 to 13:31. Focus on how the main loop implements the iterative update rule. You can see the learning_rate, the call to the derivative function y_derivative, and the update step current_position[0] - learning_rate * ... all coming together.


7. A Note on A Practical Variation: Stochastic Gradient Descent (SGD)

In our examples, the gradient was calculated using all the data points. This is called Batch Gradient Descent. When you have millions of data points, this can be very slow.

A common and more efficient variant is Stochastic Gradient Descent (SGD), where the gradient is estimated using just one randomly selected data point at each step. A middle ground is Mini-Batch Gradient Descent, which uses a small random subset (a "mini-batch") of the data. This provides a good balance between accuracy and speed and is the most common method used in deep learning.

The StatQuest video provides a great, simple explanation of this.

Gradient Descent, Step-by-Step

Finally, let's learn about this important practical variant of gradient descent.

Watch the final segment from 21:55 to 23:33. The key takeaway is that SGD and its variants are used to make training faster on large datasets by approximating the gradient instead of calculating it perfectly.


Conclusion

Fantastic work today! You have just learned the single most important optimization algorithm in machine learning. This concept will reappear constantly as you study linear regression, logistic regression, and neural networks.

Key Takeaways:

  • Gradient Descent is an iterative algorithm for finding the minimum of a function (the loss function).
  • It works by repeatedly taking steps in the direction opposite to the gradient.
  • The core update rule is .
  • The learning rate () is a critical hyperparameter that controls the step size. If it's too large, the algorithm can diverge; if it's too small, it will be too slow.
  • For functions with multiple parameters, we compute the gradient (vector of partial derivatives) and update all parameters simultaneously.
  • Stochastic Gradient Descent (SGD) is a faster, more practical variant used for large datasets.

Preview of the Next Lesson:

Gradient descent is the go-to tool for the unconstrained optimization problems that dominate machine learning. However, sometimes we encounter problems where the variables must satisfy certain conditions or constraints. In our next lesson, we will explore a powerful analytical technique for these situations: the method of Lagrange multipliers. This will add another important optimization tool to your mathematical toolkit for the GATE exam.