pytorch方法测试——损失函数(CrossEntropyLoss)

测试代码:

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)
输出为:
输入为5类:
tensor([[ 1.1157,  1.3396,  0.6192,  0.3732,  0.8985]])
要计算loss的类别:
tensor([ 4])
计算loss的结果:
tensor( 1.6380)
自己的计算结果:
tensor( 1.6380)

多维度测试:

 
  
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("输入为35:")
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)
输出为:
输入为3个5类:
tensor([[ 0.0606, -1.1610, -1.2990,  0.2101,  1.5104],
        [-0.6388, -0.4053, -0.4196,  0.7060,  2.2793],
        [ 0.3973,  0.6114, -0.1127, -0.7732, -0.0592]])
要计算loss的类别:
tensor([ 4,  1,  4])
计算loss的结果:
tensor( 1.7661)
自己的计算结果:
tensor( 1.7661)
结论:

    CrossEntropyLoss计算公式为

    CrossEntropyLoss带权重的计算公式为(默认weight=None

多维度计算时:loss为所有维度loss的平均。

你可能感兴趣的:(pytorch)