Pytorch实现交叉熵的过程

在pytorch当中,有两种方式可以实现交叉熵,而我们把softmax概率传入传入对数似然损失得到的损失函数叫做“交叉熵损失”

在pytorch当中有两种方法实现交叉熵损失:

实现方式1:

criterion=nn.CrossEntropyLoss()
loss=criterion(input,target)

实现方式2:

#对输出值进行计算softmax,并取对数,而这个output是需要在神经网络模型的输出return语句当中就要计算好的
output=F.log_softmax(x,dim=-1)
#使用pytorch当中的带权损失
loss=F.nll_loss(output,target)

 

posted @ 2021-07-03 13:05  Geeksongs  阅读(823)  评论(0编辑  收藏  举报

Coded by Geeksongs on Linux

All rights reserved, no one is allowed to pirate or use the document for other purposes.