Indexing with LongTensor is inconsistent with that of BoolTensor

As of today, we can use long, byte or bool tensor as indices. but indexing with byte tensor is deprecated.
But indexing with longtensor doesn’t give same answer as indexing with booltensor.

x = torch.rand(3, 3)
>>> x
tensor([[0.0857, 0.9867, 0.0330],
        [0.9295, 0.9787, 0.4835],
        [0.9376, 0.3851, 0.8934]])
y = torch.tensor([[1, 1, 1],
        [0, 0, 0],
        [0, 0, 0]])
>>> y
tensor([[1, 1, 1],
        [0, 0, 0],
        [0, 0, 0]])

indexing with booltensor gives

>>> x[y.bool()]
tensor([0.0857, 0.9867, 0.0330])

indexing with longtensor gives

>>> x[y]
tensor([[[0.9295, 0.9787, 0.4835],
         [0.9295, 0.9787, 0.4835],
         [0.9295, 0.9787, 0.4835]],

        [[0.0857, 0.9867, 0.0330],
         [0.0857, 0.9867, 0.0330],
         [0.0857, 0.9867, 0.0330]],

        [[0.0857, 0.9867, 0.0330],
         [0.0857, 0.9867, 0.0330],
         [0.0857, 0.9867, 0.0330]]])

Is this an expected behavior? Is there something am i doing wrong?
I want indexing with longtensor to give same result as indexing with booltensor.

Thank you in advance!

Yes, this is expected behavior and e.g. explained in numpy - integer array indexing and numpy - boolean array indexing.

1 Like