pytorch加载自定义网络权重的实现


Posted in Python onJanuary 07, 2020

在将自定义的网络权重加载到网络中时,报错:

AttributeError: 'dict' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.

我们一步一步分析。

模型网络权重保存额代码是:torch.save(net.state_dict(),'net.pkl')

(1)查看获取模型权重的源码:

pytorch源码:net.state_dict()

def state_dict(self, destination=None, prefix='', keep_vars=False):
  r"""Returns a dictionary containing a whole state of the module.

  Both parameters and persistent buffers (e.g. running averages) are
  included. Keys are corresponding parameter and buffer names.

  Returns:
    dict:
      a dictionary containing a whole state of the module

  Example::

    >>> module.state_dict().keys()
    ['bias', 'weight']

  """

将网络中所有的状态保存到一个字典中了,我自己构建的就是一个字典,没问题!

(2)查看保存模型权重的源码:

pytorch源码:torch.save()

def save(obj, f, pickle_module=pickle, pickle_protocol=DEFAULT_PROTOCOL):
  """Saves an object to a disk file.

  See also: :ref:`recommend-saving-models`

  Args:
    obj: saved object
    f: a file-like object (has to implement write and flush) or a string
      containing a file name
    pickle_module: module used for pickling metadata and objects
    pickle_protocol: can be specified to override the default protocol

  .. warning::
    If you are using Python 2, torch.save does NOT support StringIO.StringIO
    as a valid file-like object. This is because the write method should return
    the number of bytes written; StringIO.write() does not do this.

    Please use something like io.BytesIO instead.

函数功能是将字典保存为磁盘文件(二进制数据),那么我们在torch.load()时,就是在内存中加载二进制数据,这就是报错点。

解决方案:将字典保存为BytesIO文件之后,模型再net.load_state_dict()

#b为自定义的字典
torch.save(b,'new.pkl')
net.load_state_dict(torch.load(b))

解决方法很简单,主要记录解决思路。

以上这篇pytorch加载自定义网络权重的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python程序抓取新浪在国内的所有IP的教程
May 04 Python
Python中使用ElementTree解析XML示例
Jun 02 Python
Python实现批量转换文件编码的方法
Jul 28 Python
Python使用正则表达式实现文本替换的方法
Apr 18 Python
Python实现ping指定IP的示例
Jun 04 Python
基于python指定包的安装路径方法
Oct 27 Python
对Python 语音识别框架详解
Dec 24 Python
python机器人运动范围问题的解答
Apr 29 Python
python 上下文管理器及自定义原理解析
Nov 19 Python
如何在keras中添加自己的优化器(如adam等)
Jun 19 Python
浅谈如何使用python抓取网页中的动态数据实现
Aug 17 Python
Python实现对word文档添加密码去除密码的示例代码
Dec 29 Python
Matplotlib绘制雷达图和三维图的示例代码
Jan 07 #Python
Pytorch 神经网络—自定义数据集上实现教程
Jan 07 #Python
浅谈Python访问MySQL的正确姿势
Jan 07 #Python
pytorch自定义二值化网络层方式
Jan 07 #Python
Pytorch: 自定义网络层实例
Jan 07 #Python
Python StringIO如何在内存中读写str
Jan 07 #Python
Python内置数据类型list各方法的性能测试过程解析
Jan 07 #Python
You might like
php 短链接算法收集与分析
2011/12/30 PHP
一个PHP针对数字的加密解密类
2014/03/20 PHP
PHP处理Oracle的CLOB实例
2014/11/03 PHP
php实现将wav文件转换成图像文件并在页面中显示的方法
2015/04/21 PHP
php生成txt文件实例代码介绍
2016/04/28 PHP
iOS+PHP注册登录系统 PHP部分(上)
2016/12/26 PHP
PHP实现蛇形矩阵,回环矩阵及数字螺旋矩阵的方法分析
2017/05/29 PHP
php 删除指定文件夹的实例讲解
2017/07/25 PHP
jQuery阻止同类型事件小结
2013/04/19 Javascript
利用JS判断用户是否上网(连接网络)
2013/12/23 Javascript
js实现对table动态添加、删除和更新的方法
2015/02/10 Javascript
JavaScript中Function详解
2015/02/27 Javascript
使用Jasmine和Karma对AngularJS页面程序进行测试
2016/03/05 Javascript
headjs实现网站并行加载但顺序执行JS
2016/11/29 Javascript
jquery点击展示与隐藏更多内容
2016/12/03 Javascript
AngularJS ng-repeat指令中使用track by子语句解决重复数据遍历错误问题
2017/01/21 Javascript
基于jQuery实现手风琴菜单、层级菜单、置顶菜单、无缝滚动效果
2017/07/20 jQuery
使用react-router4.0实现重定向和404功能的方法
2017/08/28 Javascript
Vue-Router实现组件间跳转的三种方法
2017/11/07 Javascript
node前端开发模板引擎Jade的入门
2018/05/11 Javascript
JS获取当前时间的年月日时分秒及时间的格式化的方法
2019/12/18 Javascript
[01:02:09]Liquid vs TNC 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.21
2020/07/19 DOTA
Python开发编码规范
2006/09/08 Python
在Python中使用PIL模块处理图像的教程
2015/04/29 Python
Python的collections模块中namedtuple结构使用示例
2016/07/07 Python
spyder常用快捷键(分享)
2017/07/19 Python
对python3中pathlib库的Path类的使用详解
2018/10/14 Python
Python3之不使用第三方变量,实现交换两个变量的值
2019/06/26 Python
python使用turtle库绘制奥运五环
2020/02/24 Python
Pandas的Apply函数具体使用
2020/07/21 Python
中国最大隐形眼镜网上商城:视客眼镜网
2016/10/30 全球购物
Urban Outfitters德国官网:美国跨国生活方式零售公司
2018/05/21 全球购物
党的群众路线教育实践活动批评与自我批评
2014/02/16 职场文书
个人思想政治总结
2015/03/05 职场文书
工作失职检讨书范文
2015/05/05 职场文书
英语专业毕业论文答辩开场白
2015/05/27 职场文书