Binary classification
A model outputs one probability for each binary decision.
TensorFlow · Class
BinaryCrossentropy is a Keras loss class for binary classification. It compares binary targets with predicted probabilities or logits and supports label smoothing, axis selection, and configurable reduction.
BinaryCrossentropy is a callable Keras loss object for binary classification. It compares binary targets with predicted probabilities or logits, optionally smooths targets, reduces values across the selected axis, and applies the configured reduction.
tf.keras.losses.BinaryCrossentropy(
from_logits=False,
label_smoothing=0.0,
axis=-1,
reduction='sum_over_batch_size',
name='binary_crossentropy'
)| Name | Type | Description |
|---|---|---|
from_logits | bool | Whether y_pred contains logits instead of probabilities; defaults to False. |
label_smoothing | float | Amount of target smoothing in the range [0, 1]; defaults to 0.0. |
axis | int | Axis used to compute cross-entropy; defaults to -1. |
reduction | str or None | Loss reduction mode: "sum", "sum_over_batch_size", or None. |
name | str or None | Optional name for the loss instance. |
A loss value produced by comparing y_true and y_pred, with the configured axis and reduction applied.
import tensorflow as tf
loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)
y_true = tf.constant([[0., 1.], [1., 0.]])
logits = tf.constant([[1.2, -0.4], [-0.8, 1.5]])
value = loss(y_true, logits)
print(float(value))Creates a binary cross-entropy loss for logits and evaluates it on two batches of binary targets.
A model outputs one probability for each binary decision.
A model's final layer emits raw scores rather than sigmoid probabilities.
Training should reduce confidence in exact 0/1 targets.
A custom loss pipeline needs control over feature axes or aggregation.
It computes cross-entropy for binary classification, accepting one binary target and one predicted value per output position. Predictions can be probabilities or logits, controlled by from_logits.
Follow-up: Why prefer logits in a model? The loss can apply the numerically appropriate logits-based computation instead of requiring a separate probability transformation.
label_smoothing replaces the exact binary targets with softened targets closer to 0.5. A value of zero disables this behavior, while larger values produce stronger smoothing.
Follow-up: When might smoothing help? It can reduce overconfidence by preventing the target distribution from being completely one-hot.
The default axis is -1, the features axis. The reduction defaults to sum_over_batch_size, which averages the loss over the batch after the per-axis cross-entropy computation.
Follow-up: How can unreduced values be requested? Set reduction=None, which is one of the documented supported reduction options.
tf.keras.losses.binary_crossentropytf.keras.ops.binary_crossentropytf.keras.metrics.BinaryCrossentropytf.keras.losses.Loss