pytorch如何查看网络参数显存占用量
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要介绍pytorch如何查看网络参数显存占用量,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!1.使用torchstatpip install torchstat fr
千家信息网最后更新 2025年11月08日pytorch如何查看网络参数显存占用量
这篇文章主要介绍pytorch如何查看网络参数显存占用量,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1.使用torchstat
pip install torchstat from torchstat import statimport torchvision.models as modelsmodel = models.resnet152()stat(model, (3, 224, 224))关于stat函数的参数,第一个应该是模型,第二个则是输入尺寸,3为通道数。我没有调研该函数的详细参数,也不知道为什么使用的时候并不提示相应的参数。
2.使用torchsummary
pip install torchsummary from torchsummary import summarysummary(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 torchimport torch.nn as nnimport torchvisionclass 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 xif __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 statimport torchvision.models as modelsmodel = models.alexnet()stat(model, (3, 224, 224))from torchvision.models import alexnetimport torchfrom thop import profilemodel = alexnet()input = torch.randn(1, 3, 224, 224)flops, params = profile(model, inputs=(input, ))print(flops, params)以上是"pytorch如何查看网络参数显存占用量"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
参数
模型
函数
占用量
显存
网络
内容
篇文章
提示
训练
输入
价值
兴趣
原因
名称
地方
小伙
小伙伴
尺寸
工具
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器丢包要怎么处理
科技中国互联网
数据库dba工资
湖南计算机网络技术对口本科专业
大数据用到的数据库教程
威海戴尔服务器拆机
通俗易懂网络技术
山西视频会议服务器多少钱
平阳高科技刀片服务器售后保障
常州赛迪网络技术有限公司
浙江优质软件开发价格
长沙 招聘 软件开发
爱推广网络技术有限公司
盐城idc服务器厂家直供
中国 低价服务器
sql2008 数据库附件
网络安全隐患的原因有哪些
每月网络安全工作报告
网络安全培训方案学校
网络技术专业专科考研
云服务器行业动态
200g服务器
数据库时间戳相减的值是秒吗
程序化软件开发
最近国家网络安全专项整治
体现了网络安全法的
高并发时序数据库
个股期权软件开发公司
北京诚信网络技术咨询常见问题
免费企业网络安全系统