# Week 3 Exercise 5 - computing the cost throws an error

I’m trying to compute the cost function, which equals:

J = \frac{1}{m} \sum_{i=1}^{m} ( - a^{[2](i)} \log y^{(i)} - (1 - a^{[2](i)}) \log (1 - y^{(i)})).

My implementation is:

# YOUR CODE STARTS HERE
logprobs = np.multiply(np.log(A2),Y*1) + np.multiply(1 - np.log(A2),(1-Y*1))
print('logprobs', logprobs)
cost = - (1/m) * np.sum(logprobs, axis=1, keepdims=True)
print('cost', cost)
# YOUR CODE ENDS HERE

cost = float(np.squeeze(cost))


However I get an assertion error. What could be the issue?

why do you element-wise multiply Y with 1?

to cast boolean values to ints (and later floats)

to cast the boolean values into integers

this is wrong. check the formula.

1 Like

but Y doesnt contain boolean values?

• a numpy-array (vector) Y that contains your labels (red:0, blue:1).

Thanks. I just checked it and of course it should have been

np.log(1 - A2)


wow. Such a blunt mistake. Thanks for the hint.

1 Like

when I print:

print(Y.dtype)


I get a bool

okey, i would not do explicit casts in python3 if it is not necessary. python 2 required this, but in python3, it only looks strange

1 Like