gather

paddle.fluid.layers. gather ( input, index, overwrite=True ) [源代码]

根据索引 index 获取输入 input 的最外层维度的条目,并将它们拼接在一起。

gather - 图1

  1. X = [[1, 2],
  2. [3, 4],
  3. [5, 6]]
  4. Index = [1, 2]
  5. Then:
  6. Out = [[3, 4],
  7. [5, 6]]

参数:

  • input (Tensor) - 输入, 秩 rank >= 1 , 支持的数据类型包括 int32、int64、float32、float64 和 uint8 (CPU)、float16(GPU) 。

  • index (Tensor) - 索引,秩 rank = 1, 数据类型为 int32 或 int64。

  • overwrite (bool) - 具有相同索引时在反向更新梯度的模式。如果为 True ,则使用覆盖模式更新相同索引的梯度;如果为 False ,则使用累积模式更新相同索引的梯度。默认值为 True

返回:和输入的秩相同的输出张量。

代码示例

  1. import paddle.fluid as fluid
  2. x = fluid.layers.data(name='x', shape=[-1, 5], dtype='float32')
  3. index = fluid.layers.data(name='index', shape=[-1, 1], dtype='int32')
  4. output = fluid.layers.gather(x, index)