Course 2-week 1-Exercise 4 - gradient_check_n

what is going on here, I wore the as the instructions says but it gives error at function vector to dictionary
here are the screen shots.

{Moderator’s Edit: Solution Code Removed}

Hey @Mubarak_Ibrahim,
If you take a look at the implementation of forward_propagation, you will find that it returns 2 things, cost and cache, where the former one is considered as the output of this function.

Now, coming to gradient_check_n, J_plus[i] is supposed to contain the output of the forward propagation, and not the cache, but you are storing both of the things in this variable, which is the cause for this error. So, you need to modify your code as per the outputs of forward_propagation. I hope this helps.

P.S. - For future references, posting solution code publicly is strictly against the community guidelines. If a mentor needs to take a look at your code, (s)he will ask you to DM it.


I correct the error but it give me this error.
cannot reshape array of size 1 into shape (5,4)
the error takes me more than month I debagged the code more and more, but I didn’t find way

Hey @Mubarak_Ibrahim,
Can you please share the entire error stack, so that we can try to pin-point the issue?


Hey @Mubarak_Ibrahim,
Please note that you have an error in the following line of code:

theta_plus = theta_plus[i] + epsilon

In order to find out the exact error, print the values of these variables before and after this line of code, and I am sure you will be able to figure out the exact issue with this line of code. Let us know if this helps.


this is what my code is

theta_plus= np.copy(parameters_values)
theta_plus[i] = theta_plus[i]+ epsilon
J_plus[i],_= forward_propagation_n(X,Y, vector_to_dictionary(theta_plus[i]))

i did’t find any thing and the output says

--- 43   J_plus[i],_= forward_propagation_n(X,Y, vector_to_dictionary(theta_plus[i]))
 44         # YOUR CODE ENDS HERE

~/work/release/W1A3/ in vector_to_dictionary(theta)
53 “”"
54 parameters = {}
—> 55 parameters[“W1”] = theta[: 20].reshape((5, 4))
56 parameters[“b1”] = theta[20: 25].reshape((5, 1))
57 parameters[“W2”] = theta[25: 40].reshape((3, 5))

ValueError: cannot reshape array of size 1 into shape (5,4