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?
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, :].