AttributeError: module 'torch.nn.init' has no attribute 'zeros_'
问题
File "E:\wj-lab\expStad\model.py", line 50, in __init__feat_block.apply(weights_init_kaiming)File "D:\Anaconda3\envs\python35\lib\site-packages\torch\nn\modules\module.py", line 231, in applymodule.apply(fn)File "D:\Anaconda3\envs\python35\lib\site-packages\torch\nn\modules\module.py", line 232, in applyfn(self)File "E:\wj-lab\expStad\model.py", line 26, in weights_init_kaiminginit.zeros(m.bias.data)AttributeError: module 'torch.nn.init' has no attribute 'zeros_'
出错源码
def weights_init_kaiming(m):classname = m.__class__.__name__ if classname.find('Conv') != -1:init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')elif classname.find('Linear') != -1:init.kaiming_normal_(m.weight.data, a=0, mode='fan_out')init.zeros(m.bias.data)elif classname.find('BatchNorm1d') != -1:init.normal_(m.weight.data, 1.0, 0.01)init.zeros(m.bias.data)def weights_init_classifier(m):classname = m.__class__.__name__if classname.find('Linear') != -1:init.normal_(m.weight.data, 0, 0.001)init.zeros(m.bias.data)
原因
>>> python3 # 进入python 环境
>>> import torch
>>> dir(torch) # 查看pytorch支持环境,是否有属性'__init_'如下:
[ ByteStorage', 'ByteTensor', 'CharTensor', 'CompiledFunction', 'CudaCharStorageBase', 'CudaDoubleStorageBase', 'CudaFloatStorageBase', 'CudaIntStorageBase', 'CudaLongStorageBase '__init__', '__le__', '__loader__' ........]>>> dir(torch.nn.init) # 查看torch.nn.init 支持环境,是否有'attribute zeros_'如下:
['__builtins__','__cached__','__doc__','__file__','__loader__','__package__','kaiming_normal', 'kaiming_normal_', 'kaiming_uniform', 'kaiming_uniform_', 'math', 'normal', 'normal_' ......]
发现根本没有 'attribute zeros_' 说明错误原因
- 源码有问题,源码版本是python2
- 当前python环境缺少库函数,使得不支持 attribute ##
pytorch 的 torch.nn.init 中文文档
https://pytorch-cn.readthedocs.io/zh/latest/package_references/nn_init/
torch.nn.init.kaiming_normal() Example 中文文档
https://www.programcreek.com/python/example/108248/torch.nn.init.kaiming_normal
解决办法
def weights_init_kaiming(m):classname = m.__class__.__name__if classname.find('Conv') != -1:m.weight.data = init.kaiming_normal(m.weight.data, a=0, mode='fan_in')elif classname.find('Linear') != -1: m.weight.data = init.kaiming_normal(m.weight.data, a=0, mode='fan_out')m.bias.data.fill_(0)elif classname.find('BatchNorm1d') != -1:m.weight.data.normal_(1.0, 0.01)m.bias.data.fill_(0)def weights_init_classifier(m):classname = m.__class__.__name__if classname.find('Linear') != -1:m.weight.data = init.normal_(m.weight.data, 0, 0.001)m.bias.data.fill_(0)
AttributeError: module 'torch.nn.init' has no attribute 'zeros_'相关推荐
- AttributeError: module 'torch.nn' has no attribute 'LocalResponseNorm'问题的解决办法
错误如下: AttributeError: module 'torch.nn' has no attribute 'LocalResponseNorm' 错误原因: 我在Windows装的pytorc ...
- 运行pytorch时报错——AttributeError: module 'torch.nn' has no attribute 'ModuleDict'
2020.2月的最后两个多小时,下次相遇2.29号就要等到四年后了-- 很神奇的一个月,学生生涯的最后一个假期,没想到竟是最长的一次...还是想祝福每位朋友,无论这段时间境况如何,都要稳稳地走下去,生 ...
- 成功解决AttributeError: module 'tensorflow.nn.rnn_cell' has no attribute 'linear'
成功解决AttributeError: module 'tensorflow.nn.rnn_cell' has no attribute 'linear' 目录 解决问题 解决思路 解决方法 解决问题 ...
- 关于AttributeError: module ‘torch.nn‘ has no attribute ‘Moudle‘的解决方法
注意拼写! 注意拼写! 注意拼写! 注意看看U是第几个字母 是 Mo-du-le 不是moudle
- Can‘t get attribute ‘SiLU‘ on <module ‘torch.nn.modules.activation
在yolov5 4.0版本测试的时候,加载模型报错: Can't get attribute 'SiLU' on <module 'torch.nn.modules.activation 报错代 ...
- 导入torchvision出现:AttributeError: module ‘torch.jit‘ has no attribute ‘unused‘错误
导入torchvision出现:AttributeError: module 'torch.jit' has no attribute 'unused'错误 文章目录: 1 问题原因 2 解决方法 我 ...
- 成功解决AttributeError: module 'torch.utils' has no attribute 'data'
成功解决AttributeError: module 'torch.utils' has no attribute 'data' 目录 解决问题 解决思路 解决方法 解决问题 AttributeErr ...
- AttributeError: module ‘torch‘ has no attribute ‘inference_mode‘
在运行论文源码时弹出AttributeError: module 'torch' has no attribute 'inference_mode' 异常 原因是pytorch版本问题 torch.i ...
- AttributeError: module ‘torch.distributed‘ has no attribute ‘_all_gather_base‘
问题描述 安装完apex后,调用的是时候出现如下错误: File "/home/shuyuan/anaconda3/envs/shuyuan/lib/python3.8/site-packa ...
最新文章
- zabbix自动发现规则实现批量监控主机的TCP监听端口
- Leetcode 240.剑指 Offer 06. 从尾到头打印链表 (每日一题 20210728)
- synchronized不能锁静态变量_肝了一下午的 Synchronized 解析!
- 搞懂 SQL 查询优化原理分析,秒速处理大数据量查询
- RDD, DataFrame or Dataset
- 分库分表的事务处理机制
- “租房帮”产品体验报告
- [转]网络爬虫(一):抓取网页的含义和URL基本构成
- 宝塔mysql表使用中_Mysql-表的基本操作
- 使用Jsoup完成网页爬虫
- Linux多线程编程[精]
- Mysql rpm包安装
- xilinx低延时视频编解码方案
- tms320vc5416c语言编程,TMS320VC5416与CAN总线的接口设计及软件编程
- 网页崩溃原因软件测试,支招:原来这些才是APP崩溃的主要原因!资深技术大牛测试经验总结...
- 再见笨重的ELK!这套轻量级日志收集方案要火!
- SpringMVC---Handler到底是个什么?及流程详解
- 黑客留下的网站后门,可以作什么?
- adonis.js mysql_专注于易用性与速度的Nodejs Web和微服务框架——Adonis
- 安装智能消费机服务器连接,中控消费机软件常见问题|中控CM20消费机教程