RuntimeError mat1 and mat2 shapes cannot be multiplied

详细显示如下

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的样本。

trainLoader = DataLoader(dataset=trainSet, batch_size=batchSize, shuffle=True, drop_last=True)

解决方案2,reshape时使用样本的数量

......
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))
......

你可能感兴趣的:(python,深度学习,python,机器学习,pytorch)