pytorch 交叉熵计算过程

同样可以参考Pytorch中Softmax、Log_Softmax、NLLLoss以及CrossEntropyLoss的关系与区别详解_NeilPy的博客-CSDN博客

 参考以上公式我们进行如下计算:

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

程序结果: 

input  tensor([[ 0.8613,  0.2848, -0.9878,  1.6137,  1.6703],
        [-0.5740,  0.6567, -0.7853, -1.5065,  1.3024],
        [-1.2544, -0.7814,  0.0204, -0.7491,  0.3055]], requires_grad=True)
tensor([0, 2, 3])
loss:tensor(2.1812, grad_fn=<NllLossBackward>)

计算:

loss = -x[class]+log(exp(x[i])累加)

loss1=1.8061534287700232

listnum=[ 0.8613,  0.2848, -0.9878,  1.6137,  1.6703]
sum=0
for num in listnum:
    sum = sum+math.exp(num)
print(-listnum[0]+math.log(sum))

loss2=2.709178782095951

listnum=[-0.5740,  0.6567, -0.7853, -1.5065,  1.3024]
sum=0
for num in listnum:
    sum = sum+math.exp(num)
print(-listnum[2]+math.log(sum))
loss2=-listnum[2]+math.log(sum)

loss3=2.028286903303233

listnum=[-1.2544, -0.7814,  0.0204, -0.7491,  0.3055]
sum=0
for num in listnum:
    sum = sum+math.exp(num)
print(-listnum[3]+math.log(sum))
loss3=-listnum[3]+math.log(sum)

loss=(loss1+loss2+loss3)/3=2.181206371389736 和上面结果一样

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值