【发布时间】:2020-10-31 05:41:25
【问题描述】:
我正在关注示例here,文档中说:
输入:(N, C) 其中 C = 类数
目标:(N),其中每个值为 0 ≤ targets[i] ≤ C−1
2d 张量的例子就是这种情况
loss = nn.CrossEntropyLoss()
input = torch.randn(3, 5, requires_grad=True)
target = torch.empty(3, dtype=torch.long).random_(5)
output = loss(input, target)
output.backward()
但是对于二维张量,我得到一个错误
import torch.nn as nn
import torch
loss = nn.CrossEntropyLoss(ignore_index=0)
inputs = torch.rand(32, 128, 3)
targets = torch.ones(32, 128)
loss(inputs, targets.long())
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-26-61e7f03039a6> in <module>
7 targets = torch.ones(32, 128)
8
----> 9 loss(inputs, targets.long())
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
959
960 def forward(self, input: Tensor, target: Tensor) -> Tensor:
--> 961 return F.cross_entropy(input, target, weight=self.weight,
962 ignore_index=self.ignore_index, reduction=self.reduction)
963
/opt/conda/lib/python3.8/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
2466 if size_average is not None or reduce is not None:
2467 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2468 return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
2469
2470
/opt/conda/lib/python3.8/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
2271 out_size = (n,) + input.size()[2:]
2272 if target.size()[1:] != input.size()[2:]:
-> 2273 raise ValueError('Expected target size {}, got {}'.format(
2274 out_size, target.size()))
2275 input = input.contiguous()
ValueError: Expected target size (32, 3), got torch.Size([32, 128])
据我所知,关于设置尺寸,我做的一切都是正确的。错误信息似乎认为我给了一个2d向量,但我给了它一个3d向量,缺少128大小的维度。
我没有为这个损失函数设置正确的东西吗?
【问题讨论】: