Mean Binary Focal Loss
Problem statement
Compute the mean binary focal loss for a batch. probabilities[i] is the predicted probability of class 1, and labels[i] is the true class.
For each example, choose pt = p and weight = alpha when its label is 1. When the label is 0, choose pt = 1 - p and weight = 1 - alpha. Its loss is:
loss = -weight × (1 - pt)^gamma × ln(pt)
Return the sum of these losses divided by the number of examples, including examples whose class weight is zero. Here ln is the natural logarithm, and a positive number raised to power zero is one. For an empty batch, return 0. The supplied values are probabilities, rather than logits; do not apply a sigmoid.
Function
binaryFocalLoss(probabilities: double[], labels: int[], alpha: double, gamma: double) → doubleExamples
Example 1
probabilities = [0.5,0.5]labels = [1,0]alpha = 0.25gamma = 2return = 0.08664339756999316Both true-class probabilities are 0.5. Their class weights are 0.25 and 0.75, and the focusing factor is 0.25. The two losses sum to 0.25 × ln(2); dividing by two gives 0.125 × ln(2), about 0.08664339757.
Example 2
probabilities = [0.8,0.2]labels = [1,0]alpha = 0.5gamma = 0return = 0.11157177565710488Both true-class probabilities are 0.8. With gamma = 0, the focusing factor is one. Each example has loss -0.5 × ln(0.8), so their mean is about 0.11157177566.
Example 3
probabilities = []labels = []alpha = 0.25gamma = 2return = 0.0The practice empty-batch rule returns zero without dividing by the batch size.
Constraints
0 <= probabilities.length <= 200probabilities.length == labels.length0.000001 <= probabilities[i] <= 0.999999- Every label is the integer
0or1. 0 <= alpha <= 10 <= gamma <= 5- All numeric inputs are finite.