TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Binary Focal Loss

Loss Functions
Medium

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):

  1. Compute the probability assigned to the true class:
pt=pif y=1p_t = p \quad \text{if } y = 1pt​=pif y=1 pt=1−pif y=0p_t = 1 - p \quad \text{if } y = 0pt​=1−pif y=0
  1. Compute the focal loss for this sample:
FL=−α⋅(1−pt)γ⋅ln⁡(pt)FL = -\alpha \cdot (1 - p_t)^{\gamma} \cdot \ln(p_t)FL=−α⋅(1−pt​)γ⋅ln(pt​)
  1. Return the mean focal loss across all samples.

Return the mean focal loss as a float.

Loading visualization...

Examples

Input: predictions = [0.9], targets = [1], alpha = 1, gamma = 2

Output: 0.001054

Explanation: The factor (1 - 0.9)² strongly reduces the loss of this confident correct prediction.

Input: predictions = [0.1], targets = [1], alpha = 1, gamma = 2

Output: 1.865094

Hint 1

Select p when the target is 1 and one minus p when the target is 0.

Hint 2

Accumulate the focal term with math.log, then divide by the sample count.

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
Focal LossCross Entropy LossHinge LossLog Loss Per SampleDice Loss

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Binary Focal Loss

Loss Functions
Medium

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):

  1. Compute the probability assigned to the true class:
pt=pif y=1p_t = p \quad \text{if } y = 1pt​=pif y=1 pt=1−pif y=0p_t = 1 - p \quad \text{if } y = 0pt​=1−pif y=0
  1. Compute the focal loss for this sample:
FL=−α⋅(1−pt)γ⋅ln⁡(pt)FL = -\alpha \cdot (1 - p_t)^{\gamma} \cdot \ln(p_t)FL=−α⋅(1−pt​)γ⋅ln(pt​)
  1. Return the mean focal loss across all samples.

Return the mean focal loss as a float.

Loading visualization...

Examples

Input: predictions = [0.9], targets = [1], alpha = 1, gamma = 2

Output: 0.001054

Explanation: The factor (1 - 0.9)² strongly reduces the loss of this confident correct prediction.

Input: predictions = [0.1], targets = [1], alpha = 1, gamma = 2

Output: 1.865094

Hint 1

Select p when the target is 1 and one minus p when the target is 0.

Hint 2

Accumulate the focal term with math.log, then divide by the sample count.

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
Focal LossCross Entropy LossHinge LossLog Loss Per SampleDice Loss

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.