国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作

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

2021-10-28 09:47張林克 Python

這篇文章主要介紹了pytorch查看網(wǎng)絡參數(shù)顯存占用量等操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

1.使用torchstat

?
1
2
3
4
5
6
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

?
1
2
3
4
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)過論壇咨詢,報錯的原因找到了,只需要把

?
1
pip install torchsummary

修改為

?
1
pip install torch-summary

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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())
?
1
2
3
4
5
6
7
8
9
10
11
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ù)量

?
1
2
3
4
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}

第三方工具

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

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

?
1
2
3
4
5
6
7
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查看網(wǎng)絡參數(shù)顯存占用量等操作

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

原文鏈接:https://blog.csdn.net/weixin_45292794/article/details/108227437

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲视频综合 | 玖玖操| 久久国产精品久久久久久电车 | 国产精品视频入口 | 精品一区二区视频 | 国产精品久久久久久久久久久久久 | 大毛片| 欧美一级高清免费 | 成人精品一区二区三区 | 日本中文字幕久久 | 久久99精品国产麻豆婷婷洗澡 | 成人免费小视频 | 婷婷激情久久 | 伊人久久艹 | 九九久久精品 | 国产成人精品在线 | 亚洲成熟少妇视频在线观看 | 国产综合区 | 九九久久精品 | 国产亚洲一区二区三区在线观看 | 日韩电影免费在线观看中文字幕 | 一级毛片在线播放 | 99精品99| 精品国产精品三级精品av网址 | 欧美一区二区在线观看 | 91精品国产综合久久精品 | 国产婷婷 | 久久99精品久久久久久国产越南 | 欧美另类国产 | 91欧美激情一区二区三区成人 | 亚洲不卡在线 | 成人刺激视频在线 | 91精品国产乱码久久久久久久久 | 欧美在线视频一区 | 日韩欧美一区二区三 | 欧美成人激情 | 爱色av网址 | 亚洲免费在线播放 | 国产999精品久久久久久 | 91在线精品一区二区三区 | 中文字幕2019 |