All problems
Focal Loss Gradient
Problem
For sigmoid focal loss with focusing parameter gamma, the gradient with respect to the logit z is:
y = 1: (1 - p)^gamma * (gamma * p * log(p) - (1 - p)) y = 0: p^gamma * (p - gamma * (1 - p) * log(1 - p))
where p = sigmoid(z). Given logits, targets, and gamma, return the gradient list.
Hint
Differentiate -(1-p)^gamma log p through the sigmoid using dp/dz = p(1-p).
Test cases
case 1
in: [[0,0],[1,0],2]
expected: [-0.29828679513998635,0.29828679513998635]
case 2
in: [[1,-1],[1,0],0]
expected: [-0.2689414213699951,0.2689414213699951]
case 3
in: [[2,-2],[0,1],1]
expected: [0.9991172903489263,-0.9991172903489264]
Starter codePython
def focal_loss_gradient(logits, targets, gamma):
# Your code here
pass3 test cases · run your code against them in the editor