Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

losses.BinaryCrossentropy

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.

How it works

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.

Syntax

tf.keras.losses.BinaryCrossentropy(
    from_logits=False,
    label_smoothing=0.0,
    axis=-1,
    reduction='sum_over_batch_size',
    name='binary_crossentropy'
)

Parameters

NameTypeDescription
from_logitsboolWhether y_pred contains logits instead of probabilities; defaults to False.
label_smoothingfloatAmount of target smoothing in the range [0, 1]; defaults to 0.0.
axisintAxis used to compute cross-entropy; defaults to -1.
reductionstr or NoneLoss reduction mode: "sum", "sum_over_batch_size", or None.
namestr or NoneOptional name for the loss instance.

Returns

A loss value produced by comparing y_true and y_pred, with the configured axis and reduction applied.

Key points

  • Use this class when training a binary classifier with targets and predictions representing the two binary classes.
  • Each true-label value is expected to represent a binary target.
  • The from_logits option determines whether predictions are treated as unrestricted logits rather than probabilities.
  • axis selects the dimension over which the class-wise cross-entropy is calculated and defaults to -1.
  • label_smoothing=0 leaves targets unchanged; positive values move targets toward 0.5.

Common mistakes

  • Set from_logits=True when predictions are raw logits; otherwise the loss interprets them as probabilities.
  • Choose reduction deliberately; the usual setting is sum_over_batch_size, while None leaves unreduced values.
  • Nonzero label smoothing changes the targets toward 0.5, and larger values soften them more.

Example: Binary cross-entropy from logits

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.

When to use it

Binary classification

A model outputs one probability for each binary decision.

Logit-based training

A model's final layer emits raw scores rather than sigmoid probabilities.

Smoothed targets

Training should reduce confidence in exact 0/1 targets.

Custom reduction

A custom loss pipeline needs control over feature axes or aggregation.

Interview questions

QWhat does BinaryCrossentropy compute?

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.

QHow does label_smoothing affect the loss?

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.

QWhat are the default axis and reduction?

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_crossentropy
  • tf.keras.ops.binary_crossentropy
  • tf.keras.metrics.BinaryCrossentropy
  • tf.keras.losses.Loss

Source: tf.keras.losses.BinaryCrossentropy. Checked 2026-10-15.