Difference between BinaryCrossentropy and CategoricalCrossentropy

This post explains difference between BinaryCrossentropy and CategoricalCrossentropy

BinaryCrossentropy

  • BinaryCrossentropy is used for computing loss between true labels and predicted labels
  • Use BinaryCrossentropy only when there are only two label classes. For each example, there should be a single floating-point value per prediction
  • How to calculate BinaryCrossentropy in TensorFlow
    
    import tensorflow as tf
    
    y_true = [[0., 1.], [0., 0.]]
    y_pred = [[0.6, 0.4], [0.4, 0.6]]
    
    bce_loss = tf.keras.losses.BinaryCrossentropy()
    
    bce_loss_value = bce_loss(y_true, y_pred).numpy()
    print(bce_loss_value)
    
    
    

    CategoricalCrossentropy

  • CategoricalCrossentropy is used for computing loss between true labels and predicted labels
  • Use CategoricalCrossentropy only when there are two or more label classes.
  • While using CategoricalCrossentropy labels should be provided in a one_hot representation
  • How to calculate CategoricalCrossentropy in TensorFlow
    
    import tensorflow as tf
    
    y_true = [[0, 1, 0], [0, 0, 1]]
    y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
    
    cce_loss = tf.keras.losses.CategoricalCrossentropy()
    
    cce_loss_value = cce_loss(y_true, y_pred).numpy()
    print(cce_loss_value)