Ex.5, W2, computing cost

Hello,

I got error in computing the cost, and this is the error text


ValueError Traceback (most recent call last)
in
3 X =np.array([[1., 2., -1.], [3., 4., -3.2]])
4 Y = np.array([[1, 0, 1]])
----> 5 grads, cost = propagate(w, b, X, Y)
6
7 assert type(grads[“dw”]) == np.ndarray

in propagate(w, b, X, Y)
30 # YOUR CODE STARTS HERE
31 A = sigmoid((np.dot(w.T,X)) + b)
—> 32 cost = (-1 / m) * ((np.dot(Y, np.log(A))) + (np.dot(1-Y, np.log(1-A))))
33 # YOUR CODE ENDS HERE
34

<array_function internals> in dot(*args, **kwargs)

ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

I suspect that there is a dimensional mismatch, I 've tried many approached and the same sort of error.
Thanks.

You should check the shape of Y and np.log(A) and then remember what is the matrix multiplication rule (see below from Wikipedia):

image

I 'll do so, there is no dimensional analysis in the instruction so I didn’t expect I had to do so.

I 've solved the problem of dimensional analysis (I guess), and I got this error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-23-be03942b69a5> in <module>
      7 assert type(grads["dw"]) == np.ndarray
      8 assert grads["dw"].shape == (2, 1)
----> 9 assert type(grads["db"]) == np.float64
     10 
     11 

AssertionError:

Please help how I can solve this problem,

Thanks.

Hi @MoHassan, that error implies that the type of grads["db"] is wrong, db should be a float, you should check the type of db in the propagate function.

1 Like

I used np.sum for dp and for cost, it got the right value for db and exactly the double desired value for cost:

dw = [[0.99845601]
 [2.39507239]]
db = 0.001455578136784208
cost = 10.604635958182634
    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call last)
    <ipython-input-75-be03942b69a5> in <module>
         14 print ("cost = " + str(cost))
         15 
    ---> 16 propagate_test(propagate)

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

AssertionError: Wrong values for cost. 10.604635958182634 != 5.80154531

I double-checked the dimensional analysis and got the right answer.

Thanks so much.