Problems
Loading...
1 / 1

Log Loss (Per-Sample)

Metrics & Evaluation
Easy

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)=(yln(p^)+(1y)ln(1p^))L(y, p) = -\bigl(y \cdot \ln(\hat{p}) + (1 - y) \cdot \ln(1 - \hat{p})\bigr)

where:

p^=clip(p,  ε,  1ε)\hat{p} = \text{clip}(p,\; \varepsilon,\; 1 - \varepsilon)

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.

Loading visualization...

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
Try Similar Problems