Manual Calculation of Binary Cross Entropy with logits

Hi Arif!

You need to use the natural logarithm (log-base-e) in your
cross-entropy formula. You are using log-base-2. Remove
the second argument in your calls to python’s math.log().
Thus:

    p1=y*(math.log(sigmoid(x)))
    p0=(1-y)*math.log(1-sigmoid(x))

As a side note, calculating log() and sigmoid() separately
can amplify numerical errors. You can avoid this by using
something like pytorch’s logsigmoid() that uses a better
algorithm internally.

Good luck!

K. Frank

2 Likes