Binary Cross-Entropy Loss

Binary cross-entropy measures how well a predicted probability matches a binary label.

Let \(p\) be the model's predicted probability that \(y = 1\). For one example, the loss is

\[ L(p,y) = -y\log p - (1-y)\log(1-p). \]

Because \(y\) is either 0 or 1, only one term contributes:

True label Loss What minimizes it?
\(y = 1\) \(-\log p\) Making \(p\) close to 1
\(y = 0\) \(-\log(1-p)\) Making \(p\) close to 0

Understanding \(-\log p\)

When the true label is 1, \(p\) is the probability assigned to the correct class. The negative logarithm gives a small penalty to confident correct predictions and a large penalty to confident wrong predictions.

Plot of negative log p against predicted probability p
The loss approaches zero as the probability assigned to the correct class approaches one, and grows rapidly as that probability approaches zero.

For example, when the true label is 1, predicting \(p = 0.9\) gives a loss of about \(0.105\), while predicting \(p = 0.1\) gives a loss of about \(2.303\).

When the true label is 0, binary cross-entropy uses \(-\log(1-p)\), which follows the same idea with the curve reflected horizontally.

From one example to a dataset

For input \(x_i\), let \(p_\theta(x_i) = P_\theta(Y=1 \mid X=x_i)\) be the probability predicted by a model with parameters \(\theta\). Training usually minimizes the mean binary cross-entropy across a batch of \(N\) examples:

\[ \operatorname{BCE}(\theta) = -\frac{1}{N} \sum_{i=1}^{N} \left[ y_i \log p_\theta(x_i) + (1-y_i)\log\left(1-p_\theta(x_i)\right) \right]. \]

A lower binary cross-entropy means the predicted probabilities align more closely with the true labels. In practice, deep-learning libraries often compute this loss directly from logits for better numerical stability.