1. 程式人生 > >multi label loss function

multi label loss function

基本思想還是轉化為多個二分類

https://github.com/keras-team/keras/issues/10371

For the multi-label classification, you can try tanh+hinge with {-1, 1} values in labels like (1, -1, -1, 1).
Or sigmoid + hamming loss with {0, 1} values in labels like (1, 0, 0, 1).
In my case, sigmoid + focal loss with {0, 1} values in labels like (1, 0, 0, 1) worked well.
You can check this paper 

https://arxiv.org/abs/1708.02002.

比如batch為32 sample的,8個多標籤輸出,可以等價看成32*8個sample的二分類問題,自然這32*8個sample正負樣本比很容易不均(如果每個sample只有1,2個標籤的話)。這是focal loss就可以發揮很大的作用了

https://www.kaggle.com/rejpalcz/focalloss-for-keras

class FocalLoss(nn.Module):
    def __init__(self, gamma=2):
        super().__init__()
        self.gamma = gamma
        
    def forward(self, input, target):
        if not (target.size() == input.size()):
            raise ValueError("Target size ({}) must be the same as input size ({})"
                             .format(target.size(), input.size()))

        max_val = (-input).clamp(min=0)
        loss = input - input * target + max_val + \
            ((-max_val).exp() + (-input - max_val).exp()).log()

        invprobs = F.logsigmoid(-input * (target * 2.0 - 1.0))
        loss = (invprobs * self.gamma).exp() * loss
        
        return loss.sum(dim=1).mean()

focal loss參考https://zhuanlan.zhihu.com/p/32423092