# C1W1 Lab04, why aren't the gradients perpendicular to the contour lines? (SOLVED)

**URL:** <https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511>\
**Category:** Supervised ML: Regression and Classification\
**Tags:** week-module-1\
**Created:** [March 14, 2024, 5:10am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511 "2024-03-14T05:10:44Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric11235](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@eric11235](https://community.deeplearning.ai/u/eric11235)\
**Post date:** [March 14, 2024, 5:10am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/1 "2024-03-14T05:10:45Z")

</div>

In the “Plotting” section, the contour plot has the “path of gradient descent” overlaid with red arrows. I would expect the red arrows to be perpendicular (normal) to the contour lines. Can anybody explain to me why this isn’t the case?

 ![contour](https://global.discourse-cdn.com/dlai/original/3X/3/f/3ff3d824e682fd44df6c8ce019cf2735108cc400.png)

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [March 14, 2024, 6:12am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/2 "2024-03-14T06:12:26Z")

</div>

this is cut of 3 dimensional (or even more dimensional) spatial behaviour. Path even might not be as direct as shown here! Why should the arrows be perpendicular to the contours?

---

<div class="post-metadata">

**Author:** ![eric11235](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@eric11235](https://community.deeplearning.ai/u/eric11235)\
**Post date:** [March 14, 2024, 2:40pm UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/3 "2024-03-14T14:40:51Z")

</div>

Lab 4 is focused on the behavior of the batch gradient descent algorithm in a univariate linear regression. Univariate linear regression means the cost function is a function of w and b. So I’m not sure why you write “_or even more dimensional spatial behavior_”. It’s 3 dimensions.

Gradients **are** perpendicular to the contours. So I would expect the path vectors to be perpendicular to the contour lines. Since the lab is using batch gradient descent, the calculated gradient is exact; it’s not an estimate or approximation.

This is what I would expect to see:  
 ![350px-Gradient_descent.svg](https://global.discourse-cdn.com/dlai/original/3X/b/7/b7bf25ad6b19ca5597d3bf7d94fb8da03c12b464.png)

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [March 15, 2024, 12:50am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/5 "2024-03-15T00:50:31Z")

</div>

Hello @eric11235 Eric,

I believe that is because our impression of the “right angle” is always based on that we have an 1:1 image aspect ratio.

Check this out:

(Below is a GIF animation)  
 ![Project001 (3)](https://global.discourse-cdn.com/dlai/original/3X/e/8/e8a7b67e60e0a4cf73557ef660279186ab3d5a88.gif)

(Below is an image)

 ![contour](https://global.discourse-cdn.com/dlai/original/3X/3/f/3ff3d824e682fd44df6c8ce019cf2735108cc400.png)

The GIF shows how changing the aspect ratio “correct” our “right angle”. The code controlled the aspect ratio with the figure size and the ranges of w and b, so you may change those settings to make it 1:1 like what I have done below:

(Below is an image)

 ![image](https://global.discourse-cdn.com/dlai/original/3X/6/0/60dc9e1a4d339b634110272f9128ae708c4b1e6f.png)  
As you can see, (1) same range for both w and b, and (2) a square image.

Cheers,  
Raymond

---

<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:** [March 15, 2024, 1:14am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/6 "2024-03-15T01:14:56Z")

</div>

Excellent point, I did not notice that the graph axes were skewed.

---

<div class="post-metadata">

**Author:** ![eric11235](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@eric11235](https://community.deeplearning.ai/u/eric11235)\
**Post date:** [March 15, 2024, 4:21am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/7 "2024-03-15T04:21:07Z")

</div>

Son of a gun. Thanks for taking the time to make the animation!

---

<div class="post-metadata">

**Author:** ![eric11235](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@eric11235](https://community.deeplearning.ai/u/eric11235)\
**Post date:** [March 15, 2024, 4:26am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/8 "2024-03-15T04:26:54Z")

</div>

Our hero @rmwkwok has solved the mystery, but I wanted to speak to something you wrote: "_dj\_dw and dj\_db are not shown in that plot, as it would require a 3D rendering._ I don’t think that’s true. (dj\_dw, dj\_db) is a vector in 2 space. The contour plot has w and b axes, and that vector can be plotted in that plane. The value of j is not important, but the resulting j after the values of w and b are altered is visualized with the contour lines.

---

<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:** [March 15, 2024, 4:46am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/9 "2024-03-15T04:46:52Z")

</div>

I realize that better now, and I’ve deleted my post.

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [March 15, 2024, 5:21am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/10 "2024-03-15T05:21:16Z")

</div>

Yup, @eric11235, if you like some maths, you can think about this like the below too:

 ![image](https://global.discourse-cdn.com/dlai/original/3X/d/8/d859dc3868be2092c3f6a23acb4bbc87118ec435.jpeg)

Cheers,  
Raymond

---

<div class="post-metadata">

**Author:** ![eric11235](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@eric11235](https://community.deeplearning.ai/u/eric11235)\
**Post date:** [March 15, 2024, 5:48am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/11 "2024-03-15T05:48:35Z")

</div>

Exactly! Thanks.

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [March 15, 2024, 7:33am UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/12 "2024-03-15T07:33:06Z")

</div>

These are drawings anyways to give you an idea of what it supposed to happen they are not architectural designs!

---

<div class="post-metadata">

**Author:** ![eric11235](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@eric11235](https://community.deeplearning.ai/u/eric11235)\
**Post date:** [March 15, 2024, 5:41pm UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/13 "2024-03-15T17:41:54Z")

</div>

Gradients are perpendicular to the contour lines. I think it is important to understand this when one is learning about the behavior of the batch gradient descent algorithm and how it seeks a local minimum. From a pedagogical standpoint, the plots, in my opinion, should highlight this fact, not obscure it. It appears that you have a different opinion.

---

<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:** [March 15, 2024, 6:09pm UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/14 "2024-03-15T18:09:41Z")

</div>

There has a been a ticket submitted to address this issue.  
I think it’s been covered adequately.  
Closing the thread.

---

<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:** [March 15, 2024, 6:09pm UTC](https://community.deeplearning.ai/t/c1w1-lab04-why-arent-the-gradients-perpendicular-to-the-contour-lines-solved/590511/15 "2024-03-15T18:09:45Z")

</div>

Closed on March 15, 2024, 6:09pm UTC


