繁体   English   中英

Pytorch nn.CrossEntropyLoss 给出,ValueError: 预期目标大小 (x, y),得到 3d 张量的 torch.Size([x, z])

[英]Pytorch nn.CrossEntropyLoss giving, ValueError: Expected target size (x, y), got torch.Size([x, z]) for 3d tensor

我正在按照此处的示例进行操作,其中文档说:

输入:(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 大小的维度。

有什么我没有为这个损失函数正确设置的吗?

这是文档中关于 K 维损失的内容:

也可用于更高维度的输入,例如 2D 图像,通过提供大小为 (minibatch, C, d_1, d_2, ..., d_K) 且 K ≥ 1 的输入,其中 K 是维数,以及适当形状的目标(见下文)。

如果您有 3 个类,则正确的输入应该具有(32, 3, 128)形状:

import torch.nn as nn
import torch
loss = nn.CrossEntropyLoss(ignore_index=0)

inputs = torch.rand(32, 3, 128)
targets = torch.ones(32, 128)

loss(inputs, targets.long())

或者目标应该有一个(32, 3)形状,如果你有 128 个类:

inputs = torch.rand(32, 128, 3)
targets = torch.ones(32, 3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM