pytorch獲取模型某一層參數(shù)名及參數(shù)值方式
1、Motivation:
I wanna modify the value of some param;
I wanna check the value of some param.
The needed function:
2、state_dict() #generator type
model.modules()#generator type
named_parameters()#OrderDict type
from torch import nn import torch #creat a simple model model = nn.Sequential( nn.Conv3d(1,16,kernel_size=1), nn.Conv3d(16,2,kernel_size=1))#tend to print the W of this layer input = torch.randn([1,1,16,256,256]) if torch.cuda.is_available(): print('cuda is avaliable') model.cuda() input = input.cuda() #打印某一層的參數(shù)名 for name in model.state_dict(): print(name) #Then I konw that the name of target layer is '1.weight' #schemem1(recommended) print(model.state_dict()['1.weight']) #scheme2 params = list(model.named_parameters())#get the index by debuging print(params[2][0])#name print(params[2][1].data)#data #scheme3 params = {}#change the tpye of 'generator' into dict for name,param in model.named_parameters(): params[name] = param.detach().cpu().numpy() print(params['0.weight']) #scheme4 for layer in model.modules(): if(isinstance(layer,nn.Conv3d)): print(layer.weight) #打印每一層的參數(shù)名和參數(shù)值 #schemem1(recommended) for name,param in model.named_parameters(): print(name,param) #scheme2 for name in model.state_dict(): print(name) print(model.state_dict()[name])
以上這篇pytorch獲取模型某一層參數(shù)名及參數(shù)值方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python并發(fā)爬蟲常用實(shí)現(xiàn)方法解析
這篇文章主要介紹了Python并發(fā)爬蟲常用實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Python腳本實(shí)現(xiàn)定時(shí)任務(wù)的最佳方法
我們在日常工作中,常常會(huì)用到需要周期性執(zhí)行的任務(wù),下面這篇文章主要給大家介紹了關(guān)于Python腳本實(shí)現(xiàn)定時(shí)任務(wù)的最佳方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05python檢測遠(yuǎn)程服務(wù)器tcp端口的方法
這篇文章主要介紹了python檢測遠(yuǎn)程服務(wù)器tcp端口的方法,涉及Python操作socket檢測tcp端口的技巧,需要的朋友可以參考下2015-03-03python+pygame實(shí)現(xiàn)坦克大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了python+pygame實(shí)現(xiàn)坦克大戰(zhàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Pytorch使用VGG16模型進(jìn)行預(yù)測貓狗二分類實(shí)戰(zhàn)
VGG16是Visual Geometry Group的縮寫,它的名字來源于提出該網(wǎng)絡(luò)的實(shí)驗(yàn)室,本文我們將使用PyTorch來實(shí)現(xiàn)VGG16網(wǎng)絡(luò),用于貓狗預(yù)測的二分類任務(wù),我們將對VGG16的網(wǎng)絡(luò)結(jié)構(gòu)進(jìn)行適當(dāng)?shù)男薷?以適應(yīng)我們的任務(wù),需要的朋友可以參考下2023-08-08Python?PaddleNLP開源實(shí)現(xiàn)快遞單信息抽取
這篇文章主要為大家介紹了Python?PaddleNLP開源項(xiàng)目實(shí)現(xiàn)對快遞單信息抽取,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06python 3.0 模擬用戶登錄功能并實(shí)現(xiàn)三次錯(cuò)誤鎖定
Python的3.0版本,常被稱為Python 3000,或簡稱Py3k。這篇文章主要介紹了python 3.0 模擬用戶登錄功能并實(shí)現(xiàn)三次錯(cuò)誤鎖定,需要的朋友可以參考下2017-11-11python3實(shí)現(xiàn)公眾號每日定時(shí)發(fā)送日報(bào)和圖片
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)公眾號每日定時(shí)發(fā)送日報(bào)和圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02python 使用paramiko模塊進(jìn)行封裝,遠(yuǎn)程操作linux主機(jī)的示例代碼
這篇文章主要介紹了python 使用paramiko模塊進(jìn)行封裝,遠(yuǎn)程操作linux主機(jī)的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12