Use "-=" gives wrong result and does not pass the test

Use

parameters[“W” + str(l+1)] -= grads[“dW”+str(l+1)]*learning_rate

W​ill only pass two tests and fail one test but

parameters[“W” + str(l+1)] = parameters[“W” + str(l+1)] - grads[“dW”+str(l+1)]*learning_rate

W​ill pass all tests, What is the difference between “-=” and " = XX - "

1 Like

The two expressions create the same result, but the way they manage memory is different which is why the -= fails this test. There is a thread about this but I am on my phone and can’t give the link. Please stay tuned and I will send it in an hour or so. Or try searching for update_parameters.

2 Likes

@paulinpaloalto , I didn’t find this in the FAQ, but maybe I was looking in the wrong place?

Anyway, here is one of Paul’s many great write ups on the topic:

1 Like

Thanks, @jonaslalin! Yes, that was the thread I was thinking about. I was wrong that it is linked from the FAQ Thread, but will hope to fix that when I get back to a real computer!

Sorry, I just remembered that they fixed the test cases for update_parameters in Week3 Planar Data to fix this problem: they no longer fail if your code modifies the global data.

@donnie1123: That must mean you do not have the latest version of the tests for this assignment. You should follow the “get latest version” procedure that is documented on the FAQ Thread. Please read the instructions all the way down to the bottom where it explains how to delete the public_tests.py and other python support files, so that you make sure to get the latest.

Thanks for your answer. But my problem occurred in Course1 Week 4 First Assignment Exercise 10. The assignment is updated on 1 July 2021, 8:50 AM (Maybe the latest version). I found it if we replace parameters = params.copy by parameters = copy.deepcopy(params), It will pass all the tests. Maybe in the week 4 test, if we modify the params using shallow copy. It will still fail.


Yes, you’re right. I checked and they did not fix this issue in the Week 4 Step by Step exercise. Since you are copying a dictionary in that case, it turns out you need to either do a deepcopy of the whole dictionary or do shallow copies of each entry individually. Doing a shallow copy of the dictionary does not actually break the connection to the original memory: it just creates new dictionary entries that point at the same underlying objects.