Gradient_Checking Assignment - Exercise 4 Question

The above pseudocode to implement grad check is taken directly from the lab assignment.

According to my understanding, in the above for each set of parameters, we increase/decrease them by epsilon, and the compute the cost. Then compute gradapprox.

Question 1: In step 2 why do we only update weights i? I thought we were supposed to add epsilon to the entire weight vector at once. Otherwise, whats the point of putting them all together in variable theta?

Question 2: In step 3, how come we compute cost as a vector? Shouldn’t it be a scalar? And we do it i times for each J_i. I understand that these costs are different, but I don’t really understand what’s the point of what’s being done here.

Question 3: Why compute gradapprox i times? I did it once after the for loop and it worked just fine. Vectorized!

Thanks in advance!

Hi @Maro2005 ,

Welcome to the platform and glad to see that you’re looking through assignments with purpose.

To confirm we talking about the Course 2 - Week 1 assignment, and the grandient_check_n function?

The first gradient check assumes a single parameter and for simplicity of calculations, compares the derivative computation in backward propagation with gradient for that parameter’s weight.

The second one is a vector because it assumes n dimensional input for a fraud detection model, and gradient check is performed to ensure you are able to debug your use-case if outputs of your model look off for a realworld scenario.

I hope this clarified your question.

Feel free to reach out and message if you have more questions.

Best,
Parul

The thing to keep in mind here is that the cost may be a scalar, but the gradient is a vector because it contains one element for every parameter (w^{[l]}_{ij} and b^{[l]}_{ij}) which is the derivative of J w.r.t. that parameter. Here for the purposes of gradient checking, we handle things a bit differently in that we have “unrolled” all the W and b parameters for all layers into one big vector which we call \theta specifically for the purpose of being able to compare the real gradient code we have written with our “finite difference” method of estimating the gradients. Then we need the loop because we want to estimate the gradient (derivative) for each parameter separately: our gradient code could be wrong for any one of the cases and that’s what we are trying to detect. Of course we do that checking all at once by comparing the actual gradient vector with our approximated one and looking at the norm of the difference between the real and estimated gradients.