详细显示如下
x = self.fc(x)
File “D:\Python36\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl
return forward_call(*input, **kwargs)
File “D:\Python36\lib\site-packages\torch\nn\modules\linear.py”, line 103, in forward
return F.linear(input, self.weight, self.bias)
File “D:\Python36\lib\site-packages\torch\nn\functional.py”, line 1848, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x704 and 2304x4)
根据提示,全连接层两个需要相乘的矩阵维度不匹配,代码中batchSize为8,最后的类别数量为4。
原因,样本总数不是批次的倍数,有余数,因此,最后一个批次的样本会产生该问题。
解决方案1,dataloader中需要设置参数drop_last=True。即丢弃最后一个不足batchSize的样本。文章来源:https://www.toymoban.com/news/detail-418987.html
trainLoader = DataLoader(dataset=trainSet, batch_size=batchSize, shuffle=True, drop_last=True)
解决方案2,reshape时使用样本的数量文章来源地址https://www.toymoban.com/news/detail-418987.html
......
for seq, y_train in trainLoader:
sampleSize = seq.shape[0]
optimizer.zero_grad()
y_pred = model(seq.reshape(sampleSize, 1, -1)) # Dataloader中drop_last=False
# y_pred = model(seq.reshape(batchSize, 1, -1))
......
到了这里,关于RuntimeError mat1 and mat2 shapes cannot be multiplied的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!