您好,登錄后才能下訂單哦!
這篇文章主要介紹了Pytorch怎么統(tǒng)計參數(shù)網(wǎng)絡(luò)參數(shù)數(shù)量的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Pytorch怎么統(tǒng)計參數(shù)網(wǎng)絡(luò)參數(shù)數(shù)量文章都會有所收獲,下面我們一起來看看吧。
def get_parameter_number(net): total_num = sum(p.numel() for p in net.parameters()) trainable_num = sum(p.numel() for p in net.parameters() if p.requires_grad) return {'Total': total_num, 'Trainable': trainable_num}
本文以 Dense Block 為例,Pytorch 為 DL 框架,最終計算模塊參數(shù)量方法如下:
import torch import torch.nn as nn class Norm_Conv(nn.Module): def __init__(self,in_channel): super(Norm_Conv,self).__init__() self.layers = nn.Sequential( nn.Conv2d(in_channel,in_channel,3,1,1), nn.ReLU(True), nn.BatchNorm2d(in_channel), nn.Conv2d(in_channel,in_channel,3,1,1), nn.ReLU(True), nn.BatchNorm2d(in_channel), nn.Conv2d(in_channel,in_channel,3,1,1), nn.ReLU(True), nn.BatchNorm2d(in_channel)) def forward(self,input): out = self.layers(input) return out class DenseBlock_Norm(nn.Module): def __init__(self,in_channel): super(DenseBlock_Norm,self).__init__() self.first_layer = nn.Sequential(nn.Conv2d(in_channel,in_channel,3,1,1), nn.ReLU(True), nn.BatchNorm2d(in_channel)) self.second_layer = nn.Sequential(nn.Conv2d(in_channel*2,in_channel,3,1,1), nn.ReLU(True), nn.BatchNorm2d(in_channel)) self.third_layer = nn.Sequential( nn.Conv2d(in_channel*3,in_channel,3,1,1), nn.ReLU(True), nn.BatchNorm2d(in_channel)) def forward(self,input): output1 = self.first_layer(input) output2 = self.second_layer(torch.cat((output1,input),dim=1)) output3 = self.third_layer(torch.cat((input,output1,output2),dim=1)) return output3 def count_param(model): param_count = 0 for param in model.parameters(): param_count += param.view(-1).size()[0] return param_count # Get Parameter number of Network in_channel = 128 net1 = Norm_Conv(in_channel) print('Norm Conv parameter count is {}'.format(count_param(net1))) net2 = DenseBlock_Norm(in_channel) print('DenseBlock Norm parameter count is {}'.format(count_param(net2)))
最終結(jié)果如下
Norm Conv parameter count is 443520
DenseBlock Norm parameter count is 885888
關(guān)于“Pytorch怎么統(tǒng)計參數(shù)網(wǎng)絡(luò)參數(shù)數(shù)量”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“Pytorch怎么統(tǒng)計參數(shù)網(wǎng)絡(luò)參數(shù)數(shù)量”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。