W2 E5 - dot product vs element-wise multiplication

I’m a little confused by the cost function and also dw. I got the right answer for cost using element-wise multiplication. However, the hint says to use np.dot(). Is that possible?

Similarly, I used np.dot() to calculate dw. Again, I got the right answer, but I’m not sure why using the dot product was the right call.

I’m not clear when each method is applicable based on the formula. Any help is much appreciated!

If the equation says to compute the sum of the products of a matrix and a vector (or two vectors), you can do it in one step using np.dot().

If instead you use an element-wise product, then you have to use np.sum() as a second operation. You’ll also have to account for the difference in the shapes between matrices and vectors.

Ultimately they are mathematically equivalent.

For vector dot product you can get the same results as suggested by TMosh but the equivalence breaks in 2D matrix dot products unless you do a separate add for each vector dot product. To make the code generic over different type of inputs np.dot() is preferred.

Here’s a thread about how to decide between dot product and elementwise multiply.

Here’s another one that is more specific to this question of how to compute the cost.

2 Likes

Thanks, that helped a lot!