PyTorch torch.numel Function
PyTorch torch Reference Manual
torch.numelis a function in PyTorch used to calculate the total number of elements in a tensor.
Function Definition
torch.numel(input)
Usage Examples
Example
import torch
x = torch.randn(3, 4, 5)
print("Total number of elements:", torch.numel(x))
y = torch.tensor([1, 2, 3, 4, 5])
print("Total number of elements:", torch.numel(y))
x = torch.randn(3, 4, 5)
print("Total number of elements:", torch.numel(x))
y = torch.tensor([1, 2, 3, 4, 5])
print("Total number of elements:", torch.numel(y))
The output result is:
元素总数: 60 元素总数: 5
Other Extensions