I'm new, need help in exercise 5 of week 2

Getting the error while coding cost function
cost = -1 * np.sum(np.dot(Y,np.log(A)) + np.dot((1-Y),np.log(1-A))) / m


I request a mentor to provide steps to do, as I couldn’t find them

Hi @ maks, welcome to the Specialization. The first thing I notice, is that your cost function contains an unnecessary np.sum function. That is because you are using np.dot to multiply the vectors containing A and Y. Which is good! But that means the sum is automatically taken due to those operations. So, you can drop the np.sum operation. It was simply summing a scalar, which really isn’t a sum, but NumPy didn’t care.

That said, the vectors in your dot products are not conforming to the rules of vector/matrix multiplication. The column dimension of the first matrix/vector in the product, must equal the row dimension in the second. The ValueError is (cryptically!) telling you as much. If you transpose the second terms in the dot products, you should be good to go. Note: if M is an NumPy array, then its transpose is M.T.

2 Likes

Got it!
Thank you @kenb