一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實現(xiàn)

pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實現(xiàn)

2020-05-06 09:20wuming無名 Python

今天小編就為大家分享一篇pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在將自定義的網(wǎng)絡(luò)權(quán)重加載到網(wǎng)絡(luò)中時,報錯:

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.

我們一步一步分析。

模型網(wǎng)絡(luò)權(quán)重保存額代碼是:torch.save(net.state_dict(),'net.pkl')

(1)查看獲取模型權(quán)重的源碼:

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']
 
  """

將網(wǎng)絡(luò)中所有的狀態(tài)保存到一個字典中了,我自己構(gòu)建的就是一個字典,沒問題!

(2)查看保存模型權(quán)重的源碼:

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 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.

函數(shù)功能是將字典保存為磁盤文件(二進制數(shù)據(jù)),那么我們在torch.load()時,就是在內(nèi)存中加載二進制數(shù)據(jù),這就是報錯點。

解決方案:將字典保存為BytesIO文件之后,模型再net.load_state_dict()

?
1
2
3
#b為自定義的字典
torch.save(b,'new.pkl')
net.load_state_dict(torch.load(b))

解決方法很簡單,主要記錄解決思路。

以上這篇pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青草国产精品久久久久 | 男人晚上适合偷偷看的污污 | 91香蕉国产| 被强上后我成瘾了小说 | h片免费网站 | 久久精品一卡二卡三卡四卡视频版 | 无码精品一区二区三区免费视频 | 美女把小内内脱个精光打屁屁 | 日本三级做a全过程在线观看 | 亚洲 欧美 制服 校园 动漫 | 国产成人看片免费视频观看 | 国产伦精品一区二区三区免费观看 | 99精品国产成人一区二区在线 | 色字当头| 女人爽到喷水的视频免费 | 国模娜娜a4u1546全套 | 办公室大战秘书呻吟 | 扒开双腿猛进入爽爽视频ai | 欧美一卡2卡3卡无卡 | 男人日女人p | 牧教师在线观看 | 亚洲成年网站在线观看 | 海角社区在线登录 | 风间由美被义子中文字幕 | 无人视频在线观看完整版高清 | 99re热这里只有精品 | 啊好爽视频 | 亚洲欧美国产另类 | 日本中文字幕在线精品 | 性柔术18性13处交 | 日本在线观看免费观看完整版 | 免费观看国产视频 | 色老汉| 精品一区二区三区免费视频 | 天堂一区二区在线观看 | 亚洲女同在线观看 | 亚洲AV无码国产精品色在线看 | 国产精品欧美亚洲韩国日本 | 欧美xxxbrazzers | 日韩一级精品视频在线观看 | 国产剧情在线播放 |