婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > pytorch查看網絡參數顯存占用量等操作

pytorch查看網絡參數顯存占用量等操作

熱門標簽:激戰2地圖標注 廣告地圖標注app 唐山智能外呼系統一般多少錢 陜西金融外呼系統 公司電話機器人 白銀外呼系統 海南400電話如何申請 騰訊外呼線路 哈爾濱ai外呼系統定制

1.使用torchstat

pip install torchstat 

from torchstat import stat
import torchvision.models as models
model = models.resnet152()
stat(model, (3, 224, 224))

關于stat函數的參數,第一個應該是模型,第二個則是輸入尺寸,3為通道數。我沒有調研該函數的詳細參數,也不知道為什么使用的時候并不提示相應的參數。

2.使用torchsummary

pip install torchsummary
 
from torchsummary import summary
summary(model.cuda(),input_size=(3,32,32),batch_size=-1)

使用該函數直接對參數進行提示,可以發現直接有顯式輸入batch_size的地方,我自己的感覺好像該函數更好一些。但是!!!不知道為什么,該函數在我的機器上一直報錯!!!

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

Update:經過論壇咨詢,報錯的原因找到了,只需要把

pip install torchsummary

修改為

pip install torch-summary

補充:Pytorch查看模型參數并計算模型參數量與可訓練參數量

查看模型參數(以AlexNet為例)

import torch
import torch.nn as nn
import torchvision
class AlexNet(nn.Module):
    def __init__(self,num_classes=1000):
        super(AlexNet,self).__init__()
        self.feature_extraction = nn.Sequential(
            nn.Conv2d(in_channels=3,out_channels=96,kernel_size=11,stride=4,padding=2,bias=False),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3,stride=2,padding=0),
            nn.Conv2d(in_channels=96,out_channels=192,kernel_size=5,stride=1,padding=2,bias=False),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3,stride=2,padding=0),
            nn.Conv2d(in_channels=192,out_channels=384,kernel_size=3,stride=1,padding=1,bias=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=384,out_channels=256,kernel_size=3,stride=1,padding=1,bias=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=256,out_channels=256,kernel_size=3,stride=1,padding=1,bias=False),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=0),
        )
        self.classifier = nn.Sequential(
            nn.Dropout(p=0.5),
            nn.Linear(in_features=256*6*6,out_features=4096),
            nn.ReLU(inplace=True),
            nn.Dropout(p=0.5),
            nn.Linear(in_features=4096, out_features=4096),
            nn.ReLU(inplace=True),
            nn.Linear(in_features=4096, out_features=num_classes),
        )
    def forward(self,x):
        x = self.feature_extraction(x)
        x = x.view(x.size(0),256*6*6)
        x = self.classifier(x)
        return x
if __name__ =='__main__':
    # model = torchvision.models.AlexNet()
    model = AlexNet()
    
    # 打印模型參數
    #for param in model.parameters():
        #print(param)
    
    #打印模型名稱與shape
    for name,parameters in model.named_parameters():
        print(name,':',parameters.size())
feature_extraction.0.weight : torch.Size([96, 3, 11, 11])
feature_extraction.3.weight : torch.Size([192, 96, 5, 5])
feature_extraction.6.weight : torch.Size([384, 192, 3, 3])
feature_extraction.8.weight : torch.Size([256, 384, 3, 3])
feature_extraction.10.weight : torch.Size([256, 256, 3, 3])
classifier.1.weight : torch.Size([4096, 9216])
classifier.1.bias : torch.Size([4096])
classifier.4.weight : torch.Size([4096, 4096])
classifier.4.bias : torch.Size([4096])
classifier.6.weight : torch.Size([1000, 4096])
classifier.6.bias : torch.Size([1000])

計算參數量與可訓練參數量

def get_parameter_number(model):
    total_num = sum(p.numel() for p in model.parameters())
    trainable_num = sum(p.numel() for p in model.parameters() if p.requires_grad)
    return {'Total': total_num, 'Trainable': trainable_num}

第三方工具

from torchstat import stat
import torchvision.models as models
model = models.alexnet()
stat(model, (3, 224, 224))

from torchvision.models import alexnet
import torch
from thop import profile
model = alexnet()
input = torch.randn(1, 3, 224, 224)
flops, params = profile(model, inputs=(input, ))
print(flops, params)

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • pytorch固定BN層參數的操作
  • pytorch 如何自定義卷積核權值參數
  • pytorch交叉熵損失函數的weight參數的使用
  • Pytorch 統計模型參數量的操作 param.numel()
  • pytorch 一行代碼查看網絡參數總量的實現
  • pytorch 優化器(optim)不同參數組,不同學習率設置的操作
  • pytorch LayerNorm參數的用法及計算過程

標簽:常德 黑龍江 黔西 益陽 鷹潭 上海 四川 惠州

巨人網絡通訊聲明:本文標題《pytorch查看網絡參數顯存占用量等操作》,本文關鍵詞  pytorch,查看,網絡,參數,顯存,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《pytorch查看網絡參數顯存占用量等操作》相關的同類信息!
  • 本頁收集關于pytorch查看網絡參數顯存占用量等操作的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 淳安县| 朔州市| 惠安县| 高州市| 潞西市| 祁门县| 井冈山市| 鹤庆县| 四会市| 汉川市| 贡山| 旌德县| 潮安县| 潜山县| 神农架林区| 光泽县| 弥勒县| 金秀| 秦皇岛市| 松潘县| 庆元县| 公主岭市| 大余县| 宝应县| 清远市| 偃师市| 梅河口市| 美姑县| 巴林右旗| 青浦区| 邹城市| 宁津县| 阿合奇县| 台湾省| 泉州市| 夏津县| 新密市| 昌图县| 舟曲县| 澄城县| 邓州市|