Search code examples
pythontensorflowkerastensorflow2.0loss-function

Where to use binary Binary Cross-Entropy Loss


I have 2 questions:

  1. is there any source where I can see TensorFlow's implementation of those loss functions? I have searched it on Github, on their website and haven't found formulas according to which those loss functions are implemented.

  2. when I use tf.keras.losses.BinaryCrossentropy(from_logits=True), loss function, what should be target? one hot vector([[0,1]]) or just constant tensor ([1]) ? I observed and noticed that it works on both of them. can you write down the formula?

edit: I have classification such as cats vs dogs.


Solution

  • The y_true and y_pred should be tensors like this:

    import tensorflow as tf
    
    binary_crossentropy = tf.keras.losses.BinaryCrossentropy()
    
    a = tf.convert_to_tensor([1., 1., 0., 1., 0., 0.])
    b = tf.convert_to_tensor([.1, .9, 2., 8., 4., 3.])
    
    binary_crossentropy(a, b)
    
    <tf.Tensor: shape=(), dtype=float32, numpy=8.067944>
    

    from_logits should be True if you have no activation function on your final layer. It also work if bot are one-hot encoded:

    d = tf.convert_to_tensor([[.4, .6], [.8, .2], [.7, .3], [.1, .9], [.2, .8], [.4, .6]])
    c = tf.convert_to_tensor([[.8, .2], [.3, .7], [.4, .6], [.9, .1], [.2, .8], [.7, .3]])
    
    binary_crossentropy(c, d)
    
    <tf.Tensor: shape=(), dtype=float32, numpy=1.0452858>