I am writing a custom loss function and I want to take 1 - logarithm of the value calculated. I am writing in keras with tensorflow backend as follows:
nonlabels = list(np.where(predictions<threshold)[1])
loss_fg_nT = 0
probs_nT = tf.gather(probs_fg,nonlabels,axis=3)
for i in range(len(nonlabels)):
probs_temp = tf.reshape(probs_nT[:,:,:,i],[-1])
prob_max = tf.math.reduce_max(probs_temp)
const = tf.constant(0.000001)
prob_max = tf.math.add(prob_max,const)
#prob_max = tf.math.subtract(tf.constant(1.0),prob_max)
val = K.log(prob_max)
loss_fg_nT -= val
loss_fg_nT = loss_fg_nT/(len(nonlabels)+0.000001)
If i remove prob_max = tf.math.subtract(tf.constant(1.0),prob_max)
line the function is working fine but when I take log of (1-prob_max) value it is giving nan
value. I couldn't understand the reason behind this as I am adding a nominal value to avoid taking log 0
as well. Can someone help me correct this error.