(you can use either np.multiply()
and then np.sum()
or directly np.dot()
).
Note that if you use np.multiply
followed by np.sum
the end result will be a type float
, whereas if you use np.dot
, the result will be a 2D numpy array. We can use np.squeeze()
to remove redundant dimensions (in the case of single float, this will be reduced to a zero-dimension array). We can cast the array as a type float
using float()
.
logprobs = np.multiply(np.log(A2),Y)
cost = - np.sum(logprobs)