Binary Focal Loss

Asked at Datadog· Screen

Reported by the problem's creator, not verified

Import another question

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 sample i, either 0 or 1.
  • y_pred[i] is the model's predicted probability that sample i has label 1.
  • alpha weights the positive class; 1 - alpha weights the negative class.
  • gamma is the focusing parameter.
  • reduction is one of "none", "sum", "mean" and decides how per-sample losses are combined.

Examples

  1. Example 1

    Input: y_true = [1,0], y_pred = [0.9,0.2], alpha = 0.25, gamma = 2, reduction = mean

    Output: 0.003479

    Explanation: The y=1 sample has p_t = 0.9, alpha_t = 0.25, loss 0.25 * 0.1^2 * -ln(0.9) ≈ 0.000263. The y=0 sample has p_t = 0.8, alpha_t = 0.75, loss 0.75 * 0.2^2 * -ln(0.8) ≈ 0.006694. Their mean rounds to 0.003479.

  2. Example 2

    Input: y_true = [1,0,0,1], y_pred = [0.6,0.35,0.9,0.15], alpha = 0.4, gamma = 1.5, reduction = none

    Output: [0.051692,0.053519,1.179589,0.59468]

    Explanation: With "none" there is one loss per sample in input order, each rounded to 6 decimal places.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.