Pytorch Bug解决:RuntimeError: running_mean should contain 1 elements not 10
Pytorch Bug解决:RuntimeError: running_mean should contain 1 elements not 10
- 编程环境
- bug描述
- bug分析
- 总结
编程环境
Python 3.9
Pytorch 1.11.0
bug描述
Traceback (most recent call last):File "D:\crl\Projects\start\test.py", line 21, in <module>pred = model(x)File "D:\crl\Anaconda\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_implreturn forward_call(*input, **kwargs)File "D:\crl\Projects\start\mnist.py", line 29, in forwardx = self.layer1(x)File "D:\crl\Anaconda\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_implreturn forward_call(*input, **kwargs)File "D:\crl\Anaconda\envs\torch\lib\site-packages\torch\nn\modules\container.py", line 141, in forwardinput = module(input)File "D:\crl\Anaconda\envs\torch\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_implreturn forward_call(*input, **kwargs)File "D:\crl\Anaconda\envs\torch\lib\site-packages\torch\nn\modules\batchnorm.py", line 168, in forwardreturn F.batch_norm(File "D:\crl\Anaconda\envs\torch\lib\site-packages\torch\nn\functional.py", line 2421, in batch_normreturn torch.batch_norm(
RuntimeError: running_mean should contain 1 elements not 10Process finished with exit code 1
在模型测试阶段,输入测试样本,运行程序结果报出以上错误。
报错显示torch.nn.BatchNorm1d()模块出了问题,running_mean应该包含1个元素,而不是10个元素。
bug分析
running_mean是在网络训练过程中计算得到的数据集均值的统计量,从torch.nn.BatchNorm1d()可以了解到,BN层在通道维度上计算均值和方差的,所以BN层的第一个参数num_features应该为BN上一层输出的通道数。再联想到报错内容,就怀疑是不是在网络结构设计的时候BN层的num_features参数是不是写错了导致通道不匹配,但是经过查看发现自己并没有写错。
# 网络搭建
class weldingControlNet(nn.Module):def __init__(self, channels): # channels是储存通道数的listsuper(weldingControlNet, self).__init__()self.layer1 = nn.Sequential(nn.Linear(channels[0], channels[1]),nn.BatchNorm1d(channels[1]),nn.ReLU(inplace=False))self.layer2 = nn.Sequential(nn.Linear(channels[1], channels[2]),nn.BatchNorm1d(channels[2]),nn.ReLU(inplace=False))self.layer3 = nn.Sequential(nn.Linear(channels[2], channels[3]),nn.ReLU(inplace=False))def forward(self, x):x = self.layer1(x)x = self.layer2(x)x = self.layer3(x)return x
但是依然觉得是输入数据通道的问题,经过仔细检查,发现我把输入的测试样本加入了Batch维度[1, 1, 5],其中第一个维度上的1表示batch_size,但在测试阶段是不应该添加batch维度的,正确的维度应该为[1, 5]。改正过后,程序可以正常运行。
for item in range(test_num):# 改正前的代码# tf = torchvision.transforms.Compose([transforms.ToTensor()])# x = tf(np.float32(test_data[0: 5, item]).reshape(-1, 1).T) # x维度[1, 1, 5]# y = tf(np.float32(test_data[5:, item]).reshape(-1, 1).T) # y维度[1, 1, 2]# pred = model(x)# 改正后代码x = torch.FloatTensor(np.float32(test_data[0: 5, item]).reshape(-1, 1).T) # [1, 5]y = torch.FloatTensor(np.float32(test_data[5:, item]).reshape(-1, 1).T) # [1, 2]pred = model(x)
总结
如果大家在写代码的时候遇到上述问题,有以下建议:
1、首先查看网络结构设计的时候,BN层的num_features参数是否设置正确,num_features要等于上一层网络输出的通道数
2、如果没有第一条中描述的问题,那就要检查以下测试样本输入的维度是否正确,不需要添加batchsize的维度。
Pytorch Bug解决:RuntimeError: running_mean should contain 1 elements not 10相关推荐
- 解决RuntimeError: running_mean should contain 36864 elements not 4096
一.问题描述 在模型微调最后2层时,BatchNorm1d如题报错. def get_model():model_pre = models.resnet50(pretrained=True) # 获取 ...
- Pytorch Bug解决:RuntimeError:one of the variables needed for gradient computation has been modified
Pytorch Bug解决:RuntimeError: one of the variables needed for gradient computation has been modified b ...
- PyTorch——解决报错“RuntimeError: running_mean should contain *** elements not ***”
1 问题描述 在使用PyTorch编程的时候,经常遇到一种报错就是:"RuntimeError: running_mean should contain *** elements not * ...
- 已解决RuntimeError: CUDA error: device-side assert triggered异常的正确解决方法,亲测有效!!!
已解决RuntimeError: CUDA error: device-side assert triggered异常的正确解决方法,亲测有效!!! 文章目录 报错问题 解决方法 福利 报错问题 粉丝 ...
- 成功解决RuntimeError: Selected KDE bandwidth is 0. Cannot estiamte density
成功解决RuntimeError: Selected KDE bandwidth is 0. Cannot estiamte density 目录 解决问题 解决思路 解决方法 1.重新定义bw参数 ...
- docker配置阿里云镜像加速并更改镜像和容器的存储位置及常见的docker bug解决方法
配置阿里云镜像加速 使用docker容器管理服务,总结下来就三个重要概念: 镜像,容器,数据卷.在之前的博文中我们已经讲述了如何在linux服务器上安装docker,并运行docker服务了. 这篇文 ...
- 如何解决“RuntimeError: CUDA Out of memory”问题
点击上方"AI公园",关注公众号,选择加"星标"或"置顶" 作者:Nitin Kishore 编译:ronghuaiyang 导读 一些可以 ...
- Pytorch BUG: ValueError:only one element tensors can be converted to Python scalars
Pytorch BUG: ValueError:only one element tensors can be converted to Python scalars 这是由于把包含tensor的列表 ...
- pytorch版本问题RuntimeError: params/unet.pth is a zip archive (did you mean to use torch.jit.load()?)
pytorch版本问题RuntimeError: params/unet.pth is a zip archive (did you mean to use torch.jit.load()?) 问题 ...
- vscode 格式化某一段代码_VSCode格式化代码功能失效的bug解决方法
VSCode格式化代码功能失效的bug解决方法 前不久我装上了 黑苹果,那么为了快速转移开发环境,我使用了VSCode(Visual Studio Code下面简称VSCode)的插件 Setting ...
最新文章
- vs中将网站aspx.cs文件打包成一个dll
- R语言基于glmnet构建分类模型并可视化特征系数(coefficient)以及L1正则化系数(lambda)实战
- 美宣布制裁中兴通讯 商务部:坚决反对并将交涉
- java程序结构_Java 程序结构说明(学习 Java 编程语言 004)
- 前端生成Excel 表格
- RxSwift之UI控件Label扩展的使用
- 全国计算机等级考试题库二级C操作题100套(第03套)
- sql azure 语法_使用Visual Studio和SQL Azure数据库
- 转载:AD的授权还原和主还原:深入浅出Active Directory系列(六)
- 【转】OC门与线与逻辑(含逻辑图)
- fanuc机器人与示教器配对_FANUC机器人示教器维修
- 微信支付 发起商家转账API 2022年v3 transfer batches
- NOIP2012 国王游戏(贪心)
- 微信二次开发html,基于mvc的微信二次开发c#源代码
- 520|测一测你和ta的夫妻相
- 第27次CCF-CSP计算机软件能力认证(2022-09-18)
- Linux下的Web开发工具(一)
- 用Cocos Creator 做了一个模拟股票游戏
- pdf转txt java_pdf转换txt怎么操作?pdf文件可以转换成txt文件吗?
- webpack之sideEffects