Binary Cross-Entropy Loss
Problem Statement
Compute the Binary Cross-Entropy (BCE) loss for a single sample.
Background
Binary Cross-Entropy measures the difference between predicted probabilities and actual binary labels:
Where:
- is the true label (0 or 1)
- (y-hat) is the predicted probability
- is natural logarithm
To avoid log(0), which is undefined, keep predictions within with before taking logs (simply adding would break when ).
Your Task
Write a function bce_loss(y_true, y_pred) that computes the BCE loss for a single sample.
Output Format
Return a float rounded to 4 decimal places.
Example
y_true=1, y_pred=0.9
0.1054
- Inputs:
y_true=1is the true label (the positive class);y_pred=0.9is the predicted probability that the label is 1, so the model implicitly gives to label 0. - Because , the second term is weighted by and vanishes: .
- , slightly negative because is just below 1. Negating gives a positive loss of , which rounds to 4 decimal places as
0.1054. - Only the probability placed on the correct label matters. With , the surviving term is , the same ; a coin-flip guess costs .
- Notice the loss grows without bound as the probability on the correct label shrinks (, already costs ); the bound keeps a prediction of exactly 0 or 1 from producing .
Constraints
- y_true is 0 or 1
- 0 <= y_pred <= 1