Binary Cross-Entropy Loss

Medium
~15 min
code completion

Binary Cross-Entropy Loss

For binary classification, the standard loss is binary cross-entropy (log loss):

where:

  • is the true label
  • is the predicted probability
  • Intuition:

  • When : penalizes low via
  • When : penalizes high via
  • Your task:

    Implement binary_cross_entropy(y_true, y_pred). Assume all y_pred values are strictly in (0, 1).

    Example Tests

    Mixed predictions, standard case

    Input: {"y_pred":[0.9,0.1,0.8,0.2],"y_true":[1,0,1,0]}

    Expected: 0.16425

    Perfect confidence on correct label

    Input: {"y_pred":[0.5],"y_true":[1]}

    Expected: 0.69315

    All correct with high confidence: low loss

    Input: {"y_pred":[0.99,0.99,0.01,0.01],"y_true":[1,1,0,0]}

    Expected: 0.01005

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.