paddle.fluid.layers.nn. gather ( input, index, overwrite=True ) [source]

Output is obtained by gathering entries of the outer-most dimension of X indexed by index and concatenate them together.

\[Out = X[Index]\]

X = [[1, 2],
     [3, 4],
     [5, 6]]

Index = [1, 2]


Out = [[3, 4],
       [5, 6]]
  • input (Tensor) – The source input tensor with rank>=1. Supported data type is int32, int64, float32, float64 and uint8 (only for CPU), float16 (only for GPU).

  • index (Tensor) – The index input tensor with rank=1. Data type is int32 or int64.

  • overwrite (bool, optional) – The mode that updating the grad when has same index. If True, use the overwrite mode to update the grad of the same index, if False, use the accumulate mode to update the grad of the same index. Default value is True.


The output is a tensor with the same rank as input.

Return type

output (Tensor)


import paddle
import paddle.fluid as fluid

x ='x', shape=[-1, 5], dtype='float32')
index ='index', shape=[-1, 1], dtype='int32')
output = fluid.layers.gather(x, index)