I am wanting to make print statements "showing my working out" of Binary Cross Entropy loss function, that works with scalar inputs, not arrays.

import math as m
import numpy as np

y_ans = 1
y_pred = 1

print("Cost(y,y^) = -yln(y^) - (1-y)-y^ln(1-y^)")
print('Cost('+str(y_ans)+', '+str(y_pred)+') = -'+str(y_ans)+'ln('+str(y_pred)+') -('+str(1-y_ans)+')ln('+str(1-y_pred)+')')
#ans = round((-y_ans*m.log(y_pred)) - ((1-y_ans)*m.log(1-y_pred)), 2)
ans = round((-y_ans*np.log(y_pred)) - ((1-y_ans)*np.log(1-y_pred)), 2)
print('Cost('+str(y_ans)+', '+str(y_pred)+') = '+str(ans))

The answer should be 0, but I get NaN

Cost(y,y^) = -yln(y^) - (1-y)-y^ln(1-y^)
Cost(1, 1) = -1ln(1) -(0)ln(0)
/home/runner/MalwareAnalysis/BinaryCrossEntropy.py:10: RuntimeWarning: divide by zero encountered in log
  ans = round((-y_ans*np.log(y_pred)) - ((1-y_ans)*np.log(1-y_pred)), 2)
/home/runner/MalwareAnalysis/BinaryCrossEntropy.py:10: RuntimeWarning: invalid value encountered in double_scalars
  ans = round((-y_ans*np.log(y_pred)) - ((1-y_ans)*np.log(1-y_pred)), 2)
Cost(1, 1) = nan

I'm fairly new to Python.

  • $\begingroup$ np.log(1-y_pred) will try to take the log of 0, which is not defined. Using a value for y_pred that is lower than 1 works perfectly fine. $\endgroup$
    – Oxbowerce
    Apr 11 at 11:53
  • $\begingroup$ Hi, sorry I'm not sure as to what the solution is from what you are saying. I have appended outputs to the 2 halves to see if that helps anyone answer $\endgroup$ Apr 11 at 12:15
  • $\begingroup$ The issue is that the second part of the cost function (((1-y_ans)*np.log(1-y_pred))) will return a nan value since np.log(1-y_pred) tries to take the log of zero, which is undefined. You can use something like numpy.nan_to_num to automatically convert any nan values to zero. $\endgroup$
    – Oxbowerce
    Apr 11 at 13:15
  • $\begingroup$ Tysm :) Working now $\endgroup$ Apr 11 at 15:54

np.nan_to_num() does the trick, as @Oxbowerce pointed out.

import numpy as np

Not the answer you're looking for? Browse other questions tagged or ask your own question.