RuntimeError: mat1 and mat2 shapes cannot be multiplied (1024x1 and 1024x3)

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]
错误信息"mat1 and mat2 shapes cannot be multiplied"表示矩阵mat1和mat2的维度不匹配,无法进行矩阵乘法操作。具体来说,这个错误可能是因为矩阵mat1的行数与矩阵mat2的列数不一致,或者矩阵mat1的列数与矩阵mat2的行数不一致。这通常是由于矩阵的维度定义不正确或者在进行矩阵乘法操作时输入的矩阵维度不符合要求导致的。要解决这个问题,你需要检查输入矩阵的维度定义,并确保它们满足矩阵乘法操作的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [报错解决:RuntimeError: mat1 and mat2 shapes cannot be multiplied](https://blog.csdn.net/u013537270/article/details/132154390)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [RuntimeError: mat1 and mat2 shapes cannot be multiplied (250x7 and 9x256) pytorch报错](https://blog.csdn.net/qq_44679208/article/details/130751469)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [shape2d:用于Unity3D的Shapes2D-在Unity中快速制作简单的美术资产](https://download.csdn.net/download/weixin_42109598/15094541)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值