Doubt on C1W3 Assignment

I was stuck in the “Update Parameters” function step in the Neural Networks and Deep Learning C1W3 Assignment, where I received the following error. Also, b2 was also different from the expected output. I passed tests on both b1 (initialisation function) and db1 (backward propagation function). I used the following to update b1 : b1 = b1 - learning rate * db1. Not sure what went wrong and my b1 output is a number instead of a 4X1 vector. Need some guidance. Thanks.

W1 = [[-0.00643025 0.01936718]
[-0.02410458 0.03978052]
[-0.01653973 -0.02096177]
[ 0.01046864 -0.05990141]]
b1 = [[0.00010457]]
W2 = [[-0.01041081 -0.04463285 0.01758031 0.04747113]]
b2 = [[9.14954378e-05]]

AssertionError Traceback (most recent call last)
7 print("b2 = " + str(parameters[“b2”]))
----> 9 update_parameters_test(update_parameters)

~/work/release/W3A1/ in update_parameters_test(target)
244 assert output[“W1”].shape == expected_output[“W1”].shape, f"Wrong shape for W1."
→ 245 assert output[“b1”].shape == expected_output[“b1”].shape, f"Wrong shape for b1."
246 assert output[“W2”].shape == expected_output[“W2”].shape, f"Wrong shape for W2."
247 assert output[“b2”].shape == expected_output[“b2”].shape, f"Wrong shape for b2."

AssertionError: Wrong shape for b1.

1 Like

Hi @KCK ,

The parameters and gradients are given by the test case. You can use a print statement to print out the shape of W1,b1 and W2,b2 to help find where the problem might be.

Did you modify any other code in the update_parameters() function, or in its test cell?

The test for update_parameters() does not call any of your other functions. So the issue would only be in that function, or maybe due to some other changes you made by accident.

The only other likely place for an error is in the code for db1 = ...

Thanks a lot. I might have accidentally changed some codes in the update_parameters() function as I passed all tests before this issue happened. Mind guiding me:
(a) the way to reset the function to the original so that I can start afresh of my work on the function again or
(b) if this doesn’t work, the way to wipe everything clean to start the whole assignment again. I don’t mind that as I have already saved a copy of my work in a notebook so that I can copy and paste my work prior to the function.

I already tried Lab Help to update the notebook to the latest version. It did not help (probably not the right way). Sorry for so many questions as I am new to this course/Specialisation. Thx again.

To get a new copy of the notebook, you need to rename your current copy (using “File → Rename”), then use “Help → Get latest version” to get a new copy.

Then use the File menu to Open the new copy. It will be the one with the original file name.

After that it is best if you use “Kernel → Restart and clear output” before you start working on the new notebook.

I managed to get a new copy and start afresh on the assignment again. Yet, the same issue happened when I reached the “update_parameter” function. I added a new line and used the print statement on all the parameters just before the said function. All of them showed the message of “undefined” except for A2, a 1x3 vector. I suspect this might have caused the issue. Need some help/guidance.

Hi @KCK ,

You need to refresh the kernel and rerun all the code cells.

From the menu bar on the top of your notebook, please try the following:
kernel ->restart and clear all output
and then rerun all the code cell from start

Thanks for your prompt response. I followed the suggestion to clear all output and rerun all the code from the start. The same issue happened reaching the update_parameter(). I printed all the parameters before the function step - all are undefined except A2. Need help on this.

Please post a screen capture image that shows what you are describing.

Hi @KCK ,

I am not sure where your A2 comes from. I am not sure what you have done. First, the definition of the function looks like this:

def update_parameters(parameters, grads, learning_rate = 1.2):

Your print statement should be within that function definition. For example, if you want to print the shape of W1:

print(“shape of W1:”, W1.shape)

This print statement should be located after W1 is defined:

W1 = parameters[“W1”]

Pls see the attached screenshot of what I meant on A2. I added one cell before the update_parameters fn, and printed the parameters one by one (I included 2 parameters, A1 and A2, for illustration sake). The print statement was outside of any specific function and I suppose all parameters should read “undefined”. The exception is on A2. I suspect this might have caused the issue (but I might be wrong). Appreciate guidance.

Hi @KCK ,

You can not just add a cell above the update_parameters() function to access the values passed into the function. As the values passed to the function are specified when the function is called, therefore, it has nothing to do with variables declared from other code cell.
Please follow the my suggestion from earlier reply and put the print statement within the function.
In order for you to get the most out of the course, you might like to enrol to a python programming course.
Here is a link to a list of python programming courses from Coursera

In addition to @Kic’s excellent comments, the update_parameters() function doesn’t even use the variables A1 or A2. So it isn’t clear to me why you would want to print them at that point in the notebook.

And W1 is used in that function, but it a key in the “parameters” dictionary. You have to read it from the dictionary and assign it to a local ‘W1’ variable before you can use it.

The only way that A1, A2, and W1 would be defined at that point in the code where you added cell In[23], would be if they were global variables.

Global variables are generally not a good idea.

I agree that maybe a tutorial on Python programming would be useful for you.

Thanks for the info and guidance. If A2 is not the issue, I would rework my coding on that function once again. Regarding W1, I already used copy.deepcopy(…“W1”) and assigned it to local W1 variable within the function. It seemed to work fine. Rgds.