python画有权重网络图_pytorch加载自定义网络权重的实现

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

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()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

def save(obj, f, pickle_module=pickle, pickle_protocol=DEFAULT_PROTOCOL):

"""Saves anobject to a diskfile.

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

Args:

obj: savedobject

f: afile-likeobject (has to implement writeand flush)or a string

containing afile name

pickle_module: module usedfor pickling metadataand objects

pickle_protocol: can be specified to override the default protocol

.. warning::

If you are using Python2, torch.save does NOT support StringIO.StringIO

as a validfile-likeobject. Thisis because the write method shouldreturn

the number of bytes written; StringIO.write() doesnot do this.

Please use something like io.BytesIO instead.

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

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

1

2

3

#b为自定义的字典

torch.save(b,'new.pkl')

net.load_state_dict(torch.load(b))

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

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

原文链接:https://blog.csdn.net/qq_34789262/article/details/83376374

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值