PyTorch里NLLLoss、CrossEntropyLoss、BCELoss和BCEWithLogitsLoss之间的区别和联系

NLLLoss

examples

m = nn.LogSoftmax(dim=1)
loss = nn.NLLLoss()
# input is of size N x C = 3 x 5
input = torch.randn(3, 5, requires_grad=True)
# each element in target has to have 0 <= value < C
target = torch.tensor([1, 0, 4])
output = loss(m(input), target)
output.backward()


# 2D loss example (used, for example, with image inputs)
N, C = 5, 4
loss = nn.NLLLoss()
# input is of size N x C x height x width
data = torch.randn(N, 16, 10, 10)
conv = nn.Conv2d(16, C, (3, 3))
m = nn.LogSoftmax(dim=1)
# each element in target has to have 0 <= value < C
target = torch.empty(N, 8, 8, dtype=torch.long).random_(0, C)
output = loss(m(conv(data)), target)
output.backward()

loss

unreduced

reduced

N是batchsize

理解

来自 https://www.cnblogs.com/ZJUT-jiangnan/p/5489047.html

NLLLoss前面必须要有log_softmax。拿着前面log_softmax输出的一批logy(这里的notation和上面的不一样。上面的y相当于这里的t,这里的logy相当于上面的x),找出target对应的那个logy,给它加个负号就是loss了。

CrossEntropyLoss

This criterion combines nn.LogSoftmax() and nn.NLLLoss() in one single class.

example

loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()

loss

理解

跟上面NLLLoss里cross entropy = log_softmax + NLLLoss的搭配是一致的。

就是对每一个样本预测多个值,每个值对应一个类别,经过log_softmax后取target 类别对应的那个值。

BCELoss

example

m = nn.Sigmoid()
loss = nn.BCELoss()
input = torch.randn(3, requires_grad=True)
target = torch.empty(3).random_(2)
output = loss(m(input), target)
output.backward()

loss

理解

前面一定要加sigmoid。每个样本预测一个值,经过sigmoid后加个log优化。

BCEWithLogitsLoss

就是上面那个把sigmoid层放进去,为了数值稳定。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值