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

Implement Focal Loss

Loss Functions
Medium

Compute the mean binary focal loss from predicted probabilities:

Li=−(1−pi)γyilog⁡(pi)−piγ(1−yi)log⁡(1−pi)L_i = -(1-p_i)^\gamma y_i\log(p_i) - p_i^\gamma(1-y_i)\log(1-p_i)Li​=−(1−pi​)γyi​log(pi​)−piγ​(1−yi​)log(1−pi​)

Here, pip_ipi​ is the predicted probability for sample iii, yiy_iyi​ is its binary label, and γ\gammaγ is gamma. Clip probabilities only for numerical stability, then return the mean of all LiL_iLi​ values as a Python float.

Loading visualization...

Examples

Input: p = [0.9, 0.2, 0.7, 0.1], y = [1, 0, 1, 0], gamma = 2.0

Output: 0.010783

Explanation: Confident correct predictions receive small weights, so their mean focal loss is low.

Input: p = [0.5, 0.5, 0.5, 0.5], y = [1, 0, 1, 0], gamma = 2.0

Output: 0.173287

Input: p = [0.9, 0.2, 0.7, 0.1], y = [1, 0, 1, 0], gamma = 0.0

Output: 0.197635

Hint 1

Use np.clip(p, 1e-15, 1.0 - 1e-15) before taking logarithms.

Hint 2

Use np.log1p(-p) for the negative-class logarithm.

Hint 3

Average the negative sum of the positive and negative terms.

Requirements

  • Treat p as probabilities without applying sigmoid
  • Apply the focal-loss equation elementwise
  • Return the mean loss as a Python float

Constraints

  • p and y are equal-length nonempty numeric lists
  • Every label in y is 0 or 1
  • Every probability in p is between 0 and 1
  • gamma is between 0 and 5
  • Use NumPy only
Try Similar Problems
Binary Focal LossCross Entropy LossDice LossLabel Smoothing LossKl Divergence

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Implement Focal Loss

Loss Functions
Medium

Compute the mean binary focal loss from predicted probabilities:

Li=−(1−pi)γyilog⁡(pi)−piγ(1−yi)log⁡(1−pi)L_i = -(1-p_i)^\gamma y_i\log(p_i) - p_i^\gamma(1-y_i)\log(1-p_i)Li​=−(1−pi​)γyi​log(pi​)−piγ​(1−yi​)log(1−pi​)

Here, pip_ipi​ is the predicted probability for sample iii, yiy_iyi​ is its binary label, and γ\gammaγ is gamma. Clip probabilities only for numerical stability, then return the mean of all LiL_iLi​ values as a Python float.

Loading visualization...

Examples

Input: p = [0.9, 0.2, 0.7, 0.1], y = [1, 0, 1, 0], gamma = 2.0

Output: 0.010783

Explanation: Confident correct predictions receive small weights, so their mean focal loss is low.

Input: p = [0.5, 0.5, 0.5, 0.5], y = [1, 0, 1, 0], gamma = 2.0

Output: 0.173287

Input: p = [0.9, 0.2, 0.7, 0.1], y = [1, 0, 1, 0], gamma = 0.0

Output: 0.197635

Hint 1

Use np.clip(p, 1e-15, 1.0 - 1e-15) before taking logarithms.

Hint 2

Use np.log1p(-p) for the negative-class logarithm.

Hint 3

Average the negative sum of the positive and negative terms.

Requirements

  • Treat p as probabilities without applying sigmoid
  • Apply the focal-loss equation elementwise
  • Return the mean loss as a Python float

Constraints

  • p and y are equal-length nonempty numeric lists
  • Every label in y is 0 or 1
  • Every probability in p is between 0 and 1
  • gamma is between 0 and 5
  • Use NumPy only
Try Similar Problems
Binary Focal LossCross Entropy LossDice LossLabel Smoothing LossKl Divergence

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: number

You must run your code first.