hello everyone:
my task is emotion recognition. due to unbalance, try to use focal loss to solve it.
Here is coding as follows:
is that true? Thanks, best wishes
class FocalLoss(nn.Module):
def __init__(self, weight=None, alpha=None,
gamma=2., reduction='none'):
nn.Module.__init__(self)
self.weight = weight
#self.alpha = torch.tensor([alpha,1-alpha]).cuda()
self.alpha = torch.tensor([4.0/47, 15.0/47, 15.0/47, 3.0/47, 1.0/47, 6.0/47, 3.0/47]).cuda()
self.gamma = gamma
self.reduction = reduction
def forward(self, input_tensor, target_tensor):
at = self.alpha
log_prob = F.log_softmax(input_tensor, dim=-1)
prob = torch.exp(log_prob)
loss = F.nll_loss(((1 - prob) ** self.gamma) * log_prob*at, target_tensor, weight=self.weight, reduction = self.reduction)
#self.reduction == 'sum'
loss = loss.sum()
return loss