Week 4 - Assignment 1 Error - Wrong output for variable 2. for Exercise 7

Hi,

I am working on Week4 - Assignment 1 of Neural Networks and Deep Learning Course. I am getting below error when I run linear_backward function.

I checked the formula for db per np.sum requirement. It looks right to me. Some how it is giving error for how db is calculated.

Can you please advise what is the issue here?

Thanks,

Radhika

Error: Wrong output for variable 2.
 2  Tests passed
 1  Tests failed

Please show us the output of the full test, including the output that is being generated by your code.

For example here’s the output that I see for that function with a few debugging print statements that I added:

dZ.shape (3, 4)
A_prev.shape (5, 4)
W.shape (3, 5)
dA_prev: [[-1.15171336  0.06718465 -0.3204696   2.09812712]
 [ 0.60345879 -3.72508701  5.81700741 -3.84326836]
 [-0.4319552  -1.30987417  1.72354705  0.05070578]
 [-0.38981415  0.60811244 -1.25938424  1.47191593]
 [-2.52214926  2.67882552 -0.67947465  1.48119548]]
dW: [[ 0.07313866 -0.0976715  -0.87585828  0.73763362  0.00785716]
 [ 0.85508818  0.37530413 -0.59912655  0.71278189 -0.58931808]
 [ 0.97913304 -0.24376494 -0.08839671  0.55151192 -0.10290907]]
db: [[-0.14713786]
 [-0.11313155]
 [-0.13209101]]
dZ.shape (3, 4)
A_prev.shape (5, 4)
W.shape (3, 5)
dZ.shape (3, 4)
A_prev.shape (5, 4)
W.shape (3, 5)
dZ.shape (3, 4)
A_prev.shape (5, 4)
W.shape (3, 5)
 All tests passed.

Here is the output:

dA_prev: [[-1.15171336  0.06718465 -0.3204696   2.09812712]
 [ 0.60345879 -3.72508701  5.81700741 -3.84326836]
 [-0.4319552  -1.30987417  1.72354705  0.05070578]
 [-0.38981415  0.60811244 -1.25938424  1.47191593]
 [-2.52214926  2.67882552 -0.67947465  1.48119548]]
dW: [[ 0.07313866 -0.0976715  -0.87585828  0.73763362  0.00785716]
 [ 0.85508818  0.37530413 -0.59912655  0.71278189 -0.58931808]
 [ 0.97913304 -0.24376494 -0.08839671  0.55151192 -0.10290907]]
db: [[-0.58855142]
 [-0.4525262 ]
 [-0.52836405]]
Error: Wrong output for variable 2.
 2  Tests passed
 1  Tests failed

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-17-3427f65cb756> in <module>
      6 print("db: " + str(t_db))
      7 
----> 8 linear_backward_test(linear_backward)

~/work/release/W4A1/public_tests.py in linear_backward_test(target)
    395     ]
    396 
--> 397     multiple_test(test_cases, target)
    398 
    399 

~/work/release/W4A1/test_utils.py in multiple_test(test_cases, target)
    140         print('\033[92m', success," Tests passed")
    141         print('\033[91m', len(test_cases) - success, " Tests failed")
--> 142         raise AssertionError("Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))
    143 

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

You’re right that only your db values don’t match the “Expected” ones. But if you get out your calculator app, you’ll notice that your db[i] for all i are exactly 4 times the expected values.

Hmmmm, maybe that’s a clue about where your code went off the rails. Note that m = 4 in the test case here. :nerd_face:

I am doing calculating db as

{moderator edit - solution code removed}

what do you mean by calculator app?

It seems to be running 4 times

m is:4
The shape of W is: (3, 5)
The shape of b is: (3, 1)
The shape of A_prev is: (5, 4)
The shape of dW is: (3, 5)
The shape of db is: (3, 1)
The shape of dA_prev is: (5, 4)
dA_prev: [[-1.15171336  0.06718465 -0.3204696   2.09812712]
 [ 0.60345879 -3.72508701  5.81700741 -3.84326836]
 [-0.4319552  -1.30987417  1.72354705  0.05070578]
 [-0.38981415  0.60811244 -1.25938424  1.47191593]
 [-2.52214926  2.67882552 -0.67947465  1.48119548]]
dW: [[ 0.07313866 -0.0976715  -0.87585828  0.73763362  0.00785716]
 [ 0.85508818  0.37530413 -0.59912655  0.71278189 -0.58931808]
 [ 0.97913304 -0.24376494 -0.08839671  0.55151192 -0.10290907]]
db: [[-0.58855142]
 [-0.4525262 ]
 [-0.52836405]]
m is:4
The shape of W is: (3, 5)
The shape of b is: (3, 1)
The shape of A_prev is: (5, 4)
The shape of dW is: (3, 5)
The shape of db is: (3, 1)
The shape of dA_prev is: (5, 4)
m is:4
The shape of W is: (3, 5)
The shape of b is: (3, 1)
The shape of A_prev is: (5, 4)
The shape of dW is: (3, 5)
The shape of db is: (3, 1)
The shape of dA_prev is: (5, 4)
m is:4
The shape of W is: (3, 5)
The shape of b is: (3, 1)
The shape of A_prev is: (5, 4)
The shape of dW is: (3, 5)
The shape of db is: (3, 1)
The shape of dA_prev is: (5, 4)
Error: Wrong output for variable 2.
 2  Tests passed
 1  Tests failed

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-23-3427f65cb756> in <module>
      6 print("db: " + str(t_db))
      7 
----> 8 linear_backward_test(linear_backward)

~/work/release/W4A1/public_tests.py in linear_backward_test(target)
    395     ]
    396 
--> 397     multiple_test(test_cases, target)
    398 
    399 

~/work/release/W4A1/test_utils.py in multiple_test(test_cases, target)
    140         print('\033[92m', success," Tests passed")
    141         print('\033[91m', len(test_cases) - success, " Tests failed")
--> 142         raise AssertionError("Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))
    143 

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

Please look again at the mathematical formula for db. Your code does not faithfully implement that formula.

We have to reason from evidence, right? Look at your output values and compare them to the expected ones.

Your db[0] = -0.58855142.

The correct value is db[0] = -0.14713786

Get out your calculator app and divide your value by the correct one and what do you see? Then try that with the other two entries. Notice a pattern?

Thanks for catching that. I fixed the formula it worked.

I did notice db was off.

Great! But then the next step is to ask “How is it wrong? Is there a pattern that might tell me something?” :nerd_face: