Binary cross entropy
Quick answer
Binary cross-entropy measures the quality of a probability prediction against a binary label. It rewards high probability assigned to the observed class and penalizes confident mistakes much more than uncertain ones. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Compare confidence penalties
from math import logprint([round(-log(p), 6) for p in [0.9, 0.5, 0.1]])Expected output:
[0.105361, 0.693147, 2.302585]All labels are positive; less probability on the observed class increases loss.
How it works
For y in {0,1} and probability p strictly between 0 and 1, loss = -[y ln(p) + (1-y) ln(1-p)]. For a positive event it reduces to -ln(p); for a negative event it reduces to -ln(1-p). For y=1, p=0.9 gives about 0.105, while p=0.1 gives about 2.303. Average the individual losses over nonempty samples for mean log loss. This is the negative log-likelihood per observation under the Bernoulli conditional model; minimizing it is equivalent to maximizing the corresponding likelihood. The loss on logits can be written as softplus(z)-yz, with softplus(z)=ln(1+exp(z)). A stable form is max(z,0)-yz+log1p(exp(-abs(z))); use finite logits. Its derivative with respect to z is sigmoid(z)-y, so for a feature vector x the weight gradient is (p-y)x. A batch gradient averages those terms. A correctly classified prediction of 0.51 still has a relatively high loss, unlike a confident correct 0.99. A wrong exact probability of 0 or 1 has infinite ideal loss; the scikit-learn 1.8 log_loss implementation clips probabilities using the floating dtype precision. Clipping is an implementation convention, not the mathematical definition. Use a fixed label ordering and provide labels when a split contains just one class.
Evaluate extreme logits safely
from math import exp, log1pdef loss(z,y): return max(z,0)-y*z+log1p(exp(-abs(z)))print([round(loss(z,y), 6) for z,y in [(0,1),(1000,0),(-1000,0)]])Expected output:
[0.693147, 1000.0, 0.0]A confidently wrong positive score costs about 1000, without exponent overflow.
Common mistakes
Do not feed class IDs or logits into log_loss as if they were probabilities. Use natural logs consistently. Never silently omit extreme wrong predictions; use a stable logit-space formula or explicitly document the clipping rule.
Practice and next steps
Compare loss for a negative label with p=0.2 and p=0.8. Explain why equal accuracy does not imply equal log loss. Complete the knowledge check and coding exercise. Next: Multiclass softmax.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Binary cross entropy — knowledge check
Apply binary cross entropy to new examples.
For y=0 and p=0.8, what is the loss?
What is the derivative of binary logit loss with respect to z?
What is the ideal loss for y=1 and p=0?
Why use log1p(exp(-abs(z)))?
Two models have equal accuracy. Must their log loss agree?