Binary Focal Loss
Binary Focal Loss
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.
Algorithm
For each sample, let p be the predicted probability and y be the target (0 or 1):
- Compute the probability assigned to the true class:
- Compute the focal loss for this sample:
- Return the mean focal loss across all samples.
Examples
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.
Hint 1
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.
Hint 2
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.
Requirements
- Compute p_t based on whether the target is 1 or 0
- Apply the focal modulating factor (1 - p_t)^gamma
- Scale by the alpha parameter
- Return the mean loss across all samples
Constraints
- predictions contains values strictly between 0 and 1
- targets contains only 0s and 1s
- predictions and targets have the same length (at least 1)
- alpha > 0, gamma >= 0
- Return a single float (mean loss)
- Time limit: 300 ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Accepts: number
Binary Focal Loss
Binary Focal Loss
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.
Algorithm
For each sample, let p be the predicted probability and y be the target (0 or 1):
- Compute the probability assigned to the true class:
- Compute the focal loss for this sample:
- Return the mean focal loss across all samples.
Examples
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.
Hint 1
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.
Hint 2
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.
Requirements
- Compute p_t based on whether the target is 1 or 0
- Apply the focal modulating factor (1 - p_t)^gamma
- Scale by the alpha parameter
- Return the mean loss across all samples
Constraints
- predictions contains values strictly between 0 and 1
- targets contains only 0s and 1s
- predictions and targets have the same length (at least 1)
- alpha > 0, gamma >= 0
- Return a single float (mean loss)
- Time limit: 300 ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Accepts: number