Pytorch: 向 Conv2d 提供 3d 输入会给出错误消息

创建于 2017-05-21  ·  3评论  ·  资料来源: pytorch/pytorch

import torch
import torch.nn as nn
from torch.autograd import Variable

m = Variable(torch.randn(3, 10, 20))
nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3)(m)
Traceback (most recent call last):
  File "too.py", line 6, in <module>
    nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3)(m)
  File "/home/soumith/code/pytorch/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/soumith/code/pytorch/torch/nn/modules/conv.py", line 237, in forward
    self.padding, self.dilation, self.groups)
  File "/home/soumith/code/pytorch/torch/nn/functional.py", line 43, in conv2d
    return f(input, weight, bias)
RuntimeError: expected 3D tensor
medium priority (this tag is deprecated)

最有用的评论

是的! 对此非常满意。 发送公关 :)

所有3条评论

刚刚使用具有形状(batch_size,height,weight)的数据遇到了类似的问题,在位置 1 处添加了 1 个维度并对问题进行了排序。 PS。 我正在使用 in_channels = 1。

发生错误是因为在torch/csrc/autograd/functions/convolution.cpp中对view4d的调用期望权重张量也是 3D。

但是,(3D) 输入将被转换为形状为 (3, 10, 1, 20) 的 4D 张量,即使我们不尝试解压权重张量,大小也会不匹配。

也许我们应该在 Python 级别添加一个检查(在nn.functional.conv2d中)以提高 if input.dim() != 4

@sumith如果您对此感到满意,我可以发送 PR

是的! 对此非常满意。 发送公关 :)

此页面是否有帮助?
0 / 5 - 0 等级