Binary Cross-Entropy Loss
Easy
~12 min
code completion
Binary Cross-Entropy Loss
Binary cross-entropy (BCE) is the standard loss function for binary classification. For a single prediction and label :
For a batch of examples, take the mean:
Numerical stability: Raw model outputs can saturate to exactly 0 or 1, making . Clip predictions to where before computing the log.
Your task:
Implement binary_cross_entropy(y_true, y_pred) that returns the mean BCE loss over the batch.
Example Tests
Mixed correct predictions
Input: {"y_pred":[0.9,0.1,0.8],"y_true":[1,0,1]}
Expected: 0.14462
High-confidence correct predictions: low loss
Input: {"y_pred":[0.9,0.9,0.9],"y_true":[1,1,1]}
Expected: 0.10536
Completely wrong predictions: high loss
Input: {"y_pred":[0.1,0.9],"y_true":[1,0]}
Expected: 2.30259