Wk 2 Cost function in propagate uses np.trace, not np.sum--why?

If you had to use np.trace, I think that indicates there is something wrong in your code. For starters, if you are using a dot product to compute the cost, the result should be 1 x 1, not n x n for some value of n > 1, right? And if you compute the cost using the dot product correctly, no sum is required. The point of using the dot product is that it combines both operations (product and sum) into one operation.

We’re not supposed to write the code for you, so let’s talk about some general examples. Suppose I have a vector v that is 1 x 4 and I want to compute the sum of the squares of the elements of v. I can think of several ways to do that using numpy functions. Try running the following and watch what happens:

v = np.array([[1, 2, 3, 4]])
print(np.sum(v * v))
print(np.sum(v ** 2))
print(np.sum(np.power(v, 2)))
print(np.dot(v, v.T))
print(np.dot(v.T, v))

Here’s what that generates:

(1, 4)
[[ 1  2  3  4]
 [ 2  4  6  8]
 [ 3  6  9 12]
 [ 4  8 12 16]]

So you can get the right answer using np.trace, but only because you are compensating for a previous error.