I try writing a custom binary cross-entropy loss function. This is my script:
def my_custom_loss(y_true,y_pred):
t_loss = (-1)*(y_true * K.log(y_pred) + (1 - y_true) * K.log(1 - y_pred))
return K.mean(t_loss)
When I run my script using this loss function, after few iterations, I get NaN as output for loss function.
Then I looked at TensorFlow documentation, I modified the loss function into the following:
t_loss = K.max(y_pred,0)-y_pred * y_true + K.log(1+K.exp((-1)*K.abs(y_pred)))
The code runs without any issue. I would like to know if someone could provide some explanation why my first loss function gives a NaN output.
Binary Cross-Entropy: y * log(p) + (1-y) * log(1-p)
I have sigmoid function as activation for my last layer. So the value of 'p' should be between 0 and 1. Log should exist for this range.
Thank you.