pytorch 求網(wǎng)絡模型參數(shù)實例
用pytorch訓練一個神經(jīng)網(wǎng)絡時,我們通常會很關心模型的參數(shù)總量。下面分別介紹來兩種方法求模型參數(shù)
一 .求得每一層的模型參數(shù),然后自然的可以計算出總的參數(shù)。
1.先初始化一個網(wǎng)絡模型model
比如我這里是 model=cliqueNet(里面是些初始化的參數(shù))
2.調(diào)用model的Parameters類獲取參數(shù)列表
一個典型的操作就是將參數(shù)列表傳入優(yōu)化器里。如下
optimizer = optim.Adam(model.parameters(), lr=opt.lr)
言歸正傳,繼續(xù)回到參數(shù)里面,參數(shù)在網(wǎng)絡里面就是variable,下面分別求每層的尺寸大小和個數(shù)。
函數(shù)get_number_of_param( ) 里面的參數(shù)就是剛才第一步初始化的model
def get_number_of_param(model): """get the number of param for every element""" count = 0 for param in model.parameters(): param_size = param.size() count_of_one_param = 1 for dis in param_size: count_of_one_param *= dis print(param.size(), count_of_one_param) count += count_of_one_param print(count) print('total number of the model is %d'%count)
再來看看結果:
torch.Size([64, 1, 3, 3]) 576 576 torch.Size([64]) 64 640 torch.Size([6, 36, 64, 3, 3]) 124416 125056 torch.Size([30, 36, 36, 3, 3]) 349920 474976 torch.Size([12, 36]) 432 475408 torch.Size([6, 36, 216, 3, 3]) 419904 895312 torch.Size([30, 36, 36, 3, 3]) 349920 1245232 torch.Size([12, 36]) 432 1245664 torch.Size([6, 36, 216, 3, 3]) 419904 1665568 torch.Size([30, 36, 36, 3, 3]) 349920 2015488 torch.Size([12, 36]) 432 2015920 torch.Size([6, 36, 216, 3, 3]) 419904 2435824 torch.Size([30, 36, 36, 3, 3]) 349920 2785744 torch.Size([12, 36]) 432 2786176 torch.Size([216, 216, 1, 1]) 46656 2832832 torch.Size([216]) 216 2833048 torch.Size([108, 216]) 23328 2856376 torch.Size([108]) 108 2856484 torch.Size([216, 108]) 23328 2879812 torch.Size([216]) 216 2880028 torch.Size([216, 216, 1, 1]) 46656 2926684 torch.Size([216]) 216 2926900 torch.Size([108, 216]) 23328 2950228 torch.Size([108]) 108 2950336 torch.Size([216, 108]) 23328 2973664 torch.Size([216]) 216 2973880 torch.Size([216, 216, 1, 1]) 46656 3020536 torch.Size([216]) 216 3020752 torch.Size([108, 216]) 23328 3044080 torch.Size([108]) 108 3044188 torch.Size([216, 108]) 23328 3067516 torch.Size([216]) 216 3067732 torch.Size([140, 280, 1, 1]) 39200 3106932 torch.Size([140]) 140 3107072 torch.Size([216, 432, 1, 1]) 93312 3200384 torch.Size([216]) 216 3200600 torch.Size([216, 432, 1, 1]) 93312 3293912 torch.Size([216]) 216 3294128 torch.Size([9, 572, 3, 3]) 46332 3340460 torch.Size([9]) 9 3340469 total number of the model is 3340469
可以通過計算驗證一下,發(fā)現(xiàn)參數(shù)與網(wǎng)絡是一致的。
二:一行代碼就可以搞定參數(shù)總個數(shù)問題
2.1 先來看看torch.tensor.numel( )這個函數(shù)的功能就是求tensor中的元素個數(shù),在網(wǎng)絡里面每層參數(shù)就是多維數(shù)組組成的tensor。
實際上就是求多維數(shù)組的元素個數(shù)??创a。
print('cliqueNet parameters:', sum(param.numel() for param in model.parameters()))
當然上面代碼中的model還是上面初始化的網(wǎng)絡模型。
看看兩種的計算結果
torch.Size([64, 1, 3, 3]) 576 576 torch.Size([64]) 64 640 torch.Size([6, 36, 64, 3, 3]) 124416 125056 torch.Size([30, 36, 36, 3, 3]) 349920 474976 torch.Size([12, 36]) 432 475408 torch.Size([6, 36, 216, 3, 3]) 419904 895312 torch.Size([30, 36, 36, 3, 3]) 349920 1245232 torch.Size([12, 36]) 432 1245664 torch.Size([6, 36, 216, 3, 3]) 419904 1665568 torch.Size([30, 36, 36, 3, 3]) 349920 2015488 torch.Size([12, 36]) 432 2015920 torch.Size([6, 36, 216, 3, 3]) 419904 2435824 torch.Size([30, 36, 36, 3, 3]) 349920 2785744 torch.Size([12, 36]) 432 2786176 torch.Size([216, 216, 1, 1]) 46656 2832832 torch.Size([216]) 216 2833048 torch.Size([108, 216]) 23328 2856376 torch.Size([108]) 108 2856484 torch.Size([216, 108]) 23328 2879812 torch.Size([216]) 216 2880028 torch.Size([216, 216, 1, 1]) 46656 2926684 torch.Size([216]) 216 2926900 torch.Size([108, 216]) 23328 2950228 torch.Size([108]) 108 2950336 torch.Size([216, 108]) 23328 2973664 torch.Size([216]) 216 2973880 torch.Size([216, 216, 1, 1]) 46656 3020536 torch.Size([216]) 216 3020752 torch.Size([108, 216]) 23328 3044080 torch.Size([108]) 108 3044188 torch.Size([216, 108]) 23328 3067516 torch.Size([216]) 216 3067732 torch.Size([140, 280, 1, 1]) 39200 3106932 torch.Size([140]) 140 3107072 torch.Size([216, 432, 1, 1]) 93312 3200384 torch.Size([216]) 216 3200600 torch.Size([216, 432, 1, 1]) 93312 3293912 torch.Size([216]) 216 3294128 torch.Size([9, 572, 3, 3]) 46332 3340460 torch.Size([9]) 9 3340469 total number of the model is 3340469 cliqueNet parameters: 3340469
可以看出兩種計算出來的是一模一樣的。
以上這篇pytorch 求網(wǎng)絡模型參數(shù)實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
對matplotlib改變colorbar位置和方向的方法詳解
今天小編就為大家分享一篇對matplotlib改變colorbar位置和方向的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python使用socket模塊實現(xiàn)簡單tcp通信
這篇文章主要介紹了Python使用socket模塊實現(xiàn)簡單tcp通信,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08anaconda3:conda not found報錯問題解決
這篇文章主要給大家介紹了關于anaconda3:conda not found報錯問題解決的相關資料,Anaconda指的是一個開源的Python發(fā)行版本,其包含了conda、Python等180多個科學包及其依賴項,需要的朋友可以參考下2023-10-10Python實戰(zhàn)小程序利用matplotlib模塊畫圖代碼分享
這篇文章主要介紹了Python實戰(zhàn)小程序利用matplotlib模塊畫圖代碼分享,具有一定借鑒價值,需要的朋友可以了解下。2017-12-12