CrossEntropyLoss计算公式为
CrossEntropyLoss带权重的计算公式为(默认weight=None)
多维度计算时:loss为所有维度loss的平均。
import torch
import torch.nn as nn
import math
loss = nn.CrossEntropyLoss()
input = torch.randn(1, 5, requires_grad=True)
target = torch.empty(1, dtype=torch.long).random_(5)
output = loss(input, target)
print("输入为5类:")
print(input)
print("要计算loss的类别:")
print(target)
print("计算loss的结果:")
print(output)
first = 0
for i in range(1):
first -= input[i][target[i]]
second = 0
for i in range(1):
for j in range(5):
second += math.exp(input[i][j])
res = 0
res += first +math.log(second)
print("自己的计算结果:")
print(res)
import torch
import torch.nn as nn
import math loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
print("输入为3个5类:")
print(input)
print("要计算loss的类别:")
print(target) print("计算loss的结果:")
print(output)
first = [0,0,0]
for i in range(3):
first[i] -= input[i][target[i]]
second = [0,0,0]
for i in range(3):
for j in range(5):
second[i] += math.exp(input[i][j])
res = 0
for i in range(3):
res += first[i] +math.log(second[i])
print("自己的计算结果:")
print(res/3)
转载:https://blog.csdn.net/tmk_01/article/details/80839810