#149BCE — clip the probabilities so log stays finiteEasy

BCE — clip the probabilities so log stays finite

Background

Binary cross-entropy is

LBCE=−1n∑i[yilog⁡y^i+(1−yi)log⁡(1−y^i)].L_{\text{BCE}} = -\frac{1}{n}\sum_i \bigl[y_i\log\hat{y}_i + (1-y_i)\log(1-\hat{y}_i)\bigr].

The catch: log⁡(0)=−∞\log(0) = -\infty. If a model ever predicts exactly 00 or 11, the naive formula returns nan/inf. Real implementations clip y^\hat{y} into [ε,1−ε][\varepsilon, 1-\varepsilon] first so the log stays finite.

Problem statement

Implement clipped_bce(y, y_hat, eps=1e-7) that clips the predictions to [ε,1−ε][\varepsilon, 1-\varepsilon] and returns the mean BCE.

Input

  • y — array-like of labels (0/1).
  • y_hat — array-like of predicted probabilities in [0,1][0, 1] (may include exactly 0 or 1).
  • eps — float, the clip margin (default 1e-7).

Output

Returns a Python float: a finite mean BCE.

Examples

Example 1 — ordinary case

Input:  y = [1, 0], y_hat = [0.9, 0.1]
Output: 0.1053605...

Explanation: −12(log⁡0.9+log⁡0.9)-\tfrac12(\log 0.9 + \log 0.9).

Example 2 — exact 0/1 no longer explodes

Input:  y = [1, 0], y_hat = [1.0, 0.0]
Output: ~1.2e-7  (finite, tiny)

Explanation: clipping to 1−ε1-\varepsilon / ε\varepsilon keeps the log finite instead of nan.

Constraints

  • Clip y_hat to [eps, 1 - eps] before taking logs.
  • Average over the nn examples.
  • Return a finite float.

Notes

  • A confident wrong prediction (e.g. y^=ε\hat{y}=\varepsilon when y=1y=1) still produces a large loss ≈−log⁡ε\approx -\log\varepsilon — the clip bounds it instead of letting it become infinite.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • •Example: ordinary probabilities
  • •Reference: exact 0/1 stays finite and tiny
  • •Sample: confident-wrong single prediction