For binary classification with logit and target , evaluating naively can overflow or take . How do you derive the numerically stable form , and what value does it give at ?
Deep learning, Loss functions
Level L3 Intermediate. Length Short. Nature Mat Mathematical.
What's being asked
Whether you can derive the standard binary-cross-entropy-with-logits identity and explain why every exponent stays .
Key idea
Substituting and simplifies binary cross-entropy to . Using when gives , where never overflows.
Steps
- Substitute : and .
- Combine terms: .
- Split by sign of : if , use ; if , factor out to get . Combining both cases gives .
- Evaluate at : (matching without computing ).
Common mistakes
- Applying sigmoid first and then computing binary cross-entropy separately, which loses precision near or and produces NaN when underflows to or rounds to .
- Getting the sign of wrong when simplifying .
How long your answer should be
5–6 lines with the derivation and numerical check.
Last checked on 4 December 2026.
Read this
Previous in Deep learning: A convolutional layer outputs a tensor of shape (batch size, channels, height, width). Over which axes are the mean and variance computed, how many distinct pairs are computed per batch, and how many learnable scale and shift parameters exist (assuming one pair per channel) for (a) batch normalisation, (b) layer normalisation, (c) instance normalisation and (d) group normalisation with groups?
Next in Deep learning: How does focal loss modify cross-entropy for extreme class imbalance, where is the predicted probability for the true class and is the focusing parameter? What is the ratio of the loss on a hard example () to an easy example () for versus , and why does this help?