title: Keras Custom Binary Cross Entropy Loss Function. Get NaN as output for loss
body: <p>I try writing a custom binary cross-entropy loss function. 
  This is my script: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>When I run my script using this loss function, after few iterations, I get NaN as output for loss function. </p>
  
  <p>Then I looked at TensorFlow documentation, I modified the loss function into the following: </p>
  
  <pre><code> t_loss = K.max(y_pred,0)-y_pred * y_true + K.log(1+K.exp((-1)*K.abs(y_pred)))
  </code></pre>
  
  <p>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.</p>
  
  <p>Binary Cross-Entropy: y * log(p) + (1-y) * log(1-p) </p>
  
  <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. </p>
  
  <p>Thank you. </p>