Cant get Week3 Practice Lab cost function to work

I cant see what is wrong with my compute_cost function.

{code removed by mentor}

The test fails with the cost being double what it should be.
The sigmoid function tests correctly.

{code removed by mentor}

Hello @kirsten_greed math.exp() function only works for scalars, So for vectors use np.exp() instead.

Hi @kirsten_greed

I Thinks that total cost should be =1/(2*m) …


1 Like

The initial test passes now. Thankyou.
I feel like I am misled by this guidance.

Ok I thinks That you should firstly declare total_cost variable before the loop over m and give it initial value = 0 , and after that in the line total_cost = (1 / m) * loss_sum you should first change that to total_cost += loss_sum =to comupte cost over all training set(m) and before return you should divide total cost with m like total_cost /= m


Hello @ritik5
I am not using math.exp() just math.e which is a constant
The sigmoid function passes the vector test.

Thank you I see now that I had total_cost at the wrong indent.
But fixing it has not helped.

{code removed by mentor}

Doing it exacly as you prescribe does not help either

{code removed by mentor}

I mean that total_cost += loss_sum in the indentation of the loop over m

Thanks , but no joy with this either

{code removed by mentor}

Hi @kirsten_greed,

You have 2 loops here, and I want to focus on the second one. You are including 6 lines in the 2nd loop. Now, I am not saying you shouldn’t have the 2nd loop, but why do you have the 2nd loop and why do you include each of those 6 lines in it? Some of them shouldn’t be in the 2nd loop.

This image that you quoted will only be talking about the 1st loop, but not the 2nd one.


PS1: it’s against the community’s guideline to share assignment code publicly, so I am removing them.

PS2: I am moving your topic to MLS Course 1. You posted it in Course 3.

@kirsten_greed, please do not post your code on the forum. That isn’t allowed by the community standards.

Apologies and Thank you.
I have it working now.

Great work @kirsten_greed! :wink:

1 Like

For me the key was all in the indenting.
The inner most loop is only 1 statement deep.