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”, line 1102, in _call_impl
return forward_call(*input, **kwargs)
File “D:\Python36\lib\site-packages\torch\nn\modules\linear.py”, line 103, in forward
return F.linear(input, self.weight, self.bias)
File “D:\Python36\lib\site-packages\torch\nn\functional.py”, line 1848, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x704 and 2304x4)

根据提示,全连接层两个需要相乘的矩阵维度不匹配,代码中batchSize为8,最后的类别数量为4。

原因,样本总数不是批次的倍数,有余数,因此,最后一个批次的样本会产生该问题。

解决方案1,dataloader中需要设置参数drop_last=True。即丢弃最后一个不足batchSize的样本。

trainLoader = DataLoader(dataset=trainSet, batch_size=batchSize, shuffle=True, drop_last=True)

解决方案2,reshape时使用样本的数量文章来源地址https://www.toymoban.com/news/detail-418987.html

......
for seq, y_train in trainLoader:
	sampleSize = seq.shape[0]
	optimizer.zero_grad()
    y_pred = model(seq.reshape(sampleSize, 1, -1)) # Dataloader中drop_last=False
    # y_pred = model(seq.reshape(batchSize, 1, -1))
......

到了这里,关于RuntimeError mat1 and mat2 shapes cannot be multiplied的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • RuntimeError: mat1 dim 1 must match mat2 dim 0 解决方法

    RuntimeError: mat1 dim 1 must match mat2 dim 0 解决方法

    RuntimeError: mat1 dim 1 must match mat2 dim 0 这个错误的大概意思是:矩阵mat1 的第二维度要与mat2的第一维度不匹配 在新增别的数据集进行训练时报当前错误,原因是输入的图像大小与之前不一样,这是新手在学习时常会遇到的问题。 先看报错信息,确定报错位置 我的这个代码是

    2024年02月15日
    浏览(9)
  • RuntimeError: Input type (unsigned char) and bias type (float) should be the same错误

    这个错误通常是由于输入数据类型与模型参数的类型不匹配导致的。在PyTorch中,当输入的张量类型与模型的参数类型不匹配时,PyTorch会尝试将它们转换为相同的类型,但是当它们的类型不可转换时,就会出现这个错误。 解决办法是确保输入的张量类型与模型的参数类型相同

    2024年02月15日
    浏览(8)
  • RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the

    RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the

    问题描述: mobilenetv3在残差块中加入了注意力机制 用GPU 进行训练时报的错 解决方法1: 1,不用GPU 用CPU 就可以 CUDA 设置为False,确实可以解决,但是不用GPU 好像意义不大 解决方法2 : 用仍然用GPU ,看下面的的解决方案: 报错的原因:2 1,我直接在倒残差块的前向传播内对导

    2024年02月16日
    浏览(10)
  • Commit cannot be completed since the group has already rebalanced and assign

    Commit cannot be completed since the group has already rebalanced and assign

    这里是说提交commit失败, 因为这个组已经重新分配了 正常情况下, kafka会有一个配置用于设置一条消息的过期时间, 在规定时间内, 如果消费者提交了消费完成的信息, 那么就可以正常的分配下一条记录给消费者, 并且将当前记录的状态记为\\\"已消费\\\"状态, 对消息队列做一个标识

    2024年02月11日
    浏览(8)
  • ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directl

    ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directl

    安装yolov5依赖库时,最后pycocotools报错 重点是以下原因: error: Microsoft Visual C++ 14.0 or greater is required. Get it with \\\"Microsoft C++ Build Tools\\\": https://visualstudio.microsoft.com/visual-cpp-build-tools/ 尝试的解决方法如下: 1、直接下载VS2022中,工作负荷里有关C++和Python的(未成功) 结果报错 vs2

    2024年02月02日
    浏览(15)
  • git fatal: ‘xxx‘ is not a commit and a branch ‘xxx‘ ‘ cannot be created from it

    当拉取一个git远程仓库分支时报错: 命令:git checkout -b 本地分支名 远程分支名 报错:fatal: \\\'origin/dev_v2.8.4_v10.74.1\\\' is not a commit and a branch \\\'dev_v2.8.4_v10.74.1\\\' cannot be created from it 远程新建的分支没有更新到本地。实际上,git仓库分为本地仓库和远程仓库,我们用 checkout 命令是从本

    2024年02月10日
    浏览(7)
  • Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly

    Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly

    当我们运行代码要运用到cv2库时,提示我们没有安装cv2,而直接用pip install opencv-python下载却显示下载失败: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly 直接运用conda安装: 随后完成cv2的安装。

    2024年02月10日
    浏览(15)
  • Could not build wheels for opencv-python-headless which use PEP 517 and cannot be installed directly

    笔者是python环境下安装 albumentations 出现的,该库经常用于图像增强,在cv领域有很大的知名度。在使用下边的命令进行安装后 就报了 ERROR:Could not build wheels for opencv-python-headless which use PEP 517 and cannot be installed directly 。 albumentations库依赖opencv,在直接使用pip命令安装时,albumen

    2024年02月15日
    浏览(8)
  • 【Git报错】fatal: ‘origin/XXX‘ is not a commit and a branch ‘XXX‘ cannot be created from it

    【Git报错】fatal: ‘origin/XXX‘ is not a commit and a branch ‘XXX‘ cannot be created from it

    发现问题 远程已有分支,本地需要新建对应分支,于是执行命令: git checkout --track origin/XXX 时报错。 原因: 远程真的没有这个分支,所以失败 这个情况没什么好说的 远程有这个分支,但是本地认为远程没有这个分支 执行如下命令,查看本地缓存的所有远程分支,看看你要

    2024年02月16日
    浏览(10)
  • ERROR: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly

    ERROR: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly

    pip install --upgrade -r requirements.txt -i https://mirror.baidu.com/pypi/simple Looking in indexes: https://mirror.baidu.com/pypi/simple Collecting prettytable Downloading https://mirror.baidu.com/pypi/packages/5f/ab/64371af206988d7b15c8112c9c277b8eb4618397c01471e52b902a17f59c/prettytable-3.3.0-py3-none-any.whl (26 kB) Collecting ujson Downloading https://

    2024年01月22日
    浏览(17)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包