运行时错误:大小不匹配,m1:[192 x 68],m2:[1024 x 68]at/opt/conda/conda bld/pytorch\uwork/aten/src/THC/generic/THCTensorMathBlas.cu:268

运行时错误:大小不匹配,m1:[192 x 68],m2:[1024 x 68]at/opt/conda/conda bld/pytorch\uwork/aten/src/THC/generic/THCTensorMathBlas.cu:268,pytorch,Pytorch,我遇到了一个无法理解的大小不匹配错误 (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/pyt

我遇到了一个无法理解的大小不匹配错误

(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
为什么会出现不匹配? 可能是因为我在
前进
中定义权重的方式(而不是
\u init

我就是这样定义自己的

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)
任何指针都将不胜感激

请检查我的答案,一般情况下,您可以设置

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

或者增加功能。

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

我认为您不应该使用
转发
方法创建网络,因为每次运行它时都会重新创建网络(除非您是有意这样做的)。我不确定网络是否能够以这种方式学习。我不完全确定为什么会出现不匹配,但是
192
来自
32 x 6
,这是
索引的前两个维度。我建议您尝试
32 x 6 x 68=13056
作为线性层的
输入功能。你好,Florian,我在
向前
中初始化了它们,目的是: