当前位置:   article > 正文

BCEWithLogitsLoss= BCELoss + Sigmoid_bcewithlogitsloss公式

bcewithlogitsloss公式

CE

nn.CrossEntropyLoss()

交叉熵描述了两个概率分布之间的距离,当交叉熵越小说明二者之间越接近。该损失函数结合了nn.LogSoftmax()和nn.NLLLoss()两个函数。它在做分类训练的时候是非常有用的。

BCELoss

b这里指的是binary,这是个二分类交叉熵损失,公式如下所示在这里插入图片描述

BCEWithLogitsLoss

BCEWithLogitsLoss就是把Sigmoid-BCELoss合成一步
BCELoss(sigmoid(pre),target)==BCEWithLogitsLoss(pre,target)
简单说就是在BCELoss加一个sigmoid处理

注意

BCEWithLogitsLoss比BCELoss多一个pos_weight参数,用于处理每种标签中正负样本不均衡问题

代码演示

import torch
sigmoid = torch.nn.Sigmoid()
BCEWithLogitsLoss = torch.nn.BCEWithLogitsLoss()
BCELoss = torch.nn.BCELoss()
pre = torch.FloatTensor([0.2,0.8,0.9])
target = torch.FloatTensor([0,1,1])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

结果

BCELoss(sigmoid(pre),target),BCEWithLogitsLoss(pre,target)
  • 1

在这里插入图片描述

总结

BCELoss传入的值要进行Sigmoid操作
BCEWithLogitsLoss内部封装了Sigmoid操作

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/790712
推荐阅读
相关标签
  

闽ICP备14008679号