+4 votes
in Programming Languages by (56.5k points)
Which function should I use to count the number of elements in a given tensor?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

You can use numel() function (number of elements) of the torch to find the number of elements in a given tensor.

Here is an example:

>>> import torch
>>> a=torch.tensor([1,2,3,4])
>>> a
tensor([1, 2, 3, 4])
>>> torch.numel(a)
4
>>> a=torch.randn(5,6)
>>> torch.numel(a)
30
 


...