Calculation process of CrossEntropyLoss loss function

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

The following code uses nn.CrossEntropyLoss and nn.logSoftmax()+nn.NLLLoss() to find the loss respectively. According to the final output, the two have the same effect

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



Guess you like

Origin blog.csdn.net/weixin_42173136/article/details/129888761
Recommended