In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Pytorch how to count the number of model parameters, the article is very detailed, has a certain reference value, interested friends must read it!
Pytorch statistics of the number of model parameters can be achieved using param.numel ().
Param.numel ()
Returns the number of elements in param
Statistical model parameters num_params = sum (param.numel () for param in net.parameters ()) print (num_params)
Supplement: Pytorch view model parameters
Pytorch view model parameters
Check the parameters of building a model using Pytorch, and look directly at the program.
Import torch# introduces torch.nn and specifies aliases import torch.nn as nnimport torch.nn.functional as Fclass Net (nn.Module): def _ _ init__ (self): # functions of the nn.Module subclass must execute the constructor super (Net, self) of the parent class in the constructor. _ _ init__ () # convolution layer'1' indicates that the input image is a single channel. '6' indicates the number of output channels '3' indicates that the convolution kernel is 3: 3 self.conv1 = nn.Conv2d (1, 6, 3) # linear layer, input 1350 features, output 10 features self.fc1 = nn.Linear (1350, 10) # how is the 1350 calculated here? This depends on the following forward function # forward propagation def forward (self, x): print (x.size ()) # result: [1, 1, 32, 32] # convolution-> activation-> pooling x = self.conv1 (x) # according to the convolution size formula, the result is 30 The convolution neural network in section 4 of the second section of the specific calculation formula is introduced in detail. X = F.relu (x) print (x.size ()) # result: [1,6,30,30] x = F.max_pool2d (x, (2,2)) # We use pooled layer, and the result is 15 x = F.relu (x) print (x.size ()) # result: [1,6,15,15] # reshape '- 1' means adaptive # what is done here is to flatten the back [1, 6, 15, 15] Change to [1, 1350] x = x.view (x.size () [0],-1) print (x.size ()) # here is the input of the fc1 layer 1350 x = self.fc1 (x) return xnet = Net () for parameters in net.parameters (): print (parameters)
The output is:
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 ())
Output:
Conv1.weight: torch.Size ([6,1,3,3])
Conv1.bias: torch.Size ([6])
Fc1.weight: torch.Size ([10,1350])
Fc1.bias: torch.Size ([10])
The above is all the contents of the article "how to count Model parameters by Pytorch". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 295
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.