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

class CNN(nn.Module):def __init__(self):super(CNN,self).__init__()self.conv1 = nn.Sequential(nn.Conv2d(in_channels=1,out_channels=16,kernel_size=3,stride=1,padding=1),nn.ReLU(),nn.MaxPool2d(kernel_size=2),)self.conv2 = nn.Sequential(nn.Conv2d(16,32,3,1,1),nn.ReLU(),nn.MaxPool2d(2),)self.conv3 = nn.Sequential(nn.Conv2d(32,64,2,1,1),nn.ReLU(),nn.MaxPool2d(2),)self.out = nn.Linear(128,4)def forward(self,x):x = self.conv1(x)x = self.conv2(x)output = self.out(x)return output

此时就会出现如下的错误 :

这种问题源于所定义的最后一层池化层输出的形状和全连接层输入的形状不一样。我们通过在前向传播函数中打印池化层的输出形状可知:

def forward(self,x):x = self.conv1(x)x = self.conv2(x)print(x.shape)output = self.out(x)return output>> torch.Size([30, 32, 6, 6])  #池化层输出形状

([30, 32, 6, 6]) 其中的30是设置的batch_size,后三维才是其真正的形状,而全连接层的输入是一维特征,因此需要添加一个flatten层进行压平操作。压平后如下:

torch.Size([30, 1152])

鉴于pytorch框架的特点, 需要再添加一个全连接层来衔接压平层和最后一层全连接层,其输入形状为1152,输出为128。(即在以上代码conv3和out再封装一个层):

    def __init__(self):super(CNN,self).__init__()...... self.conv3 = nn.Sequential(nn.Conv2d(32,64,2,1,1),nn.ReLU(),    nn.MaxPool2d(2),)self.dense = nn.Sequential(nn.Flatten(),nn.Linear(1152,128),nn.Linear(128,4),)def forward(self,x):x = self.conv1(x)x = self.conv2(x)output = self.out(x)return output

代码不再报错,训练网络成功。

RuntimeError: mat1 and mat2 shapes cannot be multiplied (5760x6 and 128x4)相关推荐

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

    RuntimeError: mat1 and mat2 shapes cannot be multiplied (1024x1 and 1024x3) 前言:在学习pytorch 搭建神经网络的时候, ...

  2. RuntimeError: mat1 and mat2 shapes cannot be multiplied

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

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

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

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

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

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

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

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

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

  7. RuntimeError mat1 and mat2 shapes cannot be multiplied

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

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

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

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

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

最新文章

  1. win10计算机右键属性打不开,win10系统右键计算机无法打开属性窗口的解决技巧...
  2. 开心一刻,你和导师之间有什么有趣的事情?
  3. AQS理解之一,基础知识——LockSupport
  4. ViewRoot,DecorView,MeasureSpec和View的工作原理——Android开发艺术探索笔记
  5. asp.net treeView绑定
  6. 穷人最缺少的是什么?
  7. 获取到的数字证书如何配置在自己的Apache中?
  8. 运维:使用awk命令获取文本的某一行,某一列
  9. 每天只睡 4 小时!大佬们都这么拼吗?
  10. 【学习 Opencv】—— 访问摄像机和视频
  11. 微信小程序微商城(七):动态API实现商品分类
  12. 如何在Windows Server 2003下安装NetBEUI
  13. 搞全闪存阵列的各执一词 宏杉说别吵了,就用我哒
  14. 最新黑客攻防实战从入门到精通(第二版)_学习笔记(二)
  15. 高斯模糊 高斯核函数
  16. 服务器终端性能测试之iometer
  17. iozone测试IO方法
  18. 观点:游戏模块分析总结
  19. 学习《软件工程》心得
  20. 丘成桐谈几何:从黎曼、爱因斯坦到弦论

热门文章

  1. 【产品经理】AARRR模型
  2. Linux:HDMI驅動之HPD
  3. amCharts使用方式
  4. Java UTC时间戳
  5. 25 Nacos实战:灰度配置如何实现?
  6. 从程序员到项目经理(16):原来一切问题都是可以解决的
  7. 安徽师大附中%你赛day9 T2 富 解题报告
  8. 基于华为云服务器Docker nginx安装和配置挂载
  9. luogu P2252 [SHOI2002]取石子游戏|【模板】威佐夫博弈
  10. 发送QQ邮件报证书不被信任 Error in certificate