W3_A1_Update_parameters_Test fails

Update: In the time since this thread was first created, they have fixed the test cases for update_parameters in the Week 3 Planar Data assignment so that they no longer fail if you simply use -= without copying the parameters first. Note that this problem still exists in the Week 4 update parameters logic. It is also worth reading the thread just for the information about how things work in python with object references and procedure call semantics.

Did you use the “in place” operators for your update? That will fail the grader unless you are careful to copy the variables first to break the connection to the global definition of the input data. That is to say this fails:

W1 -= ... (update formula) ...

But this will pass the test cases (and the grader):

W1 = W1 - ... (update formula) ...

The reasons for this are pretty subtle. Those two statements produce the same output value, but how they manage memory is very different. In python, objects are passed “by reference” when making function calls. That means that the variable parameters inside the update_parameters function is actually an object reference to the global definition (the same one that was used to invoke the function). That means that if you write this:

W1 = parameters['W1']

Inside the function, that local variable W1 is actually an object reference to the global definition of the data in that dictionary. So if you then write this:

W1 -= <some formula>

You have actually modified the global data, because the definition of that operator “-=” is that it operates “in place”. That same global data is reused in several tests, so the subsequent tests fail because the inputs have been changed.

You can make “-=” work by first copying the elements of the dictionary internally to the function to break the connection between the local W1 and the global objects indexed by the dictionary:

W1 = parameters['W1']
W1 = W1.copy()

After that operation, the local variable W1 is now an object reference to a newly allocated memory object which is different from the original object referenced by parameters['W1'], but which contains the same values of course. There is a simpler solution using “deepcopy” that avoids having to individually copy every element of the dictionary: see this later post on this same thread for more information.

Just to complete the explanation here, the reason that this version works without the copy:

W1 = W1 - <update formula>

Is that the semantics of that assignment statement in python are that the RHS is allocated freshly as a new object reference, so then it gets assigned to W1 on the LHS which thus becomes a reference to the new object. The reference to the previous object referenced by W1 is dropped and may be garbage collected (although it won’t be in this case because there’s still a live global reference).

As I said earlier, it’s subtle. You may think python is a nice clean and easy language, but there can be some pretty nasty surprises from code that looks relatively straightforward.