Open In App

Python – PyTorch numel() method

Last Updated : 26 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

PyTorch torch.numel() method returns the total number of elements in the input tensor.

Syntax: torch.numel(input)

Arguments

  • input: This is input tensor.

Return: It returns the length of the input tensor.

Let’s see this concept with the help of few examples:
Example 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)


Output:

-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

Example 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)


Output:

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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads