pytorch中的numel函数用法说明

时间:2022-12-01 12:29:33

获取tensor中一共包含多少个元素

?
1
2
3
4
5
import torch
x = torch.randn(3,3)
print("number elements of x is ",x.numel())
y = torch.randn(3,10,5)
print("number elements of y is ",y.numel())

输出:

number elements of x is 9

number elements of y is 150

27和150分别位x和y中各有多少个元素或变量

补充:pytorch获取张量元素个数numel()的用法

numel就是"number of elements"的简写。

numel()可以直接返回int类型的元素个数

?
1
2
3
4
5
import torch
a = torch.randn(1, 2, 3, 4)
b = a.numel()
print(type(b)) # int
print(b) # 24

通过numel()函数,我们可以迅速查看一个张量到底又多少元素。

补充:pytorch 卷积结构和numel()函数

看代码吧~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from torch import nn
class CNN(nn.Module):
    def __init__(self, num_channels=1, d=56, s=12, m=4):
        super(CNN, self).__init__()
        self.first_part = nn.Sequential(
            nn.Conv2d(num_channels, d, kernel_size=3, padding=5//2),
            nn.Conv2d(num_channels, d, kernel_size=(1,3), padding=5//2),
            nn.Conv2d(num_channels, d, kernel_size=(3,1), padding=5//2),
            nn.PReLU(d)
        )
 
    def forward(self, x):
        x = self.first_part(x)
        return x
 
model = CNN()
for m in model.first_part:
    if isinstance(m, nn.Conv2d):
        # print('m:',m.weight.data)
        print('m:',m.weight.data[0])
        print('m:',m.weight.data[0][0])
        print('m:',m.weight.data.numel()) #numel() 计算矩阵中元素的个数
 
结果:
m: tensor([[[-0.28220.0128, -0.0244],
         [-0.23290.10370.2262],
         [ 0.2845, -0.30940.1443]]]) #卷积核大小为3x3
m: tensor([[-0.28220.0128, -0.0244],
        [-0.23290.10370.2262],
        [ 0.2845, -0.30940.1443]]) #卷积核大小为3x3
m: 504   # = 56 x (3 x 3)  输出通道数为56,卷积核大小为3x3
m: tensor([-0.03350.29450.25120.27700.20710.1133, -0.18830.2738,
         0.08050.1339, -0.3000, -0.1911, -0.17600.2855, -0.0234, -0.0843,
         0.18150.23570.27580.2689, -0.2477, -0.2528, -0.1447, -0.0903,
         0.18700.0945, -0.2786, -0.04190.1577, -0.3100, -0.1335, -0.3162,
        -0.15700.30800.09510.19530.1814, -0.19360.1466, -0.2911,
        -0.12860.30240.1143, -0.0726, -0.2694, -0.32300.2031, -0.2963,
         0.29650.2525, -0.26740.0564, -0.32770.2185, -0.04760.0558]) bias偏置的值
m: tensor([[[ 0.5747, -0.34210.2847]]]) 卷积核大小为1x3
m: tensor([[ 0.5747, -0.34210.2847]]) 卷积核大小为1x3
m: 168 # = 56 x (1 x 3) 输出通道数为56,卷积核大小为1x3
m: tensor([ 0.5328, -0.5711, -0.19450.28440.2012, -0.00840.4834, -0.2020,
        -0.09410.4683, -0.23860.2781, -0.1812, -0.2990, -0.46520.1228,
        -0.06270.3112, -0.27000.08250.4345, -0.0373, -0.3220, -0.5038,
        -0.3166, -0.38230.3947, -0.32320.10280.23780.45890.1675,
        -0.3112, -0.0905, -0.07050.27630.54330.2768, -0.38040.4855,
        -0.4880, -0.45550.41430.54740.3305, -0.03810.24830.5133,
        -0.39780.04070.23510.1910, -0.53850.13400.1811, -0.3008]) bias偏置的值
m: tensor([[[0.0184],
         [0.0981],
         [0.1894]]]) 卷积核大小为3x1
m: tensor([[0.0184],
        [0.0981],
        [0.1894]]) 卷积核大小为3x1
m: 168 # = 56 x (3 x 1) 输出通道数为56,卷积核大小为3x1
m: tensor([-0.2951, -0.44750.13010.4747, -0.05120.21900.3533, -0.1158,
         0.2237, -0.1407, -0.47560.1637, -0.4555, -0.21570.0577, -0.3366,
        -0.32520.28070.16600.2949, -0.2886, -0.52160.16650.2193,
         0.2038, -0.13570.26260.20360.32550.27560.1283, -0.4909,
         0.5737, -0.4322, -0.4930, -0.08460.21580.55650.3751, -0.3775,
        -0.5096, -0.45200.2246, -0.53670.55310.3372, -0.5593, -0.2780,
        -0.5453, -0.28630.5712, -0.28820.47880.3222, -0.48460.2170]) bias偏置的值
  
'''初始化后'''
class CNN(nn.Module):
    def __init__(self, num_channels=1, d=56, s=12, m=4):
        super(CNN, self).__init__()
        self.first_part = nn.Sequential(
            nn.Conv2d(num_channels, d, kernel_size=3, padding=5//2),
            nn.Conv2d(num_channels, d, kernel_size=(1,3), padding=5//2),
            nn.Conv2d(num_channels, d, kernel_size=(3,1), padding=5//2),
            nn.PReLU(d)
        )
        self._initialize_weights()
    def _initialize_weights(self):
        for m in self.first_part:
            if isinstance(m, nn.Conv2d):
                nn.init.normal_(m.weight.data, mean=0.0, std=math.sqrt(2/(m.out_channels*m.weight.data[0][0].numel())))
                nn.init.zeros_(m.bias.data)
 
    def forward(self, x):
        x = self.first_part(x)
        return x
 
model = CNN()
for m in model.first_part:
    if isinstance(m, nn.Conv2d):
        # print('m:',m.weight.data)
        print('m:',m.weight.data[0])
        print('m:',m.weight.data[0][0])
        print('m:',m.weight.data.numel()) #numel() 计算矩阵中元素的个数
 
结果:
m: tensor([[[-0.0284, -0.05850.0271],
         [ 0.01250.05540.0511],
         [-0.01060.0574, -0.0053]]])
m: tensor([[-0.0284, -0.05850.0271],
        [ 0.01250.05540.0511],
        [-0.01060.0574, -0.0053]])
m: 504
m: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0.])
m: tensor([[[ 0.00590.0465, -0.0725]]])
m: tensor([[ 0.00590.0465, -0.0725]])
m: 168
m: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0.])
m: tensor([[[ 0.0599],
         [-0.1330],
         [ 0.2456]]])
m: tensor([[ 0.0599],
        [-0.1330],
        [ 0.2456]])
m: 168
m: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0.])

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/schmiloo/article/details/107020922