Binary Focal Loss
DatadogPhone screenMedium
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.
Part 1: Compute and reduce focal loss
Given the true labels y_true, predicted probabilities y_pred, weights alpha and gamma, and a reduction mode, compute the focal loss and return it.
First clip every prediction p to the range [1e-15, 1 - 1e-15], so a logarithm never sees 0.
For each sample, using the clipped p:
- if
y = 1:p_t = pandalpha_t = alpha - if
y = 0:p_t = 1 - pandalpha_t = 1 - alpha - the sample's loss is
-alpha_t * (1 - p_t) ** gamma * ln(p_t)
Then combine the per-sample losses according to reduction:
"none": return the list of losses in input order"sum": return their sum"mean": return their mean
Round every returned number with round(x, 6). For "none", round each loss; for "sum" and "mean", round only the final value.
- Example 1
- alpha
0.25- gamma
2- y_pred
[0.9,0.2]- y_true
[1,0]- reduction
"mean"- Output
0.003479
Why: 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
- alpha
0.4- gamma
1.5- y_pred
[0.6,0.35,0.9,0.15]- y_true
[1,0,0,1]- reduction
"none"- Output
[0.051692,0.053519,1.179589,0.59468]
Why: With
"none"there is one loss per sample in input order, each rounded to 6 decimal places.
Constraints
1 <= len(y_true) <= 10^5len(y_pred) == len(y_true)y_true[i]is0or10 <= y_pred[i] <= 10 <= alpha <= 10 <= gamma <= 10reductionis"none","sum"or"mean"