W2_A2_E-5_Getting wrong values for cost

I am trying to get the propogate function to work and getting this error:

AssertionError: Wrong values for cost. [-1.50118226e-03 -2.03426978e-04 1.54465265e-01 9.99664650e-01] != 2.0424567983978403

dw and db have the expected outputs.

Is the cost function supposed to have any np.dot() or reshape? Mine has only a subtraction.

The cost function is supposed to have an np.log. You also need to do a summation. This can be achieved with np.sum.

Edit: I said something alluding to a harder method but I think this will confuse you more so I remove it.

2 Likes

Hi @ilanc ,
You can check whether you use the right formula for cost function.
Screen Shot 2022-11-07 at 12.08.09

1 Like

You also need an element-wise multiplication (*) in this too.

Hint: if you have S=\sum_{i=1}^{m}a_i \log (b_i), and you have A be a vector \{a_1, a_2, ..., a_m\} and B be a vector \{b_1, b_2, ..., b_m\}, your code should be:

S = np.sum(a*np.log(b))

Hope this helps!

1 Like

Thanks. I was thinking the vectorized cost function is A-Y but that is not the case.
Anyway implementing the cost function J worked, and all tests are passing.

2 Likes

No, that is dZ^{[L]}:

dZ^{[L]} = A - Y

That is the derivative of the loss w.r.t. the linear activation output of the last layer as a vector function. In other words:

dZ^{[L]} = \displaystyle \frac {\partial L}{\partial Z^{[L]}}

Here’s a thread which shows the derivation of that formula and some related derivations.