mindspore.ops.ScatterNd

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

Scatters a tensor into a new tensor depending on the specified indices.

Creates an empty tensor with the given shape, and set values by scattering the update tensor depending on indices.

The empty tensor has rank P and indices has rank Q where Q >= 2.

indices has shape \((i_0, i_1, ..., i_{Q-2}, N)\) where N <= P.

The last dimension of indices (with length N ) indicates slices along the N th dimension of the empty tensor.

updates is a tensor of rank Q-1+P-N. Its shape is: \((i_0, i_1, ..., i_{Q-2}, shape_N, ..., shape_{P-1})\).

Inputs:
  • indices (Tensor) - The index of scattering in the new tensor with int32 data type. The rank of indices must be at least 2 and indices_shape[-1] <= len(shape).

  • updates (Tensor) - The source Tensor to be scattered. It has shape indices_shape[:-1] + shape[indices_shape[-1]:].

  • shape (tuple[int]) - Define the shape of the output tensor, has the same type as indices.

Outputs:

Tensor, the new tensor, has the same type as update and the same shape as shape.

Raises
  • TypeError – If shape is not a tuple.

  • ValueError – If any element of shape is less than 1.

Supported Platforms:

Ascend GPU

Examples

>>> op = ops.ScatterNd()
>>> indices = Tensor(np.array([[0, 1], [1, 1]]), mindspore.int32)
>>> updates = Tensor(np.array([3.2, 1.1]), mindspore.float32)
>>> shape = (3, 3)
>>> output = op(indices, updates, shape)
>>> print(output)
[[0.  3.2 0. ]
 [0.  1.1 0. ]
 [0.  0.  0. ]]