Binary Focal Loss
Asked at Datadog· Screen
Reported by the problem's creator, not verified
Focal loss is a weighted form of binary cross-entropy used to train classifiers on imbalanced data. It shrinks the loss of samples the model already classifies confidently and correctly, so training focuses on hard samples.
Implement the function binary_focal_loss(y_true, y_pred, alpha, gamma, reduction).
y_true[i]is the true label of samplei, either0or1.y_pred[i]is the model's predicted probability that sampleihas label1.alphaweights the positive class;1 - alphaweights the negative class.gammais the focusing parameter.reductionis one of"none","sum","mean"and decides how per-sample losses are combined.
Examples
Example 1
Input: y_true =
[1,0], y_pred =[0.9,0.2], alpha =0.25, gamma =2, reduction =meanOutput:
0.003479Explanation: The
y=1sample hasp_t = 0.9,alpha_t = 0.25, loss0.25 * 0.1^2 * -ln(0.9) ≈ 0.000263. They=0sample hasp_t = 0.8,alpha_t = 0.75, loss0.75 * 0.2^2 * -ln(0.8) ≈ 0.006694. Their mean rounds to0.003479.Example 2
Input: y_true =
[1,0,0,1], y_pred =[0.6,0.35,0.9,0.15], alpha =0.4, gamma =1.5, reduction =noneOutput:
[0.051692,0.053519,1.179589,0.59468]Explanation: With
"none"there is one loss per sample in input order, each rounded to 6 decimal places.