📜  Python – PyTorch numel() 方法

📅  最后修改于: 2022-05-13 01:55:20.365000             🧑  作者: Mango

Python – PyTorch numel() 方法

PyTorch torch.numel()方法返回输入张量中的元素总数。

让我们通过几个例子来看看这个概念:
示例 1:
# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.randn(4, 6)
print(a)
  
# Applying the numel function and 
# storing the result in 'out'
out = torch.numel(a)
print(out)

输出:

-0.8263  0.9807 -1.4688  0.2117 -0.8356 -0.0228
-0.8815  1.3652 -0.1892 -1.1241  0.2755  1.3006
 0.0559  0.2389  0.7944  2.6587 -2.0908  1.2973
-0.2056  0.4110  0.2163  0.3091  0.5559 -0.2468
[torch.FloatTensor of size 4x6]
24

示例 2:

# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.FloatTensor([1, 4, 6, 8])
print(a)
  
# Applying the numel function and 
# storing the result in 'out'
out = torch.numel(a)
print(out)

输出:

1
 4
 6
 8
[torch.FloatTensor of size 4]
4