PyTorch torch.numel() method calculates the total number of elements in the tensor, regardless of its shape.

The above figure shows that we calculated the total number of elements in a 2×3 tensor, which is 2×3 = 6.
It works with any shape or dimensions (e.g., 1D, 2D, 3D, or other multidimensional) and does not track gradients, as it is a metadata operation.
The numel() method returns 0 for empty tensors (e.g., torch.tensor([])), and scalars (0D tensors) return 1.
An alias for this method is called torch.nelement().
Syntax
torch.numel(input_tensor)
Parameters
Argument | Description |
input_tensor | It is an input tensor whose elements you want to count. |
Scalar Tensor

Since the scalar tensor contains only one element, this method returns 1.
import torch scalar = torch.tensor(21.0) count = torch.numel(scalar) print(count) # Output: 1
1D Tensor (Vector)

If your input is a one-dimensional tensor, numel() returns the length of the 1D tensor.
import torch vector = torch.tensor([19, 21, 18, 48]) count = torch.numel(vector) print(count) # Output: 4
The shape of the input tensor is (4, ), which means it is a 1D tensor, and it returns the number of elements.
2D Tensor (Matrix)
What about 2D tensors? Well, for a 2D tensor, it returns the product of the dimensions.
import torch matrix = torch.tensor([[1, 2, 3], [4, 5, 6]]) counting = torch.numel(matrix) print(counting) # Output: 6 print(matrix.shape) # Output: torch.Size([2, 3])
The .numel() returns 2×3 = 6, the total elements.
Higher-Dimensional Tensor
If the input is a 3D or higher-dimensional tensor, it calculates the product of all dimensions.
We can generate a random 3D tensor using torch.randn() method.
import torch tensor_3d = torch.randn(2, 3, 4) counting_elements = torch.numel(tensor_3d) print(counting_elements) # Output: 24 print(tensor_3d.shape) # Output: torch.Size([2, 3, 4])
As the input tensor’s shape is (2, 3, 4), the numel() returns 2 * 3 * 4 = 24.
Empty Tensor
import torch empty_tensor = torch.tensor([]) counting = torch.numel(empty_tensor) print(counting) # Output: 0
Validating tensor reshape
You can use the torch.view() method with the .numel() to ensure a reshape operation is valid (same number of elements).
import torch tensor = torch.randn(2, 3, 4) count_elements = torch.numel(tensor) reshaped_tensor = tensor.view(4, 6) # Reshape to (4, 6) reshaped_numel = torch.numel(reshaped_tensor) print(f"Original numel: {count_elements}") # Output: 24 print(f"Reshaped numel: {reshaped_numel}") # Output: 24
After reshaping, using the numel() method, we can confirm that both have the same elements.