gather_nd

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

Warning: API “paddle.fluid.layers.nn.gather_nd” is deprecated since 2.0.0, and will be removed in future versions. Please use “paddle.gather_nd” instead.

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]
Args:

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.

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/nn.py:docstring of paddle.fluid.layers.nn.gather_nd, line 55)

Unexpected indentation.

Its dtype should be int32, int64.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/fluid/layers/nn.py:docstring of paddle.fluid.layers.nn.gather_nd, line 56)

Block quote ends without a blank line; unexpected unindent.

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): A tensor with the shape index.shape[:-1] + input.shape[index.shape[-1]:]

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)