【发布时间】: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