Week 1 Assignment 2 Ex 2: output doesn't pass the test

In the excercise, dW3, dW2 and dW1 have to be coded. This looks easy, since in the comments the code is practically given. However, this code (with or without None) doesn’t give the right test results.

Partly, I can understand this, since I don’t understand why the commented lines should be the right implementation.

I would expect that dWl is backprop + regularization
The backprop is in my opinion A[l-1]. dZ[l] and the regularization (lambd/m)*W[l]. Is this right?
I tried to add both terms, but that gave a failure.

Note: What does “1.” stand for? Does this simply mean 1 or does it mean something else?

Help is appreciated!

Please show us the actual output you are getting.

In python 1. and 1 are not the same thing: the first is a floating point value and the second is an integer. You have to be careful about things like that because in the old days (python 2.x), if you did this:

m = 5
x = 1 / (2 * m)

You would end up with x = 0 because the computation was computed with only integer values. They fixed that specific case in python 3.x, but it’s a good idea to be careful about types in general.

If I take the code from the comments (without the “None”) and run it, I get:

dW1 =
[[-0.10206168 0.14712822 -0.16007111]
[-0.18297295 0.26376681 -0.28697042]]
dW2 =
[[0.72241565 0.72521935]
[0. 0. ]
[0. 0. ]]
dW3 =
[[-1.68070929 0. 0. ]]
1 Tests passed
1 Tests failed

AssertionError Traceback (most recent call last)
in
5 print (“dW2 = \n”+ str(grads[“dW2”]))
6 print (“dW3 = \n”+ str(grads[“dW3”]))
----> 7 backward_propagation_with_regularization_test(backward_propagation_with_regularization)

~/work/release/W1A2/public_tests.py in backward_propagation_with_regularization_test(target)
100 ]
101
→ 102 multiple_test(test_cases, target)
103
104 def forward_propagation_with_dropout_test(target):

/opt/conda/lib/python3.7/site-packages/dlai_tools/testing_utils.py in multiple_test(test_cases, target)
162 print(‘\033[91m’, len(test_cases) - success, " Tests failed")
163 raise AssertionError(
→ 164 “Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))

AssertionError: Not all tests were passed for backward_propagation_with_regularization. Check your equations and avoid using global variables inside the function.

You have to replace each None with your own code. Check the instructions from the notebook:
“For each [None], you have to add the regularization term’s gradient” and that is equal to \frac{\lambda}{m} W.

Here are the correct answers:

dW1 = 
[[-0.25604646  0.12298827 -0.28297129]
 [-0.17706303  0.34536094 -0.4410571 ]]
dW2 = 
[[ 0.79276486  0.85133918]
 [-0.0957219  -0.01720463]
 [-0.13100772 -0.03750433]]
dW3 = 
[[-1.77691347 -0.11832879 -0.09397446]]
 All tests passed.

Here’s what I get if I just erase the “None” values from each of the dW expressions:

dW1 = 
[[-0.10206168  0.14712822 -0.16007111]
 [-0.18297295  0.26376681 -0.28697042]]
dW2 = 
[[0.72241565 0.72521935]
 [0.         0.        ]
 [0.         0.        ]]
dW3 = 
[[-1.68070929  0.          0.        ]]
 1  Tests passed
 1  Tests failed

Hmmm, that looks like exactly what you get. As you say, this should be pretty straightforward: they give you the general formula for what you need to replace the “None” with in the instructions. But if I’m reading the tea leaves correctly here, it looks like you just left out the regularization terms altogether. Or maybe you typed them in, but forgot to click “Shift - Enter” on the function cell and just ran the test again? That just runs the old code. Try “Kernel → Restart and Clear Output” and then “Cell → Run All” and see if that works better.

I didn’t understand what was meant by “None”. Now I do and it works. Thanks for your hint.

None is just a syntactic placeholder that you need to replace with the correct code. If you don’t remove it, then you get a syntax error. If you simply remove it, then you have not done regularization at all.

1 Like