Total size of Tensor?

Hello, I was trying to get the total pixel count for an image tensor.
The only solution I found is torch.Tensor(np.prod(tensor.size())) which isn’t as elegant as I would like it to be.
Is there somewhere in the documentary I overlook that contains a way to directly return the value? If not, will it be useful if I make a PR about this?
Cheers.

2 Likes

Try tensor.nelement().

8 Likes

If you do not have the tensor, but you have the torch.Size object, you can use torch.Size.numel(). For example torch.Size([3,5]).numel() returns 15.