# Week1 Programming Assignment: Gradient Checking

**URL:** <https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** coursera-platform\
**Created:** [May 14, 2021, 2:44pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872 "2021-05-14T14:44:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmsrilatha](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@rmsrilatha](https://dlai.staged-by-discourse.com/u/rmsrilatha)\
**Post date:** [May 14, 2021, 2:44pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/1 "2021-05-14T14:44:14Z")

</div>

## Iam getting this error:

AssertionError Traceback (most recent call last)  
 in   
6 assert not(type(difference) == np.ndarray), “You are not using np.linalg.norm for numerator or denominator”  
7  
----\> 8 gradient\_check\_n\_test(gradient\_check\_n, parameters, gradients, X, Y)

~/work/release/W1A3/public\_tests.py in gradient\_check\_n\_test(target, parameters, gradients, X, Y)  
56 ]  
although I have used np.linalg.norm in my program

# Compare gradapprox to backward propagation gradients by computing difference.

```
# (approx. 1 line)
numerator = np.linalg.norm(grad-gradapprox) # Step 1'
denominator = np.linalg.norm(grad)+np.linalg.norm(gradapprox) # Step 2'
difference = numerator/denominator # Step 3'

```

---

<div class="post-metadata">

**Author:** ![ken25](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@ken25](https://dlai.staged-by-discourse.com/u/ken25)\
**Post date:** [May 16, 2021, 9:55am UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/2 "2021-05-16T09:55:33Z")

</div>

me too and I don’t know why

---

<div class="post-metadata">

**Author:** ![nramon](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/nramon/32/129097_2.png) [@nramon](https://dlai.staged-by-discourse.com/u/nramon)\
**Post date:** [May 16, 2021, 3:19pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/3 "2021-05-16T15:19:41Z")

</div>

Hi, @rmsrilatha and @ken25.

Please see if [this](http://dlai.staged-by-discourse.com/t/gradient-checking-doubt-about-failed-code-checking-on-submission/1948/) post helps. Good luck 🙂

---

<div class="post-metadata">

**Author:** ![blackfeather](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@blackfeather](https://dlai.staged-by-discourse.com/u/blackfeather)\
**Post date:** [June 4, 2021, 5:58am UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/5 "2021-06-04T05:58:04Z")

</div>

Did you find any solution?

---

<div class="post-metadata">

**Author:** ![blackfeather](https://avatars.discourse-cdn.com/v4/letter/b/e79b87/32.png) [@blackfeather](https://dlai.staged-by-discourse.com/u/blackfeather)\
**Post date:** [June 4, 2021, 5:58am UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/6 "2021-06-04T05:58:47Z")

</div>

That post didnt really help! I am still stuck

---

<div class="post-metadata">

**Author:** ![neurogeek](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/neurogeek/32/57_2.png) [@neurogeek](https://dlai.staged-by-discourse.com/u/neurogeek)\
**Post date:** [June 4, 2021, 1:28pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/7 "2021-06-04T13:28:47Z")

</div>

Hi @blackfeather,

I think @nramon helped out in the other thread, right?

Happy learning!

---

<div class="post-metadata">

**Author:** ![brunods](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@brunods](https://dlai.staged-by-discourse.com/u/brunods)\
**Post date:** [July 1, 2021, 2:09pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/8 "2021-07-01T14:09:13Z")

</div>

@nramon  
@neurogeek

Hi guys I am having the same problem and none of the posts mentioned are very helpful.

my code is the same as @rmsrilatha

---

<div class="post-metadata">

**Author:** ![Kimberly\_Chan](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/kimberly_chan/32/14838_2.png) [@Kimberly\_Chan](https://dlai.staged-by-discourse.com/u/Kimberly_Chan)\
**Post date:** [June 9, 2022, 2:55am UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/9 "2022-06-09T02:55:26Z")

</div>

I followed all the linked threads and still couldn’t figure out what I’m missing. I did use np.linalg.norm, and checked that X and Y are capitalized. My error codes are:

## There is a mistake in the backward propagation! difference = 0.955106072623136

AssertionError Traceback (most recent call last)  
 in   
6 expected\_values = [0.2850931567761623, 1.1890913024229996e-07]  
7 assert not(type(difference) == np.ndarray), “You are not using np.linalg.norm for numerator or denominator”  
----\> 8 assert np.any(np.isclose(difference, expected\_values)), “Wrong value. It is not one of the expected values”

AssertionError: Wrong value. It is not one of the expected values

---

<div class="post-metadata">

**Author:** ![anon57530071](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anon57530071](https://dlai.staged-by-discourse.com/u/anon57530071)\
**Post date:** [June 10, 2022, 12:32pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/10 "2022-06-10T12:32:54Z")

</div>

Hi,  
Did you fix this issue ?  
As “grad” does not change during gradient\_check\_(), the problem is “gradapprox” side. In your case, “difference” is close to 1. With looking at the formula, if “gradapprox” is really close to 0, or is far larger than “grad”, “difference” could be very close to 1. As both “forward\_propagation” and “back\_propagation” are provided, the problem should be in calculating “gradapprox”.

---

<div class="post-metadata">

**Author:** ![Kimberly\_Chan](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/kimberly_chan/32/14838_2.png) [@Kimberly\_Chan](https://dlai.staged-by-discourse.com/u/Kimberly_Chan)\
**Post date:** [June 10, 2022, 4:21pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/11 "2022-06-10T16:21:54Z")

</div>

I don’t see an error in my gradapprox formula: gradapprox[i]=np.sum(J\_plus[i]-J\_minus[i])/(2\*epsilon)

should backward\_propagation\_n be used in place of grad in the difference calculations?

backward\_propagation\_n would have needed cache as an argument but in the calculation of J\_plus[i] and J\_minus[i] the assignment instructions says to use “\_” in place of cache.

---

<div class="post-metadata">

**Author:** ![anon57530071](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@anon57530071](https://dlai.staged-by-discourse.com/u/anon57530071)\
**Post date:** [June 10, 2022, 4:39pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/12 "2022-06-10T16:39:48Z")

</div>

You need to use “forward\_propagation\_n”, since this is N-dimensional operation.  
As you see, “cost” and “cache” are returned from “forward\_propagation\_n”. In Python, “\_” (underscore) means, “does not care”… 🙂 So, we ignore 2nd parameter, which is “cache”. As you see, it is not used in this scope. (you can set dummy variable instead, of course. But, it is not used anyway.)  
So, please use “forward\_propagation\_n”

By the way, you use np.sum for a calculation of J\_plus[i]-J\_minus[i]. Both J\_plus[i] and J\_minus[i] are 1 dimensional array. We just need to focus on the "i"th location of gradapprox[]. So, np.sum is not required. (Actually, the result is same, though…)

---

<div class="post-metadata">

**Author:** ![ImAli](https://avatars.discourse-cdn.com/v4/letter/i/3ec8ea/32.png) [@ImAli](https://dlai.staged-by-discourse.com/u/ImAli)\
**Post date:** [June 20, 2024, 2:30pm UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/13 "2024-06-20T14:30:42Z")

</div>

I am having the same issue. Someone please help me

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/tmosh/32/9321_2.png) [@TMosh](https://dlai.staged-by-discourse.com/u/TMosh)\
**Post date:** [June 21, 2024, 3:23am UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/14 "2024-06-21T03:23:08Z")

</div>

Tnis thread has been cold for two years. I recommend you start a new thread for your question. Please include complete details.

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/tmosh/32/9321_2.png) [@TMosh](https://dlai.staged-by-discourse.com/u/TMosh)\
**Post date:** [June 21, 2024, 3:23am UTC](https://dlai.staged-by-discourse.com/t/week1-programming-assignment-gradient-checking/5872/15 "2024-06-21T03:23:25Z")

</div>

Closed on June 21, 2024, 3:23am UTC


