Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-338

Mean Binary Cross-Entropy

Problem

Compute the mean binary cross-entropy loss in nats for predicted probabilities and binary targets. Each probability is clamped to [1e-12, 1 - 1e-12] so the logarithms stay finite. Return 0.0 for empty input.

Hint

Average -(y ln p + (1-y) ln(1-p)) over the pairs.

Test cases

case 1
in: [[0.9,0.2],[1,0]]
expected: 0.164252033486018
case 2
in: [[0.5,0.5],[1,0]]
expected: 0.6931471805599453
case 3
in: [[0.5],[1]]
expected: 0.6931471805599453
case 4
in: [[],[]]
expected: 0
case 5
in: [[0.99,0.01],[1,0]]
expected: 0.01005033585350145
Starter codePython
import math
def mean_binary_cross_entropy(preds, targets):
    # Your code here
    pass

5 test cases · run your code against them in the editor