【问题标题】:RuntimeError: size mismatch, m1: [192 x 68], m2: [1024 x 68] at /opt/conda/conda-bld/pytorch_/work/aten/src/THC/generic/THCTensorMathBlas.cu:268RuntimeError:大小不匹配,m1:[192 x 68],m2:[1024 x 68] 在 /opt/conda/conda-bld/pytorch_/work/aten/src/THC/generic/THCTensorMathBlas.cu:268
【发布时间】:2019-12-10 19:11:38
【问题描述】:

我收到一个我无法理解的尺寸不匹配错误。

(Pdb) self.W_di
Linear(in_features=68, out_features=1024, bias=True)
(Pdb) indices.size()
torch.Size([32, 6, 68])
(Pdb) self.W_di(indices)
*** RuntimeError: size mismatch, m1: [192 x 68], m2: [1024 x 68] at /opt/conda/conda-bld/pytorch_1556653099582/work/aten/src/THC/generic/THCTensorMathBlas.cu:268

为什么会出现不匹配? 可能是因为我在forward(而不是_init_)中定义权重的方式?

这就是我定义self.W_di的方式:

def forward(self):

    if self.W_di is None:
        self.W_di_weight = nn.Parameter(torch.randn(mL_n * 2,1024).to(device))
        self.W_di_bias = nn.Parameter(torch.ones(1024).to(device))                  

    self.W_di = nn.Linear(mL_n * 2, 1024)
    self.W_di.weight = self.W_di_weight
    self.W_di.bias = self.W_di_bias

    result = self.W_di(indices)

任何指针将不胜感激!

【问题讨论】:

  • 我认为您不应该在 forward 方法中创建您的网络,因为每次运行它时都会重新创建它(除非您是故意这样做的)。我不确定网络是否能够以这种方式学习。我不完全确定为什么会出现不匹配,但192 来自32 x 6,这是indices 的前两个维度。
  • 我建议你尝试 32 x 6 x 68 = 13056 作为线性层的in_features
  • 嗨,弗洛里安,我故意在 forward 中初始化它们:discuss.pytorch.org/t/…

标签: pytorch


【解决方案1】:

here中查看我的答案,一般你可以设置

self.W_di = nn.Linear(mL_n * 2, 68)

或者增加in特征。

【讨论】:

    【解决方案2】:

    通常,当您的输入图像未调整到模型的预期大小时,我们也会在 cnn 中遇到此错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 2019-05-18
      • 2019-07-25
      相关资源
      最近更新 更多