Binary Cross-Entropy Loss

MediumMachine LearningMath

Description

Given binary labels yTrue (0 or 1) and predicted probabilities yPred (each strictly between 0 and 1), return the binary cross-entropy loss using natural log: -mean(y·ln(p) + (1-y)·ln(1-p)). Round to 4 decimal places.

Examples

Input:yTrue = [1,0,1], yPred = [0.9,0.1,0.8]
Output:0.1446
Explanation:

Averaging the negative log-likelihood across the samples gives a loss of 0.1446.

Input:yTrue = [1,1], yPred = [0.5,0.5]
Output:0.6931
Explanation:

Averaging the negative log-likelihood across the samples gives a loss of 0.6931.

Input:yTrue = [0,1], yPred = [0.2,0.7]
Output:0.2899
Explanation:

Averaging the negative log-likelihood across the samples gives a loss of 0.2899.

Constraints

  • 1 ≤ yTrue.length ≤ 10⁴
  • 0 < yPred[i] < 1

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.