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

作者:qq_34535410 时间:2021-03-13 03:09:04 

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

来源:https://blog.csdn.net/qq_34535410/article/details/89715192

标签:Pytorch,统计参数,网络参数,数量
0
投稿

猜你喜欢

  • python numpy中cumsum的用法详解

    2022-09-17 03:21:17
  • Python3.5字符串常用操作实例详解

    2023-08-31 00:25:53
  • asp之自动闭合HTML/ubb标签函数附简单注释

    2011-04-04 11:18:00
  • 网页特效文字之—压纹字

    2023-06-26 19:30:06
  • JavaScript缓动库

    2009-05-25 12:50:00
  • Go-ethereum 解析ethersjs中产生的签名信息思路详解

    2023-08-05 21:34:49
  • 通过屏蔽IP来防止采集

    2007-08-19 15:28:00
  • Python Map 函数详解

    2022-12-28 16:17:09
  • Python实现自动装机功能案例分析

    2022-05-16 12:35:48
  • asp如何将数字转化成条形图?

    2009-12-03 20:19:00
  • 详解python中的生成器、迭代器、闭包、装饰器

    2023-06-25 19:39:57
  • ASP应用:用stream读文件

    2007-09-24 13:33:00
  • javascript面向对象三大特征之封装实例详解

    2023-08-23 21:39:04
  • selenium 多窗口切换的实现(windows)

    2021-08-17 03:58:54
  • Python实现的计算器功能示例

    2023-02-16 22:25:30
  • Pytorch自动求导函数详解流程以及与TensorFlow搭建网络的对比

    2023-07-08 18:44:37
  • Python最长公共子串算法实例

    2022-08-11 01:29:57
  • python中的装饰器该如何使用

    2021-01-17 20:40:45
  • asp经典入门教程 在ASP中使用SQL 语句

    2013-06-01 20:23:21
  • 返回首页的链接地址写法

    2008-10-22 13:38:00
  • asp之家 网络编程 m.aspxhome.com