pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實(shí)現(xiàn)
在將自定義的網(wǎng)絡(luò)權(quán)重加載到網(wǎng)絡(luò)中時(shí),報(bào)錯(cuò):
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()
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è)字典中了,我自己構(gòu)建的就是一個(gè)字典,沒問題!
(2)查看保存模型權(quán)重的源碼:
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.
函數(shù)功能是將字典保存為磁盤文件(二進(jìn)制數(shù)據(jù)),那么我們?cè)趖orch.load()時(shí),就是在內(nèi)存中加載二進(jìn)制數(shù)據(jù),這就是報(bào)錯(cuò)點(diǎn)。
解決方案:將字典保存為BytesIO文件之后,模型再net.load_state_dict()
#b為自定義的字典 torch.save(b,'new.pkl') net.load_state_dict(torch.load(b))
解決方法很簡單,主要記錄解決思路。
以上這篇pytorch加載自定義網(wǎng)絡(luò)權(quán)重的實(shí)現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3批量創(chuàng)建Crowd用戶并分配組
這篇文章主要介紹了Python3批量創(chuàng)建Crowd用戶并分配組,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python模塊Typing.overload的使用場(chǎng)景分析
在 Python 中,typing.overload 是一個(gè)用于定義函數(shù)重載的裝飾器,函數(shù)重載是指在一個(gè)類中可以定義多個(gè)相同名字但參數(shù)不同的函數(shù),使得在調(diào)用函數(shù)時(shí)可以根據(jù)參數(shù)的不同選擇不同的函數(shù)執(zhí)行,這篇文章主要介紹了Python模塊Typing.overload的使用,需要的朋友可以參考下2024-02-02
python機(jī)器學(xué)習(xí)案例教程——K最近鄰算法的實(shí)現(xiàn)
本篇文章主要介紹了python機(jī)器學(xué)習(xí)案例教程——K最近鄰算法的實(shí)現(xiàn),詳細(xì)的介紹了K最近鄰算法的概念和示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-12-12
Python拼接微信好友頭像大圖的實(shí)現(xiàn)方法
這篇文章主要介紹了Python拼接微信好友頭像大圖的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
python-opencv-cv2.threshold()二值化函數(shù)的使用
這篇文章主要介紹了python-opencv-cv2.threshold()二值化函數(shù)的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
python numpy實(shí)現(xiàn)文件存取的示例代碼
這篇文章主要介紹了python numpy實(shí)現(xiàn)文件存取的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python實(shí)現(xiàn)多線程并發(fā)請(qǐng)求測(cè)試的腳本
這篇文章主要為大家分享了一個(gè)Python實(shí)現(xiàn)多線程并發(fā)請(qǐng)求測(cè)試的腳本,文中的示例代碼簡潔易懂,具有一定的借鑒價(jià)值,需要的小伙伴可以了解一下2023-06-06
Python實(shí)現(xiàn)實(shí)時(shí)監(jiān)測(cè)可視化數(shù)據(jù)大屏
實(shí)時(shí)監(jiān)測(cè)的可視化數(shù)據(jù)大屏是一種非常有用的工具,可以幫助我們實(shí)時(shí)了解數(shù)據(jù)的變化和趨勢(shì),下面我們將介紹如何使用Python代碼實(shí)現(xiàn)實(shí)時(shí)監(jiān)測(cè)的可視化數(shù)據(jù)大屏,需要的可以參考一下2023-06-06

