gather_nd

paddle.fluid.layers.gather_nd(input, index, name=None)[source]

Gather Nd Layer

This function is actually a high-dimensional extension of gather and supports for simultaneous indexing by multiple axes. index is a K-dimensional integer tensor, which is regarded as a (K-1)-dimensional tensor of index into input, where each element defines a slice of params:

\[output[(i_0, ..., i_{K-2})] = input[index[(i_0, ..., i_{K-2})]]\]

Obviously, index.shape[-1] <= input.rank . And, the output tensor has shape index.shape[:-1] + input.shape[index.shape[-1]:] .

Given:
    input = [[[ 0,  1,  2,  3],
              [ 4,  5,  6,  7],
              [ 8,  9, 10, 11]],
             [[12, 13, 14, 15],
              [16, 17, 18, 19],
              [20, 21, 22, 23]]]
    input.shape = (2, 3, 4)

* Case 1:
    index = [[1]]

    gather_nd(input, index)
             = [input[1, :, :]]
             = [[12, 13, 14, 15],
                [16, 17, 18, 19],
                [20, 21, 22, 23]]

* Case 2:
    index = [[0,2]]

    gather_nd(input, index)
             = [input[0, 2, :]]
             = [8, 9, 10, 11]

* Case 3:
    index = [[1, 2, 3]]

    gather_nd(input, index)
             = [input[1, 2, 3]]
             = [23]
Parameters
  • input (Variable) – The source input. Its dtype should be int32, int64, float32, float64.

  • index (Variable) – The index input with rank > 1, index.shape[-1] <= input.rank. Its dtype should be int32, int64.

  • name (str|None) – A name for this layer(optional). If set None, the layer will be named automatically.

Returns

A tensor with the shape index.shape[:-1] + input.shape[index.shape[-1]:]

Return type

output (Variable)

Examples

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