Datadog questions

Binary Focal Loss

DatadogPhone screenMedium

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.

Part 1: Compute and reduce focal loss

Given the true labels y_true, predicted probabilities y_pred, weights alpha and gamma, and a reduction mode, compute the focal loss and return it.

First clip every prediction p to the range [1e-15, 1 - 1e-15], so a logarithm never sees 0.

For each sample, using the clipped p:

  • if y = 1: p_t = p and alpha_t = alpha
  • if y = 0: p_t = 1 - p and alpha_t = 1 - alpha
  • the sample's loss is -alpha_t * (1 - p_t) ** gamma * ln(p_t)

Then combine the per-sample losses according to reduction:

  • "none": return the list of losses in input order
  • "sum": return their sum
  • "mean": return their mean

Round every returned number with round(x, 6). For "none", round each loss; for "sum" and "mean", round only the final value.

  1. Example 1
    alpha
    0.25
    gamma
    2
    y_pred
    [0.9,0.2]
    y_true
    [1,0]
    reduction
    "mean"
    Output
    0.003479

    Why: 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
    alpha
    0.4
    gamma
    1.5
    y_pred
    [0.6,0.35,0.9,0.15]
    y_true
    [1,0,0,1]
    reduction
    "none"
    Output
    [0.051692,0.053519,1.179589,0.59468]

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

Constraints

  • 1 <= len(y_true) <= 10^5
  • len(y_pred) == len(y_true)
  • y_true[i] is 0 or 1
  • 0 <= y_pred[i] <= 1
  • 0 <= alpha <= 1
  • 0 <= gamma <= 10
  • reduction is "none", "sum" or "mean"
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question