3

I'm learning about various loss functions used in Deep learning. I needed some help implementing a custom loss function in tensorflow. To get a concrete picture of this, I would like to implement a custom Binary Cross Entropy loss as an example.

Thanks a lot for your help

Regards

Edit: The following is loss function I have implemented:

def custom_loss(eps):
    def loss(y_true, y_pred):
        ans = -eps*(y_true*tf.log(y_pred) + (1-y_true)*tf.log(y_pred))
        return ans
    return loss

This is returning not a number after sometime. I tried to add a small quantity to the log function. Furthermore, I have changed the optimiser to adam.

1

1 Answer 1

4

I think this is a problem with numerical computation whenever y_pred == 0.

Note that log(0) is undefined so, in order to make our loss calculations numerically stable, we tend to do tf.log(y_pred + epsilon) where epsilon is a very small number that will have a negligible effect on the loss but avoid returning a NaN when trying to divide by zero (or do log(0)).

I assume that this is what you were aiming for with the eps parameter but you ought to put it inside the call to tf.log().

Perhaps something like this:

def custom_loss(eps):
    def loss(y_true, y_pred):
        ans = -(y_true*tf.log(y_pred + eps) + (1-y_true)*tf.log(y_pred + eps))
        return ans
    return loss
4
  • 1
    Yes, you're right about that. I had tried adding a small quantity to the log function. But the loss had dropped to a very small quantity. These results were completely different from what I get by using the inbuilt binary cross entropy loss.
    – haemu
    Commented Jun 18, 2019 at 7:29
  • Your original question (now restored) was specifically about the NaN. On Stack Overflow, it is generally best to ask focused questions and certainly changing the essence of a question after answers have been posted is best avoided. Instead, why not ask a new question (referencing this one if needs be)? It will almost always get more attention anyway and, more importantly, it will leave the original question available for others to benefit from
    – Stewart_R
    Commented Jun 18, 2019 at 7:37
  • IMO, it's fundamentally a different thing to ask "why am I getting NaN errors?" to "Why does my implementation not match the 'out of the box' function?" You now have an answer to your first question so move on to your second one in a new post (and, ideally upvote/accept the answer to the first one if it addresses the original question! :-) )
    – Stewart_R
    Commented Jun 18, 2019 at 7:38
  • 1
    Thanks a lot Stewart R. I was't aware about that. I will put up a new question regarding the current issue.
    – haemu
    Commented Jun 18, 2019 at 11:19

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