Week 3 - Exercise 5 Compute the Cost

Hello,

I am getting an error when calculating my cost in exercise 5. I tried both dot product and multiply with sum but still not getting the correct answer. I am not sure what I am doing wrong.

{moderator edit: code removed}

The answer I get 2.079…

I am not sure what I am doing wrong.

There is a mistake in it.
You don’t have to transpose the activation(A).
keep it as it is
change A2.T to A

Hey,

Thank you for responding. I already try that but it gives me:

"ValueError: operands could not be broadcast together with shapes (1,400) (1,3)

In order to multiply, I had to transpose A2.

In your compute_cost(A2, Y) function, before defining the logprobs, add these two print statement and see what is your output of the test case:

print(f"A2 shape:", A2.shape)
print(f"Y shape:", Y.shape)   

My output is:

A2 shape: (1, 3)
Y shape: (1, 3)
A2 shape: (1, 3)
Y shape: (1, 3)
cost = 0.6930587610394646
A2 shape: (1, 5)
Y shape: (1, 5)
A2 shape: (5,)
Y shape: (1, 5)
All tests passed!
1 Like

Thank you Saif,

It is all working now but I am very confused now on how adding print statement fixed the issue. I got the same results as you did.

Another question is, the shape of Y is (1, 400) but inside the compute_cost function, it gives the output you posted. Do you know why these happen?

Thank you!

Also, I took the print functions off, it still works. I am not sure what is going on.

Because the print statement gives the output Y which is used by the test case (it is different from the one you imported earlier).

Maybe you were using the global parameters, not the local ones. Or you were defining the logprobs outside of the compute_cost function. As I see in the screenshot you shared, you are getting the ValueError just after defining the logprobs. If you did this inside the compute_cost function, you get the error after running the test cell.

Ohh I see. I think that clears it up. Thank you Saif!

I believe in your first code you forget to average the cost, I had your result before I notice that.

Hey! I tried your suggestion and it gives me this output
A2 shape: (1, 3)
Y shape: (1, 3)
A2 shape: (1, 3)
Y shape: (1, 3)
cost = 0.6926858869721941
A2 shape: (1, 5)
Y shape: (1, 5)
A2 shape: (5,)
Y shape: (1, 5)

with this assertion error
AssertionError Traceback (most recent call last)
in
3 print("cost = " + str(compute_cost(A2, t_Y)))
4
----> 5 compute_cost_test(compute_cost)

~/work/release/W3A1/public_tests.py in compute_cost_test(target)
131
132 assert type(output_1) == float, “Wrong type. Float expected”
→ 133 assert np.isclose(output_1, expected_output_1), f"Wrong value. Expected: {expected_output_1} got: {output_1}"
134
135 assert type(output_2) == float, “Wrong type. Float expected”

AssertionError: Wrong value. Expected: 0.5447066599017815 got: 1.0493608309109266

Shapes are correct but the value of cost is incorrect. Please double-check how you implementing the below equation:

J = - \frac{1}{m} \sum\limits_{i = 1}^{m} \large{(} \small y^{(i)}\log\left(a^{[2] (i)}\right) + (1-y^{(i)})\log\left(1- a^{[2] (i)}\right) \large{)} \small

1 Like

Yes, I got it. I have implemented one part of this equation as mentioned in the instructions. Now it’s working. Thank you!