RuntimeError: mat1 and mat2 shapes cannot be multiplied (1024x1 and 1024x3)
前言:在学习pytorch 搭建神经网络的时候,测试网络发现出现RuntimeError: mat1 and mat2 shapes cannot be multiplied (1024x1 and 1024x3)的错误,记录下。
一、报错如下
Traceback (most recent call last):
File "mobilenet_v1.py", line 145, in <module>
out = model(input)
File "J:\WorkSoft\envs\yolov5_test\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "mobilenet_v1.py", line 138, in forward
x = self.fc(x)
File "J:\WorkSoft\envs\yolov5_test\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "J:\WorkSoft\envs\yolov5_test\lib\site-packages\torch\nn\modules\linear.py", line 96, in forward
return F.linear(input, self.weight, self.bias)
File "J:\WorkSoft\envs\yolov5_test\lib\site-packages\torch\nn\functional.py", line 1847, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1024x1 and 1024x3)
二、定位报错为全连接层
File "mobilenet_v1.py", line 138, in forward
x = self.fc(x)
三、原因分析
卷积层的输入为四维[batch_size,channels,H,W] ,而全连接接受维度为2的输入,通常为[batch_size, size]。
四、解决办法
在全连接层前面加入维度变换
//方法一:
x.view(-1,7* 7* 1024)
//方法二:
x = torch.flatten(x,1) //拉成二维向量[batch_size, size]