Index Multidimensional Torch Tensor By Another Multidimensional Tensor
I have a tensor x in pytorch let's say of shape (5,3,2,6) and another tensor idx of shape (5,3,2,1) which contain indices for every element in first tensor. I want a slicing of the
Solution 1:
From what I understand from the comments, you need idx
to be index in the last dimension and each index in idx
corresponds to similar index in x
(except for the last dimension). In that case (this is the numpy version, you can convert it to torch):
ind = np.indices(idx.shape)
ind[-1] = idx
x[tuple(ind)]
output:
[[10]
[43]]
Solution 2:
You can use range
; and squeeze
to get proper idx
dimension like
x[range(x.size(0)), idx.squeeze()]tensor([10., 43.])
# or
x[range(x.size(0)), idx.squeeze()].unsqueeze(1)
tensor([[10.],
[43.]])
Solution 3:
Here's the one that works in PyTorch using gather
. The idx
needs to be in torch.int64
format which the following line will ensure (note the lowercase of 't' in tensor
).
idx = torch.tensor([[0],
[2]])
torch.gather(x, 1, idx) # 1 is the axis to index here
tensor([[10.],
[43.]])
Post a Comment for "Index Multidimensional Torch Tensor By Another Multidimensional Tensor"