Find number of non-zero elements in a tensor along an aixs
I want to find the number of non-zero elements in a tensor along a particular axis. Is there any PyTorch function which can do this?

I tried to use the nonzero() method in PyTorch.

torch.nonzero(losses).size(0)


Here, lossess is a tensor of shape 64 x 1. When I run the above statement, it gives me the following error.

TypeError: Type Variable doesn't implement stateless method nonzero


But if I run, torch.nonzero(losses.data).size(0), then it works fine. Any clue, why this is happening or what the error means?

Meaning of the error message - TypeError: Type Variable doesn't implement stateless method nonzero is, we cannot use torch.nonzero() on autograd.Variable but only on simple tensors. Also it should be noted that, tensors are stateless while the Variables are stateful.
