# Gradient Descent two local minima

**URL:** <https://community.deeplearning.ai/t/gradient-descent-two-local-minima/625697>\
**Category:** Supervised ML: Regression and Classification\
**Tags:** week-module-1\
**Created:** [May 11, 2024, 4:58pm UTC](https://community.deeplearning.ai/t/gradient-descent-two-local-minima/625697 "2024-05-11T16:58:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sujirsachin](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/sujirsachin/32/87248_2.png) [@sujirsachin](https://community.deeplearning.ai/u/sujirsachin)\
**Post date:** [May 11, 2024, 4:58pm UTC](https://community.deeplearning.ai/t/gradient-descent-two-local-minima/625697/1 "2024-05-11T16:58:24Z")

</div>

> **[Gradient descent for linear regression - Week 1: Introduction to Machine...](https://www.coursera.org/lecture/machine-learning/gradient-descent-for-linear-regression-lgSMj)**
>
> Video created by DeepLearning.AI, Stanford University for the course "Supervised Machine Learning: Regression and Classification ". Welcome to the Machine Learning Specialization! You're joining millions of others who have taken either this or ...

In the video above, I didn’t understand if two local minima exists, how would the algorithm get to the global minimum?

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [May 11, 2024, 5:25pm UTC](https://community.deeplearning.ai/t/gradient-descent-two-local-minima/625697/2 "2024-05-11T17:25:32Z")

</div>

There is no guarantee that gradient descent will find the global minimum.

That’s why it’s important to use convex cost functions whenever possible. They only have one minimum.

---

<div class="post-metadata">

**Author:** ![pastorsoto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/pastorsoto/32/127023_2.png) [@pastorsoto](https://community.deeplearning.ai/u/pastorsoto)\
**Post date:** [May 11, 2024, 7:06pm UTC](https://community.deeplearning.ai/t/gradient-descent-two-local-minima/625697/3 "2024-05-11T19:06:58Z")

</div>

Hi @sujirsachin there are two parts of this question, the first part is local minimum which is the smallest value for an input of a function, so you take an specific range of values and the minimum value of that range is the local minimum, so you can have several local minimum. The second part is the global minimum which is the smallest value in the domain of the function (across all possible range of values of the function the lowest value)

Let’s evaluate an example

![image](https://global.discourse-cdn.com/dlai/original/3X/c/e/ce0ec3b97f72f133ce4679931fe0be0d53f1c910.png)

In the above picture you have some range of values in the x axis representing inputs and the y-axis the function outputs

Example:

Range between 1 to 5 you get the minimum value -1 (local minimum)  
Range between 5 to 10 you get the minimum value 2 (another local minimum)  
Range between 10 to 15 you get the minimum value -1.3 (another local minimum)

In this case the global minimum is -1.3 assuming the domain of the function to be x=1 to 15. This is the lowest value across all the intervals

When you want to optimize you can get “stuck” at a local minimum and never reach the global minimum, since you need to evaluate a wide range of values for the entire function to make it work.

Let me know if this helps!

---

<div class="post-metadata">

**Author:** ![sujirsachin](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/sujirsachin/32/87248_2.png) [@sujirsachin](https://community.deeplearning.ai/u/sujirsachin)\
**Post date:** [May 12, 2024, 7:15am UTC](https://community.deeplearning.ai/t/gradient-descent-two-local-minima/625697/4 "2024-05-12T07:15:05Z")

</div>

@pastorsoto Thank you so much for taking the time to write a detailed explanation.

One follow up question- So if we are “stuck” at the local minimum and never reach the global minimum, there’s no way we can reach the global minimum correct?

---

<div class="post-metadata">

**Author:** ![pastorsoto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/pastorsoto/32/127023_2.png) [@pastorsoto](https://community.deeplearning.ai/u/pastorsoto)\
**Post date:** [May 12, 2024, 5:29pm UTC](https://community.deeplearning.ai/t/gradient-descent-two-local-minima/625697/5 "2024-05-12T17:29:59Z")

</div>

Yes, selecting the right parameters for optimizing your model could help to reach the global minimum, the thing is that you won’t know if you are at local or global minimum, that’s what @TMosh is suggesting when he pointed out that you need to use convex function

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [May 12, 2024, 5:33pm UTC](https://community.deeplearning.ai/t/gradient-descent-two-local-minima/625697/6 "2024-05-12T17:33:14Z")

</div>

If you have to use a non-convex cost function (such as for training a neural network), then you can train multiple times using different initial weight values. Then compare all of the solutions and select the one with the lowest cost.

Sometimes you don’t need to find the global minimum - often any minimum that is “good enough” will do.
