Week1 Exercise3 Gradient Checking

What’s the use of np.copy?
There is a line of code as follows: (since it’s directly given by the hint in the exercise, maybe it’s ok to post it here?)

theta_plus = np.copy(parameters_values)

When I saw this hint, I naturally want to find whether it’s also ok to simply use:
theta_plus = parameters_values

Well, it gives the wrong answer.

But if I use np.copy only for this theta_plus, while not for theta_minus (still use theta_minus = parameters_values). The answer is right and pass the test.

Now I am wondering why? Any hint, please?

Here’s a thread which explains that. Make sure to read this later post on that same thread as well.

Thanks a lot!
So maybe I can just use copy or deepcopy everytime I want to write some code like B=A ?

Yes, I think that’s a good idea. Using deepcopy is the better idea, because just plain copy doesn’t help in the case that the RHS is an object reference to a dictionary that contains numpy arrays.

Of course it only matters when the RHS is an object reference. If it’s a python scalar, then it’s not necessary. But it does no harm really, other than wasting some cpu cycles at runtime. So that’s not really a reason not to be on the safe side …

1 Like