gather_nd

paddle.fluid.layers.nn. 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[(i0,...,iK2)]=input[index[(i0,...,iK2)]]

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 (Tensor) – The input Tensor which it’s data type should be bool, float32, float64, int32, int64.

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

  • 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

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

Return type

output (Tensor)

Examples

import paddle
import paddle.fluid as fluid
paddle.enable_static()

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)