pytorch 中的交叉熵损失 nn.CrossEntropyLoss()
Cross entropy loss in pytorch nn.CrossEntropyLoss()
也许有人可以帮助我。我正在尝试计算网络给定输出的交叉熵损失
print output
Variable containing:
1.00000e-02 *
-2.2739 2.9964 -7.8353 7.4667 4.6921 0.1391 0.6118 5.2227 6.2540
-7.3584
[torch.FloatTensor of size 1x10]
和所需的标签,格式为
print lab
Variable containing:
x
[torch.FloatTensor of size 1]
其中 x 是 0 到 9 之间的整数。
根据 pytorch 文档 (http://pytorch.org/docs/master/nn.html)
criterion = nn.CrossEntropyLoss()
loss = criterion(output, lab)
这应该可以,但不幸的是我收到一个奇怪的错误
TypeError: FloatClassNLLCriterion_updateOutput received an invalid combination of arguments - got (int, torch.FloatTensor, !torch.FloatTensor!, torch.FloatTensor, bool, NoneType, torch.FloatTensor, int), but expected (int state, torch.FloatTensor input, torch.LongTensor target, torch.FloatTensor output, bool sizeAverage, [torch.FloatTensor weights or None], torch.FloatTensor total_weight, int ignore_index)
谁能帮帮我?我真的很困惑,几乎尝试了所有我能想到的有用的东西。
最佳
请检查此代码
import torch
import torch.nn as nn
from torch.autograd import Variable
output = Variable(torch.rand(1,10))
target = Variable(torch.LongTensor([1]))
criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)
print(loss)
这将很好地打印出损失:
Variable containing:
2.4498
[torch.FloatTensor of size 1]
也许有人可以帮助我。我正在尝试计算网络给定输出的交叉熵损失
print output
Variable containing:
1.00000e-02 *
-2.2739 2.9964 -7.8353 7.4667 4.6921 0.1391 0.6118 5.2227 6.2540
-7.3584
[torch.FloatTensor of size 1x10]
和所需的标签,格式为
print lab
Variable containing:
x
[torch.FloatTensor of size 1]
其中 x 是 0 到 9 之间的整数。 根据 pytorch 文档 (http://pytorch.org/docs/master/nn.html)
criterion = nn.CrossEntropyLoss()
loss = criterion(output, lab)
这应该可以,但不幸的是我收到一个奇怪的错误
TypeError: FloatClassNLLCriterion_updateOutput received an invalid combination of arguments - got (int, torch.FloatTensor, !torch.FloatTensor!, torch.FloatTensor, bool, NoneType, torch.FloatTensor, int), but expected (int state, torch.FloatTensor input, torch.LongTensor target, torch.FloatTensor output, bool sizeAverage, [torch.FloatTensor weights or None], torch.FloatTensor total_weight, int ignore_index)
谁能帮帮我?我真的很困惑,几乎尝试了所有我能想到的有用的东西。
最佳
请检查此代码
import torch
import torch.nn as nn
from torch.autograd import Variable
output = Variable(torch.rand(1,10))
target = Variable(torch.LongTensor([1]))
criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)
print(loss)
这将很好地打印出损失:
Variable containing:
2.4498
[torch.FloatTensor of size 1]