gather

paddle. gather ( x, index, axis=None, name=None ) [source]

Output is obtained by gathering entries of axis of x indexed by index and concatenate them together.

Given:

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

index = [1, 2]
axis=[0]

Then:

out = [[3, 4],
       [5, 6]]
Parameters
  • x (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=0 or rank=1. Data type is int32 or int64.

  • axis (Tensor|int, optional) – The axis of input to be gathered, it’s can be int or a Tensor with data type is int32 or int64. The default value is None, if None, the axis is 0.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name .

Returns

output (Tensor), If the index is a 1-D tensor, the output is a tensor with the same shape as x. If the index is a 0-D tensor, the output will reduce the dimension where the axis pointing.

Examples

>>> import paddle

>>> input = paddle.to_tensor([[1,2],[3,4],[5,6]])
>>> index = paddle.to_tensor([0,1])
>>> output = paddle.gather(input, index, axis=0)
>>> print(output)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1, 2],
 [3, 4]])