PIXELBANKv9.1.0
Menu

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:

L=βˆ’(yβ‹…log⁑(y^)+(1βˆ’y)β‹…log⁑(1βˆ’y^))L = -(y \cdot \log(\hat{y}) + (1 - y) \cdot \log(1 - \hat{y}))

Where:

  • yy is the true label (0 or 1)
  • y^\hat{y} (y-hat) is the predicted probability
  • log⁑\log is natural logarithm

To avoid log(0) which is undefined, we add a small epsilon (1e-15) to predictions.

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:

Input:
y_true=1, y_pred=0.9
Output:
0.1054
Reasoning:

L = -(1 Γ— log(0.9) + 0 Γ— log(0.1)) = -log(0.9) β‰ˆ 0.1054

Constraints:

  • y_true is 0 or 1
  • 0 <= y_pred <= 1
πŸ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Binary Cross-Entropy Loss - Easy | PixelBank