Week 2: Assignment 4.3 Exercise 5

I’m using the below formula to calculate ‘cost’ without using np.sum

cost = -(np.dot(Y, np.log(A).T) + np.dot((1-Y), np.log(1-A).T))/m

and to calculate ‘db’ using the formula:
db = np.sum(A-Y)/m

Whether I use np.sum in the ‘cost’ function or not my all tests are passed but if I don’t use np.sum in db function it throws an error as ‘assert type(grads[“db”]) == np.float64’

I want to know why db can’t be used without np.sum and if it is necessary then how cost function is not giving an error?

1 Like

Hi @shsrivas

This is one of the lines in the test cases, which you also pointed out, assert type(grads["db"]) == np.float64

Over here you can see that the ‘type’ of db is being checked to make sure it is np.float64. But there’s no check on the ‘type’ of cost. I’m guessing using np.sum changes the the type to float64. And since there’s no check on the type of ‘cost’, it doesn’t matter what it ends up being.

I hope this answers your question.

1 Like

Thanks for the clarification!
In both cases ‘cost’ is returning an ‘ndarray’ while ‘db’ returns ‘ndarray’ if np.sum is not used and ‘float64’ if np.sum is used.

1 Like