赞
踩
交叉熵描述了两个概率分布之间的距离,当交叉熵越小说明二者之间越接近。该损失函数结合了nn.LogSoftmax()和nn.NLLLoss()两个函数。它在做分类训练的时候是非常有用的。
b这里指的是binary,这是个二分类交叉熵损失,公式如下所示
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])
BCELoss(sigmoid(pre),target),BCEWithLogitsLoss(pre,target)
BCELoss传入的值要进行Sigmoid操作
BCEWithLogitsLoss内部封装了Sigmoid操作
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。