CrossEntropyLoss损失函数的计算过程

CrossEntropyLoss = nn.logSoftmax()+nn.NLLLoss()

以下代码使用nn.CrossEntropyLoss和使用nn.logSoftmax()+nn.NLLLoss()分别求损失,根据最后的输出显示二者的效果相同

import torch
import torch.nn as nn
criterion = nn.CrossEntropyLoss()
output = torch.randn(3, 5, requires_grad=True)
print(f'output:{
      
      output}')
soft_max = nn.Softmax(dim=1)
output_soft = torch.log(soft_max(output))  # 进行log(softmax)
# output_soft:tensor([[-1.0698, -3.3372, -2.8469, -1.2050, -1.3331],
#         [-2.8625, -1.7787, -0.7658, -1.9911, -1.7573],
#         [-0.8101, -1.7529, -2.3501, -3.0553, -1.4295]], grad_fn=<LogBackward0>)
# loss_nll =(-1.0698 + -1.0698 + -1.7529) / 3 = 1.962  因为labels是[0,2,1], 即第一行取位置0,第二行取位置2,第三行取位置1
print(f'output_soft:{
      
      output_soft}')
nn_loss = nn.NLLLoss()
labels = torch.tensor([0,2,1])
loss_nll = nn_loss(output_soft, labels)     # 进行nllloss
l1 = loss_nll.detach().numpy()
l2 = criterion(output, labels).detach().numpy() # 进行CrossEntropyLoss
print(l1 == l2) # True



猜你喜欢

转载自blog.csdn.net/weixin_42173136/article/details/129888761