Slicing tensor using boolean list

I have a boolean Python list that I’d like to use as a “mask” for a tensor (of the same size as the list), returning the entries of the tensor where the list is true.

For instance, given the list mask = [True, False, True] and the tensor x = Tensor([1, 2, 3]), I would like to get the tensor y = Tensor([1, 3]). In numpy, this would be simply y = x[mask], but in PyTorch indexing tensors with lists is not (yet?) supported.

Moreover, I need an efficient implementation for this slicing, since this would be performed in every forward pass of my model. What do you suggest?

Thank you in advance.

Well, currently my solution is:

idx = np.argwhere(np.asarray(mask))
y = x[idx]

Is there any better option?

Hi Diogo -

We currently have a subset of support for numpy indexing but what I believe you are referring to is Boolean array indexing as described here: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing.

The closest PyTorch equivalent is masked select, as described here: http://pytorch.org/docs/master/torch.html?highlight=masked_select#torch.masked_select. If you could instead generate your mask in the ByteTensor format described there, you could achieve what you want.

7 Likes

Thanks, @killeent, this perfectly does the job!

y=torch.arange(0,3)
x=torch.Tensor([True,False,True])==True
print(y[x])

this works

6 Likes

This solution is better. Assume you have a two dimensional tensor y=torch.range(1, 8).reshape(2, 4), and x=torch.Tensor([True,False])==True. In this case, you can’t use torch.masked_select, but you can easily execute y[x, :].