All problems
Focal Loss (Binary)
Problem
Compute the binary focal loss for a single prediction.
With p_t = prob when target is 1 and 1 - prob otherwise, return -((1 - p_t)^gamma) * log(p_t) rounded to 4 decimal places. Clamp p_t to at least 1e-12 before taking the logarithm.
Hint
The modulating factor (1 - p_t)^gamma down-weights easy examples.
Test cases
case 1
in: [0.9,1,2]
expected: 0.0011
case 2
in: [0.1,1,0]
expected: 2.3026
case 3
in: [0.5,0,2]
expected: 0.1733
case 4
in: [1,1,2]
expected: 0
Starter codePython
def focal_loss(prob, target, gamma):
# Your code here
pass4 test cases · run your code against them in the editor