C1_W3_Logistic_Regression UNQ_C2 : What's wrong with my implementation?

In the Exercise 2 of this lab, I was asked to implement the compute_cost function for gradient descent, and in the solution I need to calculate the loss function, here’s two different implementation that I have tried:
{image removed by moderator as it contained codes from grader cell and posting on public post thread is against community guidelines}

Content in read rectangle is my code which cause the Exercise 5 with following exception:

When I use the code in green rectangle, everything is just fine, so what’s wrong with my code?

  1. You aren’t supposed to post solutions, a mentor will likely ask you to remove it, or do so for you

  2. Upon casual inspection it appears to me that the two expressions you wrote are not mathematically equivalent, which you could prove or disprove by printing out intermediate values as you iterate on i. If the values are different, look at the constituent terms to see what was omitted in the version that doesn’t pass the test. Let us know what you find.

Thanks for your reply.

  1. Sorry, I already remove some sensitive information relative to the solution and only leave information which is necessary.

  2. After digging into the testing code I finally figure out why, it turns out that the label y are been initialized to y = np.array([0.5] * 8) in public_tests.py, and I assume every label y either has 1 or 0, so it cause the issue. But I remember for the logistics regression the label y is always 0 or 1, why the testing code use these kind of data that not match this rule?

Hi @winsonli

as mentioned by the other mentor kindly remove the codes.

Did you calculate the total cost, which is 1/m of the loss_sum you calculated. and make sure you calculate total loss outside of the loop created for each training example.
See the below image.

Regards
DP

necessary information related to your grade cell would be your error output.
if a mentor wants to have a look at your codes, they will ask for it, until then kindly refrain from posting grade cell codes as it is against Code of Conduct.

Regards
DP

Yes I calculated the total cost. I already figure out why my code can’t pass the test as I mentioned above.

Now my question is, I remember for the logistics regression the label y is always 0 or 1, why the testing code use these kind of data that not match this rule?

Thanks.

This logic is based on this

When you are finished, try testing a few values by calling sigmoid(x) in the cell below.

  • For large positive values of x, the sigmoid should be close to 1, while for large negative values, the sigmoid should be close to 0.
  • Evaluating sigmoid(0) should give you exactly 0.5.

to get the value of y closest to 1, x is 8 and 0 it is 0.5

That’s a good question. I’ll post an issue to the course staff and see what they say.

The other two tests of the regularized cost function do use y as a array of 0’s and 1’s.
image

image

The third test is different.

Hi, any update from the course staff?

It’s the weekend, so not yet.