# Gradient Check Course 2 Wee1 Ex4

**URL:** <https://community.deeplearning.ai/t/gradient-check-course-2-wee1-ex4/9652>\
**Category:** Improving Deep Neural Networks: Hyperparameter tun\
**Tags:** coursera-platform\
**Created:** [May 27, 2021, 12:51pm UTC](https://community.deeplearning.ai/t/gradient-check-course-2-wee1-ex4/9652 "2021-05-27T12:51:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_RSC](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mario\_RSC](https://community.deeplearning.ai/u/Mario_RSC)\
**Post date:** [May 27, 2021, 12:51pm UTC](https://community.deeplearning.ai/t/gradient-check-course-2-wee1-ex4/9652/1 "2021-05-27T12:51:34Z")

</div>

Dear all,

I do not really understand the usage of the for loop to go throught the parameters in Grad Check exercise the one to compute de gradapprox. Why do not vectorize this?

for i in range(num\_parameters):

```
    # Compute J_plus[i]. Inputs: "parameters_values, epsilon". Output = "J_plus[i]".
    #(approx. 3 lines)
    # theta_plus = # Step 1
    # theta_plus[i] = # Step 2
    # J_plus[i], _ =   

```

There is something here I do not understand in the instructions, if you copy the parameters to theta\_plus, you will copy a (N,1) column vector. but later on to take only the “[i]” value to compute J\_plus[i].

Later on, in order to be able to use the function vector\_to\_dictionary, you need to supply a vector not a scalar

So there is a mess in all this, or we use loops or we vectorize this whole thing so we can use the vector\_to\_dictionary function and others

Best Regards  
Mario

---

<div class="post-metadata">

**Author:** ![nramon](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nramon/32/129097_2.png) [@nramon](https://community.deeplearning.ai/u/nramon)\
**Post date:** [May 28, 2021, 12:32pm UTC](https://community.deeplearning.ai/t/gradient-check-course-2-wee1-ex4/9652/2 "2021-05-28T12:32:41Z")

</div>

Hi, @Mario_RSC.

Good question.

The problem is you have to hold the rest of the parameters constant while you approximate the gradient with respect to one of them. You’re not using `theta_plus[i]` only to compute `J_plus[i]`. You’re nudging `theta_plus[i]`, but you’re using all the parameters to approximate the gradient. Does that make sense?

Having said that, there are probably more [optimal](https://community.deeplearning.ai/t/doubt-on-assignment-gradient-check-course-2-week-1/4062/2) implementations 🙂

---

<div class="post-metadata">

**Author:** ![Mario\_RSC](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mario\_RSC](https://community.deeplearning.ai/u/Mario_RSC)\
**Post date:** [May 28, 2021, 6:18pm UTC](https://community.deeplearning.ai/t/gradient-check-course-2-wee1-ex4/9652/3 "2021-05-28T18:18:52Z")

</div>

I see thanks. I will try just for fun some kind of boolean mask.  
Best Regards.
