Binary focal loss addresses the class imbalance problem in binary classification by down-weighting the loss contribution from easy (well-classified) examples. This allows the model to focus training on hard, misclassified examples. It was introduced in the RetinaNet paper for object detection.
Given predicted probabilities, binary targets, a balancing factor alpha, and a focusing parameter gamma, compute the mean binary focal loss.
For each sample, let p be the predicted probability and y be the target (0 or 1):
Input:
predictions = [0.9], targets = [1], alpha = 1.0, gamma = 2.0
Output:
0.000263
p_t = 0.9 (correct with high confidence). The factor (1 - 0.9)^2 = 0.01 strongly down-weights this easy example, producing a very small loss.
Input:
predictions = [0.1], targets = [1], alpha = 1.0, gamma = 2.0
Output:
1.8631
p_t = 0.1 (wrong prediction). The factor (1 - 0.1)^2 = 0.81 keeps most of the loss, so the model learns from this hard example.
For each sample, first determine p_t: if the target is 1, p_t = p; if the target is 0, p_t = 1 - p. Then apply the formula with the given alpha and gamma.
Use math.log() for the natural logarithm. The (1 - p_t)^gamma factor is what makes focal loss different from standard cross-entropy. Sum the per-sample losses and divide by the number of samples.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Accepts: number
Binary focal loss addresses the class imbalance problem in binary classification by down-weighting the loss contribution from easy (well-classified) examples. This allows the model to focus training on hard, misclassified examples. It was introduced in the RetinaNet paper for object detection.
Given predicted probabilities, binary targets, a balancing factor alpha, and a focusing parameter gamma, compute the mean binary focal loss.
For each sample, let p be the predicted probability and y be the target (0 or 1):
Input:
predictions = [0.9], targets = [1], alpha = 1.0, gamma = 2.0
Output:
0.000263
p_t = 0.9 (correct with high confidence). The factor (1 - 0.9)^2 = 0.01 strongly down-weights this easy example, producing a very small loss.
Input:
predictions = [0.1], targets = [1], alpha = 1.0, gamma = 2.0
Output:
1.8631
p_t = 0.1 (wrong prediction). The factor (1 - 0.1)^2 = 0.81 keeps most of the loss, so the model learns from this hard example.
For each sample, first determine p_t: if the target is 1, p_t = p; if the target is 0, p_t = 1 - p. Then apply the formula with the given alpha and gamma.
Use math.log() for the natural logarithm. The (1 - p_t)^gamma factor is what makes focal loss different from standard cross-entropy. Sum the per-sample losses and divide by the number of samples.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Accepts: number