pytorch用于多标签分类的bceloss

def bceloss(output, target):
    positive_prob = F.logsigmoid(output)
    negative_prob = F.logsigmoid(-output)
    loss = -positive_prob*target-negative_prob*(1-target)
    loss = loss.mean()
    return loss
    
class bceloss(nn.Module):
    def __init__(self):
        super(bceloss, self).__init__()
        
    def forward(self, pred, target):
        positive_prob = F.logsigmoid(pred)
        negative_prob = F.logsigmoid(-1 * pred)
        loss = -positive_prob*target-negative_prob*(1-target)
        loss = loss.mean()
        return loss

你可能感兴趣的:(人工智能,机器学习,深度学习)