欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作

 更新時間:2021年05月12日 11:11:54   作者:張林克  
這篇文章主要介紹了pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

1.使用torchstat

pip install torchstat 

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

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

2.使用torchsummary

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

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

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

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

pip install torchsummary

修改為

pip install torch-summary

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

查看模型參數(shù)(以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()
    
    # 打印模型參數(shù)
    #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])

計算參數(shù)量與可訓練參數(shù)量

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)

在這里插入圖片描述

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

相關文章

  • Python實現(xiàn)向列表或數(shù)組添加元素

    Python實現(xiàn)向列表或數(shù)組添加元素

    Python中的列表是一種動態(tài)數(shù)組,可以存儲不同數(shù)據(jù)類型的元素,并提供多種方法進行元素的添加和刪除,列表是Python中非常靈活和強大的數(shù)據(jù)結構,可以通過索引訪問、修改和操作列表中的元素,列表的創(chuàng)建十分簡單,只需使用方括號括起元素,并用逗號分隔
    2024-09-09
  • 詳解python的函數(shù)遞歸與調用

    詳解python的函數(shù)遞歸與調用

    Python中的函數(shù)遞歸是一種函數(shù)調用自身的編程技術,遞歸可以用來解決問題,特別是那些可以分解為更小、相似子問題的問題,本文將給大家詳細的講解一下python的函數(shù)遞歸與調用,需要的朋友可以參考下
    2023-10-10
  • TensorFlow模型保存/載入的兩種方法

    TensorFlow模型保存/載入的兩種方法

    這篇文章主要為大家詳細介紹了TensorFlow 模型保存/載入的兩種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Python wxpython模塊響應鼠標拖動事件操作示例

    Python wxpython模塊響應鼠標拖動事件操作示例

    這篇文章主要介紹了Python wxpython模塊響應鼠標拖動事件操作,結合實例形式分析了Python使用wxpython模塊創(chuàng)建窗口、綁定事件及相應鼠標事件相關操作技巧,需要的朋友可以參考下
    2018-08-08
  • CentOS7下python3.7.0安裝教程

    CentOS7下python3.7.0安裝教程

    這篇文章主要為大家詳細介紹了CentOS7下python3.7.0安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 通過C++學習Python

    通過C++學習Python

    這篇文章主要介紹了通過C++學習Python,通過對比分析,讓我們能夠更好的學習python.
    2015-01-01
  • python3連接MySQL8.0的兩種方式

    python3連接MySQL8.0的兩種方式

    這篇文章主要介紹了python3連接MySQL8.0的兩種方式,本文通過多種方式給大家介紹的非常詳細,代碼附有文字注釋,需要的朋友可以參考下
    2020-02-02
  • python中plt.imshow與cv2.imshow顯示顏色問題

    python中plt.imshow與cv2.imshow顯示顏色問題

    這篇文章主要介紹了plt.imshow與cv2.imshow顯示顏色問題,本文給大家介紹的非常詳細,同時給大家提到了cv2.imshow()和plt.imshow()的區(qū)別講解,需要的朋友可以參考下
    2020-07-07
  • 利用Python實現(xiàn)一個下班倒計時程序

    利用Python實現(xiàn)一個下班倒計時程序

    身為打工人,一定是想著下班的那一刻吧,這篇文章主要來和大家介紹一下如何利用Python實現(xiàn)一個下班倒計時程序,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12
  • python實現(xiàn)巡檢系統(tǒng)(solaris)示例

    python實現(xiàn)巡檢系統(tǒng)(solaris)示例

    這篇文章主要介紹了python實現(xiàn)巡檢系統(tǒng)(solaris)示例,需要的朋友可以參考下
    2014-04-04

最新評論