【问题标题】:Pytorch nn.CrossEntropyLoss giving, ValueError: Expected target size (x, y), got torch.Size([x, z]) for 3d tensorPytorch nn.CrossEntropyLoss give, ValueError: Expected target size (x, y), got torch.Size([x, z]) for 3d 张量
【发布时间】: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大小的维度。

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

【问题讨论】:

    标签: python pytorch


    【解决方案1】:

    这就是文档中关于 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())
    

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

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

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 2021-04-04
      • 2020-06-05
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 2020-01-20
      相关资源
      最近更新 更多