Skip to content
DeepForgeby svx
All problems
MediumDeep Learningdl-081

Focal Loss (Multiclass)

Problem

Compute the multiclass focal loss for one example: FL = -(1 - p_t)^gamma * log(p_t), where p_t is the stable-softmax probability of the target class.

The signature is multiclass_focal_loss(logits, target, gamma). gamma = 0 recovers plain cross-entropy.

Hint

The modulating factor (1 - p_t)^gamma down-weights easy, well-classified examples.

Test cases

case 1
in: [[1,2,3],2,0]
expected: 0.4076059644
case 2
in: [[1,2,3],2,2]
expected: 0.045677799
case 3
in: [[0,0,0],1,1]
expected: 0.7324081924
case 4
in: [[2,1,0],0,2]
expected: 0.045677799
Starter codePython
import math
def multiclass_focal_loss(logits, target, gamma):
    # Your code here
    pass

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