本文主要是介绍Focal Loss实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、序言
Focal Loss是分类常用到的损失函数,面试中也经常容易考,所以这里记录一下。
二、公式和代码
公式:
Focal Loss = -alpha*(1-pt)**gamma*log(pt)
其中alpha用来调节类别间的平衡,gamma用来平衡难例和简单例的平衡。
代码:
import torch
import torch.nn as nn
import torch.nn.functional as Fclass FocalLoss(nn.Module):def __init__(self, alpha=None, gamma=2.0, reduction="mean"):super(FocalLoss, self).__init__()self.gamma = gammaself.reduction = reductionif alpha == None:self.alpha = torch.tensor([1.0])else:self.alpha = torch.tensor(alpha)def forward(self, predict, label):bce_loss = F.cross_entropy(predict, label, reduction="none")pt = torch.exp(-bce_loss)alpha = self.alpha.gather(0, label.data.view(-1))loss = alpha * (1 - pt) ** self.gamma * bce_lossreturn loss.mean()if __name__ == "__main__":focal_loss = FocalLoss(alpha=[0.25, 0.5, 1.0], gamma=2.0)predict = torch.randn(10, 3, requires_grad=True)label = torch.empty(10, dtype=torch.long).random_(3)loss = focal_loss(predict, label)print("loss:", loss.item())
这篇关于Focal Loss实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!