mindspore.ops.GatherD

class mindspore.ops.GatherD(*args, **kwargs)[source]

Gathers values along an axis specified by dim.

For a 3-D tensor, the output is:

output[i][j][k] = x[index[i][j][k]][j][k]  # if dim == 0

output[i][j][k] = x[i][index[i][j][k]][k]  # if dim == 1

output[i][j][k] = x[i][j][index[i][j][k]]  # if dim == 2

If x is an n-D tensor with shape \((z_0, z_1, ..., z_i, ..., z_{n-1})\) and dim = i, the index must be an n-D tensor with shape \((z_0, z_1, ..., y, ..., z_{n-1})\) where y>=1 and the output will have the same shape as index.

Inputs:
  • x (Tensor) - The source tensor. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • dim (int) - The axis along which to index. It must be int32 or int64. Only constant value is allowed.

  • index (Tensor) - The indices of elements to gather. It can be one of the following data types: int32, int64. The value range of each index element is [-x_rank[dim], x_rank[dim]).

Outputs:

Tensor, the shape of tensor is \((z_1, z_2, ..., z_N)\), has the same data type with x.

Raises
  • TypeError – If dtype of dim or index is neither int32 nor int64.

  • ValueError – If length of shape of x is not equal to length of shape of index.

Supported Platforms:

Ascend GPU CPU

Examples

>>> x = Tensor(np.array([[1, 2], [3, 4]]), mindspore.int32)
>>> index = Tensor(np.array([[0, 0], [1, 0]]), mindspore.int32)
>>> dim = 1
>>> output = ops.GatherD()(x, dim, index)
>>> print(output)
[[1 1]
 [4 3]]