All problems
Focal Loss
Problem
Compute the mean binary focal loss: -alpha_t * (1 - p_t)^gamma * log(p_t), where p_t = p for y = 1 else 1 - p, and alpha_t = alpha for y = 1 else 1 - alpha.
Probabilities are clipped to [1e-15, 1 - 1e-15]. Empty input returns 0.0.
Hint
The (1 - p_t)^gamma factor down-weights easy, well-classified examples.
Test cases
case 1
in: [[1,0],[0.9,0.9],2,0.25]
expected: 0.6995419226415137
case 2
in: [[1,1],[0.6,0.6],0,0.5]
expected: 0.25541281188299536
case 3
in: [[0,1],[0.3,0.3],1,0.5]
expected: 0.23744586155244374
case 4
in: [[],[],2,0.25]
expected: 0
Starter codePython
import math
def focal_loss(y_true, y_pred, gamma, alpha):
# Your code here
pass4 test cases · run your code against them in the editor