Python Gluon參數(shù)和模塊命名操作教程
本文實(shí)例講述了Python Gluon參數(shù)和模塊命名操作。分享給大家供大家參考,具體如下:
Gluon參數(shù)和模塊命名教程
在gluon里,每個(gè)參數(shù)和塊都有一個(gè)名字(和前綴)。參數(shù)名可以由用戶指定,block名也可以由用戶指定,也可以自動(dòng)創(chuàng)建。
本教程中,我們將討論命名方面的最佳實(shí)踐。首先,import MXNet和Gluon
from __future__ import print_function import mxnet as mx from mxnet import gluon
Blocks命名
在創(chuàng)建block時(shí),可以指定一個(gè)前綴給它:
mydense = gluon.nn.Dense(100, prefix='mydense_') print(mydense.prefix) mydense_
若沒(méi)有指定前綴,gluon會(huì)自動(dòng)生成一個(gè)前綴
dense0 = gluon.nn.Dense(100) print(dense0.prefix) dense0_
當(dāng)你創(chuàng)建更多同類塊時(shí),它們將遞增后綴命名,以避免沖突:
dense1 = gluon.nn.Dense(100) print(dense1.prefix) dense1_
參數(shù)命名
blocks中的參數(shù)將用過(guò)將block的前綴添加到參數(shù)的名稱來(lái)命名:
print(dense0.collect_params()) dense0_ ( Parameter dense0_weight (shape=(100, 0), dtype=<type 'numpy.float32'>) Parameter dense0_bias (shape=(100,), dtype=<type 'numpy.float32'>) )
名稱空間
為了管理嵌套block的名稱,每個(gè)塊附加有一個(gè)name_scope(名稱空間)。在name_scope中創(chuàng)建的block都會(huì)在其名稱前加上父block的名稱。
我們將定義一個(gè)簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)來(lái)說(shuō)明這點(diǎn):
class Model(gluon.Block): def __init__(self, **kwargs): super(Model, self).__init__(**kwargs) with self.name_scope(): self.dense0 = gluon.nn.Dense(20) self.dense1 = gluon.nn.Dense(20) self.mydense = gluon.nn.Dense(20, prefix='mydense_') def forward(self, x): x = mx.nd.relu(self.dense0(x)) x = mx.nd.relu(self.dense1(x)) return mx.nd.relu(self.mydense(x))
現(xiàn)在實(shí)例化這個(gè)神經(jīng)網(wǎng)絡(luò)
- 注意:
model0.dense0
的名稱是model0_dense0_
而非dense0_
- 注意:我們指定
model.mydense
的前綴為mydense_
,它的父類前綴會(huì)自動(dòng)生成并添加到前面變成model0_mydense_
這里的名稱前綴和變量名model0沒(méi)有關(guān)系,這里就算把model0換成其他變量名比如net,前綴還是model?,? 表示這是一個(gè)遞增的數(shù)字,這里的名稱前綴和
class Model
有關(guān) 若將類名Model換成Hodel,那么后面的前綴都會(huì)變成 hodel?
model0 = Model() model0.initialize() model0(mx.nd.zeros((1, 20))) print(model0.prefix) print(model0.dense0.prefix) print(model0.dense1.prefix) print(model0.mydense.prefix) model0_ model0_dense0_ model0_dense1_ model0_mydense_
若我們?cè)俅螌?shí)例化Model
,在Dense
前會(huì)生成一個(gè)不同的名稱。
- 注意:
model1.dense0
的名稱依然是dense0_
而非dense2_
,遵循之前在model0
中創(chuàng)建的dense層的命名規(guī)則。這是因?yàn)槊總€(gè)model的命名空間是相互獨(dú)立
model1 = Model() print(model1.prefix) print(model1.dense0.prefix) print(model1.dense1.prefix) print(model1.mydense.prefix) model1_ model1_dense0_ model1_dense1_ model1_mydense_
建議手動(dòng)為頂層的model指定一個(gè)前綴,即model = Model(prefix=‘mymodel_'),以避免命名時(shí)可能出現(xiàn)的混淆。
相同的規(guī)則同樣適用于像Sequential這類容器block.name_scope
既可以在__init__
內(nèi)使用,也可以在__init__
外使用:
注意:這里
Sequential
也有參數(shù)prefix,是可以自己指定名稱的,不指定的話就叫Sequential
net = gluon.nn.Sequential() with net.name_scope(): net.add(gluon.nn.Dense(20)) net.add(gluon.nn.Dense(20)) print(net.prefix) print(net[0].prefix) print(net[1].prefix) sequential0_ sequential0_dense0_ sequential0_dense1_
gluon.model_zoo
也一樣
net = gluon.nn.Sequential() with net.name_scope(): net.add(gluon.model_zoo.vision.alexnet(pretrained=True)) net.add(gluon.model_zoo.vision.alexnet(pretrained=True)) print(net.prefix, net[0].prefix, net[1].prefix) sequential1_ sequential1_alexnet0_ sequential1_alexnet1_
保存和載入
由于model0和model1有不同的前綴,所以它們的參數(shù)是有不同名字的:
print(model0.collect_params(), '\n') print(model1.collect_params()) model0_ ( Parameter model0_dense0_weight (shape=(20L, 20L), dtype=<type 'numpy.float32'>) Parameter model0_dense0_bias (shape=(20L,), dtype=<type 'numpy.float32'>) Parameter model0_dense1_weight (shape=(20L, 20L), dtype=<type 'numpy.float32'>) Parameter model0_dense1_bias (shape=(20L,), dtype=<type 'numpy.float32'>) Parameter model0_mydense_weight (shape=(20L, 20L), dtype=<type 'numpy.float32'>) Parameter model0_mydense_bias (shape=(20L,), dtype=<type 'numpy.float32'>) ) model1_ ( Parameter model1_dense0_weight (shape=(20, 0), dtype=<type 'numpy.float32'>) Parameter model1_dense0_bias (shape=(20,), dtype=<type 'numpy.float32'>) Parameter model1_dense1_weight (shape=(20, 0), dtype=<type 'numpy.float32'>) Parameter model1_dense1_bias (shape=(20,), dtype=<type 'numpy.float32'>) Parameter model1_mydense_weight (shape=(20, 0), dtype=<type 'numpy.float32'>) Parameter model1_mydense_bias (shape=(20,), dtype=<type 'numpy.float32'>) )
若你嘗試將model0的參數(shù)載入到model1中,你將會(huì)得到一個(gè)名稱不匹配的錯(cuò)誤
model0.collect_params().save('model.params') try: model1.collect_params().load('model.params', mx.cpu()) except Exception as e: print(e)
Parameter 'model1_dense0_weight' is missing in file 'model.params', which contains parameters: 'model0_mydense_weight', 'model0_dense1_bias', 'model0_dense1_weight', 'model0_dense0_weight', 'model0_dense0_bias', 'model0_mydense_bias'. Please make sure source and target networks have the same prefix.
為了解決這個(gè)問(wèn)題,我們使用save_parameters
/load_parameters
而不是 collect_params
和save
/load. save_parameters
。使用模型結(jié)構(gòu)而非參數(shù)名稱來(lái)匹配參數(shù)。
model0.save_parameters('model.params') model1.load_parameters('model.params') print(mx.nd.load('model.params').keys()) ['dense0.bias', 'mydense.bias', 'dense1.bias', 'dense1.weight', 'dense0.weight', 'mydense.weight']
替換網(wǎng)絡(luò)中的block并進(jìn)行fine-turning
有時(shí)需要加載一些預(yù)訓(xùn)練的模型,并替換其中某些block并進(jìn)行fine-turning。
For example, the alexnet in model zoo has 1000 output dimensions, but maybe you only have 100 classes in your application.
例如,alexnet有1000個(gè)輸出維度但你只有100類。
我們首先載入預(yù)訓(xùn)練的AlexNet
- 在Gluon Model Zoo,所有圖像分類模型的格式都是特征提取層叫
features
,輸出層叫output
. - 注意到輸出層是一個(gè)dense block,有1000個(gè)維度的輸出
alexnet = gluon.model_zoo.vision.alexnet(pretrained=True) print(alexnet.output) print(alexnet.output.prefix) Dense(4096 -> 1000, linear) alexnet0_dense2_
改變輸出為100維,使用一個(gè)新block替換它
with alexnet.name_scope(): alexnet.output = gluon.nn.Dense(100) alexnet.output.initialize() print(alexnet.output) print(alexnet.output.prefix) Dense(None -> 100, linear) alexnet0_dense3_
原文:http://mxnet.incubator.apache.org/versions/master/tutorials/gluon/naming.html
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python使用gluon/mxnet模塊實(shí)現(xiàn)的mnist手寫(xiě)數(shù)字識(shí)別功能完整示例
- Python用imghdr模塊識(shí)別圖片格式實(shí)例解析
- Python基于whois模塊簡(jiǎn)單識(shí)別網(wǎng)站域名及所有者的方法
- python字符串編碼識(shí)別模塊chardet簡(jiǎn)單應(yīng)用
- 使用Python的OpenCV模塊識(shí)別滑動(dòng)驗(yàn)證碼的缺口(推薦)
- Python用sndhdr模塊識(shí)別音頻格式詳解
- python使用magic模塊進(jìn)行文件類型識(shí)別方法
- python實(shí)現(xiàn)識(shí)別相似圖片小結(jié)
- python實(shí)現(xiàn)人臉識(shí)別代碼
相關(guān)文章
Python中Pytest測(cè)試框架的fixture使用詳解
這篇文章主要介紹了Python中Pytest測(cè)試框架的fixture使用詳解,Pytest的fixture的目的是提供一個(gè)測(cè)試的基線,在此基線基礎(chǔ)上,可以更可靠的進(jìn)行重復(fù)測(cè)試,需要的朋友可以參考下2023-08-08python爬蟲(chóng)看看虎牙女主播中誰(shuí)最“頂”步驟詳解
這篇文章主要介紹了python爬蟲(chóng)看看虎牙女主播中誰(shuí)最“頂”,本文分步驟通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Django?ORM?事務(wù)和查詢優(yōu)化的操作方法
這篇文章主要介紹了Django?ORM?事務(wù)和查詢優(yōu)化,包括事務(wù)操作、ORM 惰性查詢及only與defer相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09Pygame游戲開(kāi)發(fā)之太空射擊實(shí)戰(zhàn)敵人精靈篇
相信大多數(shù)8090后都玩過(guò)太空射擊游戲,在過(guò)去游戲不多的年代太空射擊自然屬于經(jīng)典好玩的一款了,今天我們來(lái)自己動(dòng)手實(shí)現(xiàn)它,在編寫(xiě)學(xué)習(xí)中回顧過(guò)往展望未來(lái),下面開(kāi)始講解敵人精靈的使用2022-08-08Django關(guān)于事務(wù)transaction.atomic()的使用方式
這篇文章主要介紹了Django關(guān)于事務(wù)transaction.atomic()的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Django Sitemap 站點(diǎn)地圖的實(shí)現(xiàn)方法
這篇文章主要介紹了Django Sitemap 站點(diǎn)地圖的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04通過(guò)Python編寫(xiě)一個(gè)簡(jiǎn)單登錄功能過(guò)程解析
這篇文章主要介紹了通過(guò)Python編寫(xiě)一個(gè)簡(jiǎn)單登錄功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09