mindspore.ops.GatherNd

class mindspore.ops.GatherNd[源代码]

根据索引获取输入Tensor指定位置上的元素。

indices 是K维整型Tensor,则可看作是从 input_x 中取K-1维Tensor,每个元素都是一个切片:

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

indices 的最后一维的长度不能超过 input_x 的秩: \(indices.shape[-1] <= input\_x.rank\)

输入:

  • input_x (Tensor) - GatherNd的输入。任意维度的Tensor。

  • indices (Tensor) - 索引Tensor,其数据类型为int32或int64。

输出:

Tensor,数据类型与 input_x 相同,shape为 indices_shape[:-1] + x_shape[indices_shape[-1]:]

异常:

  • ValueError - input_x 的shape长度小于 indices 的最后一维的长度。

支持平台:

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]