Binary crossentropy computes the negative of y times log(p) plus (1 minus y) times log(1 minus p) for each example, where y is the true label, 0 or 1, and p is the predicted probability of the positive class, typically the output of a sigmoid activation. When the true label is 1, only the log(p) term matters, and when it's 0, only the log(1-p) term matters, so in both cases the loss is small when the prediction is confidently correct and large when it's confidently wrong. It's the standard loss for binary classification, and is also used per-class in multi-label classification, where each of several independent labels can be 0 or 1 simultaneously.
1Understanding losses.BinaryCrossentropy()
Binary crossentropy computes the negative of y times log(p) plus (1 minus y) times log(1 minus p) for each example, where y is the true label, 0 or 1, and p is the predicted probability of the positive class, typically the output of a sigmoid activation. When the true label is 1, only the log(p) term matters, and when it's 0, only the log(1-p) term matters, so in both cases the loss is small when the prediction is confidently correct and large when it's confidently wrong. It's the standard loss for binary classification, and is also used per-class in multi-label classification, where each of several independent labels can be 0 or 1 simultaneously.
Binary crossentropy is also the right choice for multi-label classification, where an example can belong to several classes at once, applied independently per class with a sigmoid activation on each output unit — that's different from CategoricalCrossentropy's assumption that exactly one class is correct.
import tensorflow as tf
bce = tf.keras.losses.BinaryCrossentropy()
y_true = [1, 0]
y_pred = [0.9, 0.1]
print(round(bce(y_true, y_pred).numpy(), 4))2Practical Example
Here is a real-world application of losses.BinaryCrossentropy() showing how it is used in production TensorFlow code.
import tensorflow as tf
bce = tf.keras.losses.BinaryCrossentropy()
y_true = [1, 0]
y_pred = [0.1, 0.9]
print(round(bce(y_true, y_pred).numpy(), 4))3Best Practices
Follow these guidelines when working with losses.BinaryCrossentropy():
1. Pair BinaryCrossentropy with a sigmoid activation on a single output unit for standard binary classification
2. Use BinaryCrossentropy, not CategoricalCrossentropy, for multi-label classification, where multiple independent labels can each be 0 or 1 at once
3. Pass from_logits=True if the model's final layer outputs raw logits with no sigmoid activation applied, which is often more numerically stable
Tip: Binary crossentropy is also the right choice for multi-label classification, where an example can belong to several classes at once, applied independently per class with a sigmoid activation on each output unit — that's different from CategoricalCrossentropy's assumption that exactly one class is correct.
import tensorflow as tf
bce = tf.keras.losses.BinaryCrossentropy()
y_true = [1, 0]
y_pred = [0.9, 0.1]
print(round(bce(y_true, y_pred).numpy(), 4))