ml-glossary icon indicating copy to clipboard operation
ml-glossary copied to clipboard

The cost function code return opposite sign

Open SendToSpace opened this issue 4 years ago • 5 comments

#Take the error when label=1 class1_cost = -labels*np.log(predictions)

#Take the error when label=0
class2_cost = (1-labels)*np.log(1-predictions)

#Take the sum of both costs
cost = class1_cost + class2_cost

In this code, it seem like class1 return positive cost and class2 return negative cost, wouldn't they cancel when added?

SendToSpace avatar Sep 13 '19 05:09 SendToSpace

If the labels can only be 1 or 0, one of the costs will always be 0.

-0 * anything = 0

Or

1-1 * anything = 0

iandanforth avatar Sep 13 '19 14:09 iandanforth

What I mean was, when label is 0, (1-0)log(1-p) will give negative cost, should be (label-1)log(1-p), for positive cost, or I could be missing a point here

SendToSpace avatar Sep 16 '19 05:09 SendToSpace

@SendToSpace You're correct. The - sign got moved inside the parens in the equation just above the code as well.

iandanforth avatar Sep 16 '19 14:09 iandanforth

Nice catch if you have time for a pr that would be awesome!

bfortuner avatar Jan 24 '20 20:01 bfortuner

Hey @bfortuner, Can I make a PR for this? I'm just a beginner wanted to start contributing ot Open Source.

Chandu-4444 avatar Jun 07 '21 06:06 Chandu-4444