🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

losses.BinaryCrossentropy()

AI & DATA SCIENCE // losses-binarycrossentropy

tf.keras.losses.BinaryCrossentropy() measures the difference between a predicted probability and a true 0/1 label, the standard loss for binary classification.

Syntax

tf.keras.losses.BinaryCrossentropy()

Deep Dive Course

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.

editor.html
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))
localhost:3000

2Practical Example

Here is a real-world application of losses.BinaryCrossentropy() showing how it is used in production TensorFlow code.

editor.html
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))
localhost:3000

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.

editor.html
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))
localhost:3000

Examples

Example 01Basic Usage
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))
Example 02Advanced Example
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))

Best Practices

  • Pair BinaryCrossentropy with a sigmoid activation on a single output unit for standard binary classification
  • Use BinaryCrossentropy, not CategoricalCrossentropy, for multi-label classification, where multiple independent labels can each be 0 or 1 at once
  • Pass from_logits=True if the model's final layer outputs raw logits with no sigmoid activation applied, which is often more numerically stable

Interview Question

Why does BinaryCrossentropy assign a very large loss value when a model confidently predicts the wrong class, rather than a small, bounded penalty?

Hint: Think about what happens to the negative log of p as the predicted probability p for the correct answer approaches 0.

As the predicted probability assigned to the correct answer approaches 0, meaning the model is highly confident in the wrong answer, the negative log of that probability grows without bound, approaching infinity, rather than leveling off at some fixed maximum penalty. This unbounded growth is a deliberate design property, not an accident — it creates an extremely strong gradient signal specifically when the model is confidently wrong, pushing its parameters to correct that mistake much more aggressively than a bounded penalty function would. A loss that capped out at some fixed maximum value regardless of how confidently wrong a prediction was would provide a much weaker training signal for exactly the mistakes that matter most to fix, which is why cross-entropy's unbounded penalty for confident wrongness is considered a feature, not a drawback, of the loss function.

Exercises

MediumPractice using losses.BinaryCrossentropy() in a real scenario.
View Solution
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))

Frequently Asked Questions

Why does BinaryCrossentropy assign a very large loss value when a model confidently predicts the wrong class, rather than a small, bounded penalty?

As the predicted probability assigned to the correct answer approaches 0, meaning the model is highly confident in the wrong answer, the negative log of that probability grows without bound, approaching infinity, rather than leveling off at some fixed maximum penalty. This unbounded growth is a deliberate design property, not an accident — it creates an extremely strong gradient signal specifically when the model is confidently wrong, pushing its parameters to correct that mistake much more aggressively than a bounded penalty function would. A loss that capped out at some fixed maximum value regardless of how confidently wrong a prediction was would provide a much weaker training signal for exactly the mistakes that matter most to fix, which is why cross-entropy's unbounded penalty for confident wrongness is considered a feature, not a drawback, of the loss function.

Related Functions

losses-categoricalcrossentropymetrics-aucmodel-compile