Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.gather function

PyTorch torch.gather function


PyTorch torch reference manual 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

)

print

(

result

)

The output is:

tensor([[1],

        [4],

        [5]])


PyTorch torch reference manual PyTorch torch reference manual