App下載

Pytorch 統(tǒng)計(jì)模型參數(shù)量的操作 param.numel()

嘴角的櫻桃汁 2021-08-16 11:37:55 瀏覽數(shù) (3923)
反饋

pytorch統(tǒng)計(jì)模型參數(shù)量可以使用param.numel()來實(shí)現(xiàn),接下來的這篇文章我們就來看看到底怎么實(shí)現(xiàn)吧。

param.numel()

返回param中元素的數(shù)量

運(yùn)行結(jié)果

統(tǒng)計(jì)模型參數(shù)量

num_params = sum(param.numel() for param in net.parameters())
print(num_params)

補(bǔ)充:Pytorch 查看模型參數(shù)

Pytorch 查看模型參數(shù)

查看利用Pytorch搭建模型的參數(shù),直接看程序

import torch
# 引入torch.nn并指定別名
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        # nn.Module子類的函數(shù)必須在構(gòu)造函數(shù)中執(zhí)行父類的構(gòu)造函數(shù)
        super(Net, self).__init__()
        
        # 卷積層 '1'表示輸入圖片為單通道, '6'表示輸出通道數(shù),'3'表示卷積核為3*3
        self.conv1 = nn.Conv2d(1, 6, 3) 
        #線性層,輸入1350個(gè)特征,輸出10個(gè)特征
        self.fc1   = nn.Linear(1350, 10)  #這里的1350是如何計(jì)算的呢?這就要看后面的forward函數(shù)
    #正向傳播 
    def forward(self, x): 
        print(x.size()) # 結(jié)果:[1, 1, 32, 32]
        # 卷積 -> 激活 -> 池化 
        x = self.conv1(x) #根據(jù)卷積的尺寸計(jì)算公式,計(jì)算結(jié)果是30,具體計(jì)算公式后面第二張第四節(jié) 卷積神經(jīng)網(wǎng)絡(luò) 有詳細(xì)介紹。
        x = F.relu(x)
        print(x.size()) # 結(jié)果:[1, 6, 30, 30]
        x = F.max_pool2d(x, (2, 2)) #我們使用池化層,計(jì)算結(jié)果是15
        x = F.relu(x)
        print(x.size()) # 結(jié)果:[1, 6, 15, 15]
        # reshape,‘-1'表示自適應(yīng)
        #這里做的就是壓扁的操作 就是把后面的[1, 6, 15, 15]壓扁,變?yōu)?[1, 1350]
        x = x.view(x.size()[0], -1) 
        print(x.size()) # 這里就是fc1層的的輸入1350 
        x = self.fc1(x)        
        return x

net = Net()
for parameters in net.parameters():
    print(parameters)

輸出為:

Parameter containing:
tensor([[[[-0.0104, -0.0555, 0.1417],
[-0.3281, -0.0367, 0.0208],
[-0.0894, -0.0511, -0.1253]]],


[[[-0.1724, 0.2141, -0.0895],
[ 0.0116, 0.1661, -0.1853],
[-0.1190, 0.1292, -0.2451]]],


[[[ 0.1827, 0.0117, 0.2880],
[ 0.2412, -0.1699, 0.0620],
[ 0.2853, -0.2794, -0.3050]]],


[[[ 0.1930, 0.2687, -0.0728],
[-0.2812, 0.0301, -0.1130],
[-0.2251, -0.3170, 0.0148]]],


[[[-0.2770, 0.2928, -0.0875],
[ 0.0489, -0.2463, -0.1605],
[ 0.1659, -0.1523, 0.1819]]],


[[[ 0.1068, 0.2441, 0.3160],
[ 0.2945, 0.0897, 0.2978],
[ 0.0419, -0.0739, -0.2609]]]])
Parameter containing:
tensor([ 0.0782, 0.2679, -0.2516, -0.2716, -0.0084, 0.1401])
Parameter containing:
tensor([[ 1.8612e-02, 6.5482e-03, 1.6488e-02, ..., -1.3283e-02,
1.8715e-02, 5.4037e-03],
[ 1.8569e-03, 1.8022e-02, -2.3465e-02, ..., 1.6527e-03,
2.0443e-02, -2.2009e-02],
[ 9.9104e-03, 6.6134e-03, -2.7171e-02, ..., -5.7119e-03,
2.4532e-02, 2.2284e-02],
...,
[ 6.9182e-03, 1.7279e-02, -1.7783e-03, ..., 1.9354e-02,
2.1105e-03, 8.6245e-03],
[ 1.6877e-02, -1.2414e-02, 2.2409e-02, ..., -2.0604e-02,
1.3253e-02, -3.6008e-03],
[-2.1598e-02, 2.5892e-02, 1.9372e-02, ..., 1.4159e-02,
7.0983e-03, -2.3713e-02]])
Parameter containing:
tensor(1.00000e-02 *
[ 1.4703, 1.0289, 2.5069, -2.2603, -1.5218, -1.7019, 1.2569,
0.4617, -2.3082, -0.6282])

for name,parameters in net.named_parameters():
    print(name,':',parameters.size())

輸出:

conv1.weight : torch.Size([6, 1, 3, 3])
conv1.bias : torch.Size([6])
fc1.weight : torch.Size([10, 1350])
fc1.bias : torch.Size([10])

以上就是pytorch使用param.numel()來統(tǒng)計(jì)模型參數(shù)量的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持W3Cschool


0 人點(diǎn)贊