We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Loss Functions step 6 of 8
Medium
Research
Focal Loss
Implement Focal Loss from “Focal Loss for Dense Object Detection” (Lin et al., 2017).
Focal loss addresses class imbalance by down-weighting easy examples:
$$\text{FL}(p_t) = -\alpha_t (1 - p_t)^\gamma \log(p_t)$$
Where $p_t$ is the model’s predicted probability for the true class.
For binary classification:
- If y=1: $p_t = p$, $\alpha_t = \alpha$
- If y=0: $p_t = 1-p$, $\alpha_t = 1-\alpha$
Given:
-
probs: shape(batch,)— predicted probabilities (after sigmoid) -
targets: shape(batch,)— binary targets (0 or 1) -
alpha: float — class balancing factor -
gamma: float — focusing parameter
Output: Scalar (float) — mean focal loss over the batch.
Loading visualization…
Nothing accepted yet. When a submission passes, the code that passed shows up
here, one entry per mode.
Stuck?
PyTorch reference solution
Sign in to attempt this problem and reveal the reference solution.