Machine learning building blocks
Binary cross-entropy
ml.binary-cross-entropyAlso called binary cross entropy, bce, log loss, logloss
Mean negative log-likelihood of binary targets under predicted probabilities p, in nats.
Notation
targets in {0, 1}
y ∈ {0, 1}n
predicted probabilities in (0, 1)
p ∈ ℝn, each entry ∈ (0, 1)
number of entries
Size, at least 1
Result: the mean loss, in nats
scalar
Worked examples
Each result is solved exactly and confirmed by two independent computations.
Given
Result
≈ 0.69314718056
Given
Result
≈ 1.38629436112
Given
Result
≈ 0.379811427729
Common mistakes
Sums instead of averaging
The per-example losses are summed, not averaged over the n examples.
Fix: Divide by n, or use reduction='mean'.
Swaps ln p and ln(1 − p)
Targets of 1 are scored with ln(1 − p) and targets of 0 with ln p.
Fix: Pair y with ln p and (1 − y) with ln(1 − p).
Sources
Goodfellow et al. 2016, §6.2.2.2
Goodfellow, Ian; Bengio, Yoshua; Courville, Aaron. Deep Learning. MIT Press, 2016.
Murphy 2022
Murphy, Kevin P. Probabilistic Machine Learning: An Introduction. MIT Press, 2022.