Find indices with value (zeros)

(Christos Baziotis) #1

I have a 1D Variable (LongTensor) and i want to find the indices of the elements with a given value (zero). Is there a way to do this efficiently in PyTorch?

For instance, in order to get the indices of non-zero elements, i do this:

non_zeros = torch.nonzero(lengths_c.view(-1).data).squeeze()

I need the opposite of this (indices of zero elements).

(Simon Wang) #2

(x != 0).nonzero()

(Nicolas Beaudoin Gagnon) #3

I think it would be (x == 0).nonzero() instead of (x != 0).nonzero(). The last one is the same as x.nonzero().