The choice between using * (element-wise multiplication) and np.dot (dot product)

Hello, :blush:

In the propagate function, for implementing the cost function we use element-wise:

cost = -np.sum(Y * np.log(A) + (1-Y) * np.log(1-A)) / m

but I couldn’t understand deeply and main reason why we used the element-wise instead of the dot product here between Y and log(A).

Just because the inner dimensions of the Y and A are not the same hence we use element-wise instead of the dot? So if the inner dimensions were the same, we can use dot interchangeably?

I went through this answer but it didn’t help me.

In general, I couldn’t find the main distinction between using the dot product in computing the dw and using element-wise in computing cost. when dot and when element-wise?

could someone help me with this?

Many thanks for considering my question.

Best Regards,
Reza.

Use a dot product when you want to compute the sum of the products of the elements of two vectors. Note that transposing one of the variables may be necessary to get the inner dimensions to match.

Use an element-wise product when you want just the product of the elements - such as when you’re computing with a mask value - that’s essentially what Y and (1 - Y) do in logistic regression. This is a special case because the Y values are only 0 or 1.

Note that in some cases, you can use a dot product to do both.