Pytorch统计参数网络参数数量方式

 更新时间:2023年02月20日 10:09:39   作者:qq_34535410  
这篇文章主要介绍了Pytorch统计参数网络参数数量方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Pytorch统计参数网络参数数量

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}

Pytorch如何计算网络的参数量

本文以 Dense Block 为例,Pytorch 为 DL 框架,最终计算模块参数量方法如下:

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)))

最终结果如下

Norm Conv parameter count is 443520
DenseBlock Norm parameter count is 885888

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python datacompy 找出两个DataFrames不同的地方

    Python datacompy 找出两个DataFrames不同的地方

    本文主要介绍了Python datacompy 找出两个DataFrames不同的地方,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧<BR>
    2022-05-05
  • Pytorch distributed 多卡并行载入模型操作

    Pytorch distributed 多卡并行载入模型操作

    这篇文章主要介绍了Pytorch distributed 多卡并行载入模型操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Python使用xlrd实现读取合并单元格

    Python使用xlrd实现读取合并单元格

    这篇文章主要介绍了Python使用xlrd实现读取合并单元格,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • python issubclass 和 isinstance函数

    python issubclass 和 isinstance函数

    这篇文章主要介绍了python issubclass 和 isinstance函数,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-07-07
  • Python基础知识方法重写+文件处理+异常处理

    Python基础知识方法重写+文件处理+异常处理

    这篇文章主要介绍了Python基础知识方法重写+文件处理+异常处理,这是基础知识分享的第四篇,看到这里了相信大家前几篇都学得还不错吧,下面我们继续巩固Python基础知识,需要的朋友也可以参考一下
    2022-05-05
  • Python定时任务框架APScheduler安装使用详解

    Python定时任务框架APScheduler安装使用详解

    这篇文章主要介绍了Python定时任务框架APScheduler安装使用详解,重点介绍如何使用APscheduler实现python定时任务,本文通过实例代码给大家介绍的非常详细,对Python定时任务APScheduler相关知识感兴趣的朋友一起看看吧
    2022-05-05
  • Python实现图像手绘效果的方法详解

    Python实现图像手绘效果的方法详解

    这篇文章主要为大家详细介绍了如何利用Python语言实现图像手绘效果,文中的示例代码讲解详细,具有一定的借鉴价值,需要的可以参考一下
    2022-09-09
  • python 实现图像快速替换某种颜色

    python 实现图像快速替换某种颜色

    这篇文章主要介绍了python 实现图像快速替换某种颜色,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • python基于gevent实现并发下载器代码实例

    python基于gevent实现并发下载器代码实例

    这篇文章主要介绍了python基于gevent实现并发下载器代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 解决Pyinstaller打包软件失败的一个坑

    解决Pyinstaller打包软件失败的一个坑

    这篇文章主要介绍了解决Pyinstaller打包软件失败的一个坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03

最新评论