sorry to disturb you. My classdatasets have server unbalance. Using classweight to balance data weights, it doenāt work. My task is emotion recognition (multitasks). The main architecture of the model is based on GRU. Could you please give me some suggestion?
Thanks
best wishes
This is my implementation of multi-class focal loss function using only the pytroch loss function ātorch.nn.NLLLoss`ā: in my code, (L-h) is the third dimension which is the length of sequence.
# C is number of classes
# w is the alpha_t in the main paper (should sum up to 1)
# weight_focal is (1-p_t)^gamma in the paper
# prediction is the raw output of model (without sigmoid layer)
loss_nll = nn.NLLLoss(weight=w,ignore_index=-1, reduction='none') # w.shape = [C]
gamma = 2
softmax_pred = nn.Softmax(dim=-1)(prediction) # [B, L-h, C]
logsoftmax_pred = nn.LogSoftmax(dim=-1)(prediction) # [B, L-h, C]
weight_focal = torch.pow( (1.0 - softmax_pred) , gamma) # [B, L-h, C]
# truth_hot = F.one_hot(truth, num_classes = n_classes+1).to(torch.float32)
loss = loss_nll(weight_focal.transpose(1, 2) * logsoftmax_pred.transpose(1, 2) , truth) #[B, L-h]
# transpose is neccessary because the second dimension should be the classes
Hi
Actually, I canāt get the same results with your code and another library that is mentioned here. I donāt know why is this the case but would you please check that?
Thanks
Hi Diego, I tried focal loss for one of my binary classification problems. The validation loss is reduced to 0.05 from 0.3, however, thereās no change in accuracy. Can you check my code implementation below to see if itās correct? Thank you.