最近发现有人问Focal loss怎么实现,我这里分享一下代码,可以自行调节,进行少许的改进,比如给label加上weights,也可以直接使用。
class FocalLoss(nn.Layer):
def __init__(self, alpha=0.5, gamma=2, weight=None, ignore_index=255):
super().__init__()
self.alpha = alpha
self.gamma = gamma
# 参数可调
# self.weight = paddle.to_tensor(np.array([1.063, 4.468, 1.021]))
self.ignore_index = ignore_index
self.ce_fn = nn.CrossEntropyLoss(soft_label=False)
def forward(self, preds, labels):
logpt = -self.ce_fn(preds, labels)
pt = paddle.exp(logpt)
loss = -((1 - pt) ** self.gamma) * self.alpha * logpt
return loss