How broadcasting makes a different in W2 E5

Hi all,

When I tried:

cost = - "sum "(“matmul”( Y, “log”(A) ) + “matmul” (( 1 - Y ), “log” ( 1-A ))) / m

There is a value error says:

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

I tried transposed all Y to Y.T, and make it (3,1)x(1,3) but it gives me wrong answer.

I don’t understand how “keepdims” broadcasting correct this issue.
cost = - "sum "(“matmul”( Y, “log”(A) ) + “matmul” (( 1 - Y ), “log” ( 1-A ))) / m, axis = 1, keepdims = True

Don’t do dot product. It’s element wise multiplication. Use * or tensorflow.math.multiply

This is Course 1, so we don’t know about TensorFlow yet, only numpy. You can use dot product, but you have to get the transpose in the right place. As you saw, transposing the first argument is wrong. Try transposing the second one, then you have 1 x m dotted with m x 1, which gives a 1x 1 output, right?

Then you don’t need the sum. That’s why dot product is better here: it does both multiply and sum in one operation, although np.multiply followed by np.sum gives the same answer.

Note that np.matmul is equivalent to both are “dot product” style matrix multiply. Prof Ng always uses for some reason, but they are interchangeable for our purposes here. Whereas np.multiply and * both do “elementwise” multiply.

I understand that dot product is a sum after multiplication.
It is just being very confusing to tell the difference between matrix multiplication and normal dot with function.

So this “Ylog(A)” part is meant to produce a scalar right? I thought we are doing some matmul operation here.

Yes, please read my previous reply again more carefully. I think I explained everything there. You can also read the documentation for numpy matmul and numpy dot.