class mindspore.ops.GatherNd[source]

Gathers slices from a tensor by indices.

Using given indices to gather slices from a tensor with a specified shape.

indices is an K-dimensional integer tensor. Supposes it as a (K-1)-dimensional tensor and each element of it defines a slice of input_x:

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

The last dimension of indices can not more than the rank of input_x: \(indices.shape[-1] <= input\_x.rank\).

  • input_x (Tensor) - The target tensor to gather values. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) - The index tensor, with int32 or int64 data type.


Tensor, has the same type as input_x and the shape is indices_shape[:-1] + x_shape[indices_shape[-1]:].


ValueError – If length of shape of input_x is less than the last dimension of indices.

Supported Platforms:

Ascend GPU CPU


>>> op = ops.GatherNd()
>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> indices = Tensor(np.array([[0, 0], [1, 1]]), mindspore.int32)
>>> output = op(input_x, indices)
>>> print(output)
[-0.1  0.5]