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_implreturn forward_call(*input, **kwargs)File "mobilenet_v1.py", line 138, in forwardx = self.fc(x)File "J:\WorkSoft\envs\yolov5_test\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_implreturn forward_call(*input, **kwargs)File "J:\WorkSoft\envs\yolov5_test\lib\site-packages\torch\nn\modules\linear.py", line 96, in forwardreturn F.linear(input, self.weight, self.bias)File "J:\WorkSoft\envs\yolov5_test\lib\site-packages\torch\nn\functional.py", line 1847, in linearreturn 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 forwardx = 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]

RuntimeError: mat1 and mat2 shapes cannot be multiplied (1024x1 and 1024x3)相关推荐

  1. RuntimeError: mat1 and mat2 shapes cannot be multiplied

    RuntimeError: mat1 and mat2 shapes cannot be multiplied RuntimeError: mat1 and mat2 shapes cannot be ...

  2. 解决:RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x256 and 8x256)维度不匹配问题

    在设计网络是,前面几层是去噪网络,后边几层是分类网络,因为之前没有接触过分类任务,对全连接层输入维度不太理解,出现错误RuntimeError: mat1 and mat2 shapes cannot ...

  3. RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x7 and 784x1024) 报错解决

    RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x7 and 784x1024) 运行pytorch时,出现以下报错: 错误代码: ...

  4. RuntimeError: mat1 and mat2 shapes cannot be multiplied (3584x7 and 25088x4096)

    RuntimeError: mat1 and mat2 shapes cannot be multiplied (3584x7 and 25088x4096) 使用VGG19提取图像特征时出现该问题 ...

  5. Pytorch vgg16 实现CIFAR10数据集分类 以及RuntimeError: mat1 and mat2 shapes cannot be multiplied终极详解

    最近从tensorflow转战pytorch,今天又遇到了了一个大坑:RuntimeError: mat1 and mat2 shapes cannot be multiplied,网上的结果大都模模 ...

  6. RuntimeError mat1 and mat2 shapes cannot be multiplied

    详细显示如下 x = self.fc(x) File "D:\Python36\lib\site-packages\torch\nn\modules\module.py", lin ...

  7. pytorch报错:RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x2500 and 3020x1600)

    相信同学们在刚接触深度学习的时候老会遇到类似的问题: 这个其实就是卷积层到全连接层之间的参数数量不对应的问题 卷积层的的输出结果是64*2500的矩阵,那么全连接层的第一个第一层的就应该是:2500* ...

  8. RuntimeError: mat1 and mat2 shapes cannot be multiplied (5760x6 and 128x4)

    在使用pytorch框架定义子类网络结构时,有时可能会出现mat1和mat2的形状不匹配的这种问题.如下,定义了一个7层的cnn网络: class CNN(nn.Module):def __init_ ...

  9. RuntimeError: mat1 and mat2 shapes cannot be multiplied (250x7 and 9x256) pytorch报错

    在利用pytorch搭建神经网络模型的时候,遇到这个错误通常表示矩阵乘法操作的维度不匹配.具体地说,根据错误信息,mat1是一个250x7的矩阵,而mat2是一个9x256的矩阵,它们的维度无法相乘. ...

最新文章

  1. Bootstrap按钮组件
  2. php 获取用户名,PHP如何获取当前windows系统的登录用户名
  3. spring定时任务详解(@Scheduled注解)多线程讲解
  4. 读书笔记--《MicroPython入门指南》
  5. Job 存储和持久化 (第三部分)
  6. JavaScript OO不XX 学习总结
  7. keycloak中文使用文档_基于DOCKER安装配置keycloak
  8. 【Computer Organization笔记26】总线 bus :多个部件之间进行数据传送的共享通道,总线设计 - 总线仲裁、数据传输模式、提高总线性能
  9. UnityShader13:渐变与遮罩
  10. DBeaver 导出数据库结构和数据
  11. 【转】一次HBase问题的解决过程(Status: INCONSISTENT)
  12. git报错:fatal: unable to access ‘...‘: Failed to connect to github port 443: Timed out
  13. idea中按住ctrl+滚轮改变字体大小
  14. 现代软件工程讲义 5.1 软件的质量保证 (QA) 和测试 (Test)
  15. 最近工作中遇到的某些技术问题
  16. 链表和线性表的优缺点
  17. 利用DOS命令压缩、解压文件
  18. Angular4 实现多图片上传预览
  19. Mutiselect下拉复选框(保存和设置默认选中项)
  20. 微信小程序宠物论坛6

热门文章

  1. android为什么总是闪退怎么办,手机应用老是闪退该怎么办?
  2. Java Web之第11章JSP开发模型
  3. 高通核心板,高通骁龙410系列 MSM8916
  4. 【踩坑记录】导航时出现原地旋转,然后无法执行导航
  5. leetCode 357. Count Numbers with Unique Digits | Dynamic Programming | Medium
  6. 论文阅读:Generating Talking Face Landmarks from Speech
  7. python调用百度api实现摄像头识别文字
  8. 精准面试突击55:delete、drop、truncate有什么区别?
  9. 【Node.js】关于Node.js接口的详解和案例--restful风格接口。案例:添加商品接口,添加员工接口,删除员工接口
  10. 腾讯云域名的报价表收费标准和活动报价