RuntimeError: mat1 and mat2 shapes cannot be multiplied (192x64 and 4x3)
原因分析:
矩阵1和矩阵2大小不一样,不能够相乘,这是我们在使用nn.linear函数时参数唯独不匹配,出现的错误,我们回想一下全连接层的公式:
Y = W * X + B
在这里x是我们的原始数据即待输入数据,w是我们的线性层函数。若要让两个矩阵进行相乘,则W和X大小必须一样,我们看linear函数:
全连接层:我们将数据x展平为2维(a,b)则w的维度应该为(b,c)即x的第二维和w的第一维相同。
X2_tensor展开为二维,维度为(192,64),X3_tensor展开为二维,维度为(192,64)分别与(64,3)矩阵相乘。所以linear第一个参数为x展开为二维时第二个维度,第二个参数是想把第二个维度升高或者降低的维度。
X2_tensor = torch.rand(1,3,64,64)
X3_tensor = torch.rand(3,64,64)# 用来改变最后数组最后一维的维度
# 用来缩小或者扩展特征维度
emdeding = nn.Linear(64, 3)
Y2 = emdeding(X2_tensor) # (1,3,64,3)
Y3 = emdeding(X3_tensor) # (3,64,3)
经过线性层后的维度为:
torch.Size([1, 3, 64, 3])
torch.Size([3, 64, 3])
RuntimeError: mat1 and mat2 shapes cannot be multiplied (192x64 and 4x3)相关推荐
- 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
RuntimeError: mat1 and mat2 shapes cannot be multiplied RuntimeError: mat1 and mat2 shapes cannot be ...
- 解决:RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x256 and 8x256)维度不匹配问题
在设计网络是,前面几层是去噪网络,后边几层是分类网络,因为之前没有接触过分类任务,对全连接层输入维度不太理解,出现错误RuntimeError: mat1 and mat2 shapes cannot ...
- RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x7 and 784x1024) 报错解决
RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x7 and 784x1024) 运行pytorch时,出现以下报错: 错误代码: ...
- RuntimeError: mat1 and mat2 shapes cannot be multiplied (3584x7 and 25088x4096)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (3584x7 and 25088x4096) 使用VGG19提取图像特征时出现该问题 ...
- Pytorch vgg16 实现CIFAR10数据集分类 以及RuntimeError: mat1 and mat2 shapes cannot be multiplied终极详解
最近从tensorflow转战pytorch,今天又遇到了了一个大坑:RuntimeError: mat1 and mat2 shapes cannot be multiplied,网上的结果大都模模 ...
- RuntimeError mat1 and mat2 shapes cannot be multiplied
详细显示如下 x = self.fc(x) File "D:\Python36\lib\site-packages\torch\nn\modules\module.py", lin ...
- pytorch报错:RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x2500 and 3020x1600)
相信同学们在刚接触深度学习的时候老会遇到类似的问题: 这个其实就是卷积层到全连接层之间的参数数量不对应的问题 卷积层的的输出结果是64*2500的矩阵,那么全连接层的第一个第一层的就应该是:2500* ...
- RuntimeError: mat1 and mat2 shapes cannot be multiplied (5760x6 and 128x4)
在使用pytorch框架定义子类网络结构时,有时可能会出现mat1和mat2的形状不匹配的这种问题.如下,定义了一个7层的cnn网络: class CNN(nn.Module):def __init_ ...
最新文章
- PostgreSQL 10.1 手册_部分 II. SQL 语言_第 5 章 数据定义_5.11. 外部数据
- JavaEE进阶知识学习-----SpringCloud(六)Ribbon负载均衡
- git maven 一键部署_Jenkins实现一键部署maven项目
- luogu P1659 [国家集训队]拉拉队排练
- java get提交中文乱码_java get方法提交中文乱码问题
- ROS学习笔记十:用C++编写一个简单的服务和客户端
- CentOS GRUB引导错误无法进入系统解决办法
- zabbix4.0LTS安装配置
- 在CentOS中安装MySql数据库教程
- Linux epoll模型详解
- js获取html的ip,JavaScript获取客户端IP
- 基于java嗖嗖移动业务大厅
- 简单方法:将iso镜像文件压缩包转化为直接可用的光盘映像文件
- python识别颜色并提取轮廓_用 Python 对图片主体轮廓进行提取、颜色标记、并计算区域面积...
- 【实验】阿里云大数据助理工程师认证(ACA)- 机器学习实现基本统计分析
- 下载Win10系统和在VM虚拟机中配置完成
- stm32上常用的几个通信协议简介及相关代码
- android自定义相机带方框,Android摄像头开发:拍照后添加相框,融合相框和图片为一副 图片...
- MFC绘制双有理Bezier曲面
- Tcl脚本入门笔记详解(一)