multi label loss function

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjucor/article/details/84259969

基本思想还是转化为多个二分类

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

猜你喜欢

转载自blog.csdn.net/zjucor/article/details/84259969
今日推荐