PyTorch torch.gather function
PyTorch torch reference manual
torch.gather A function in PyTorch for collecting elements at specified indices along a given dimension.
Function definition
torch.gather(input, dim, index, sparse_grad)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
[
1
,
2
]
,
[
3
,
4
]
,
[
5
,
6
]
]
)
Collect along dim=1
index
=
torch.
tensor
(
[
[
0
]
,
[
1
]
,
[
0
]
]
)
result
=
torch.
gather
(
x
,
dim
=
1
,
index
=
index
)
(
result
)
The output is:
tensor([[1],
[4],
[5]])