Week 2 Exercise 5

Getting an assertion error in exercise 5

AssertionError                            Traceback (most recent call last)
<ipython-input-22-7cd89fb9dcd2> in <module>
     14 print ("cost = " + str(cost))
     15 
---> 16 propagate_test(propagate)

~/work/release/W2A2/public_tests.py in propagate_test(target)
     41     assert np.allclose(grads['dw'], expected_dw), f"Wrong values for grads['dw']. {grads['dw']} != {expected_dw}"
     42     assert np.allclose(grads['db'], expected_db), f"Wrong values for grads['db']. {grads['db']} != {expected_db}"
---> 43     assert np.allclose(cost, expected_cost), f"Wrong values for cost. {cost} != {expected_cost}"
     44     print('\033[92mAll tests passed!')
     45 

AssertionError: Wrong values for cost. 12.519827193591217 != 2.0424567983978403

The code to calculate cost is below

 A = sigmoid(np.dot(w.T,X)+b)
    cost = (-1/m)*(np.sum(Y*np.log(A).T + (1-Y)*np.log(1-A).T))

If you are going to use elementwise multiply (*), then it is a mistake to add the transposes as you did there. But they are required if you compute the cost using the dot product. Try some experiments like this and watch what happens

v = np.array([[1, 2, 3]])
print(f"v.shape = {v.shape}")
print(v * v.T)
print(np.sum(v * v.T))
print(np.dot(v, v.T))

When I run that, here’s what I get:

v.shape = (1, 3)
[[1 2 3]
 [2 4 6]
 [3 6 9]]
36
[[14]]

The results of v * v^T should look familiar: it’s the multiplication table of the numbers from 1 to 3, right? And of course we have:

1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14