Skip to content
DeepForgeby svx
All problems
MediumComputer Visioncv-324

Alpha-Balanced Focal Loss

Problem

Compute the alpha-balanced binary focal loss for a single prediction.

With p_t = prob when target is 1 and 1 - prob otherwise, and alpha_t = alpha for positives and 1 - alpha for negatives, the loss is -alpha_t * (1 - p_t)^gamma * log(p_t). Clamp p_t to at least 1e-12 and round to 4 decimal places.

Test cases

case 1
in: [0.9,1,2,0.25]
expected: 0.0003
case 2
in: [0.5,0,2,0.25]
expected: 0.13
case 3
in: [0.1,1,0,0.75]
expected: 1.7269
case 4
in: [1,1,2,0.25]
expected: 0
Starter codePython
def focal_loss_alpha(prob, target, gamma, alpha):
    # Your code here
    pass

4 test cases · run your code against them in the editor