mindspore.Tensor.gather_nd

Tensor.gather_nd(indices)[source]

Gathers slices from a input tensor by indices. Using given indices to gather slices from a input tensor with a specified shape. input tensor’s shape is (N,) where means any number of additional dimensions. For convenience define it as input_x, the variable input_x refers to input tensor. indices is an K-dimensional integer tensor. Suppose that it is a (K-1)-dimensional tensor and each element of it defines a slice of input tensor:

output[(i0,...,iK2)]=input_x[indices[(i0,...,iK2)]]

The last dimension of indices can not more than the rank of input tensor: indices.shape[1]<=input_x.rank.

Parameters

indices (Tensor) – The index tensor that gets the collected elements, with int32 or int64 data type.

Returns

Tensor, has the same type as input tensor and the shape is indices_shape[:1]+input_x_shape[indices_shape[1]:].

Raises

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

Supported Platforms:

Ascend GPU CPU

Examples

>>> 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 = input_x.gather_nd(indices)
>>> print(output)
[-0.1  0.5]