Log Loss (Per-Sample)
Log Loss (Per-Sample)
Log loss (binary cross-entropy) measures how far each predicted probability is from the actual label. A confident correct prediction has a loss near zero, while a confident wrong prediction has a very large loss.
Given a list of true binary labels and predicted probabilities, compute the per-sample log loss and return them as a list.
Formula
For each sample:
L(y,p)=−(y⋅ln(p^)+(1−y)⋅ln(1−p^))where:
p^=clip(p,ε,1−ε)Clipping prevents taking the log of zero. That is, bound p to be no smaller than epsilon and no larger than 1 - epsilon. Use a default epsilon of 1e-15.
Return a list of floats, one loss value per sample.
Examples
Input:
y_true = [1, 0, 1] y_pred = [0.9, 0.1, 0.8]
Output:
[0.10536051565782628, 0.10536051565782628, 0.22314355131420976]
High-confidence correct predictions produce small loss values.
Input:
y_true = [1, 0] y_pred = [1.0, 0.0]
Output:
[3.3306690738754696e-16, 3.3306690738754696e-16]
Predictions of exactly 0.0 and 1.0 are clipped to epsilon and 1-epsilon before computing the log.
Hint 1
Think about what happens when p = 0.0 or p = 1.0 and how to handle it before computing the logarithm.
Hint 2
The formula has two terms: one active when y=1, the other when y=0. Make sure both are always computed.
Requirements
- Clip predicted probabilities to the range [epsilon, 1 - epsilon]
- Compute the loss independently for each sample
- Return a list of loss values preserving the original sample order
- Use natural logarithm (base e)
Constraints
- 1 <= len(y_true) == len(y_pred) <= 10000
- y_true[i] is 0 or 1
- 0.0 <= y_pred[i] <= 1.0
- eps = 1e-15
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Log Loss (Per-Sample)
Log Loss (Per-Sample)
Log loss (binary cross-entropy) measures how far each predicted probability is from the actual label. A confident correct prediction has a loss near zero, while a confident wrong prediction has a very large loss.
Given a list of true binary labels and predicted probabilities, compute the per-sample log loss and return them as a list.
Formula
For each sample:
L(y,p)=−(y⋅ln(p^)+(1−y)⋅ln(1−p^))where:
p^=clip(p,ε,1−ε)Clipping prevents taking the log of zero. That is, bound p to be no smaller than epsilon and no larger than 1 - epsilon. Use a default epsilon of 1e-15.
Return a list of floats, one loss value per sample.
Examples
Input:
y_true = [1, 0, 1] y_pred = [0.9, 0.1, 0.8]
Output:
[0.10536051565782628, 0.10536051565782628, 0.22314355131420976]
High-confidence correct predictions produce small loss values.
Input:
y_true = [1, 0] y_pred = [1.0, 0.0]
Output:
[3.3306690738754696e-16, 3.3306690738754696e-16]
Predictions of exactly 0.0 and 1.0 are clipped to epsilon and 1-epsilon before computing the log.
Hint 1
Think about what happens when p = 0.0 or p = 1.0 and how to handle it before computing the logarithm.
Hint 2
The formula has two terms: one active when y=1, the other when y=0. Make sure both are always computed.
Requirements
- Clip predicted probabilities to the range [epsilon, 1 - epsilon]
- Compute the loss independently for each sample
- Return a list of loss values preserving the original sample order
- Use natural logarithm (base e)
Constraints
- 1 <= len(y_true) == len(y_pred) <= 10000
- y_true[i] is 0 or 1
- 0.0 <= y_pred[i] <= 1.0
- eps = 1e-15
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number