mindspore.ops.TensorScatterUpdate

class mindspore.ops.TensorScatterUpdate[source]

Creates a new tensor by updating the positions in input_x indicated by indices, with values from update. This operation is almost equivalent to using mindspore.ops.ScatterNdUpdate , except that the updates are applied on input_x instead of a zero tensor.

indices must have rank at least 2, the last axis is the depth of each index vectors. For each index vector, there must be a corresponding value in update. If the depth of each index tensor matches the rank of input_x, then each index vector corresponds to a scalar in input_x and each update updates a scalar. If the depth of each index tensor is less than the rank of input_x, then each index vector corresponds to a slice in input_x, and each update updates a slice.

The order in which updates are applied is nondeterministic, meaning that if there are multiple index vectors in indices that correspond to the same position, the value of that position in the output will be nondeterministic.

\[output[indices] = update\]
Inputs:
  • input_x (Tensor) - The input tensor. The dimension of input_x must be no less than indices.shape[-1]. The shape is \((N, *)\) where \(*\) means,any number of additional dimensions. The data type is Number.

  • indices (Tensor) - The index of input tensor whose data type is int32 or int64. The rank must be at least 2.

  • update (Tensor) - The tensor to update the input tensor, has the same type as input_x, and \(update.shape = indices.shape[:-1]+input\_x.shape[indices.shape[-1]:]\)

Outputs:

Tensor, has the same shape and type as input_x.

Raises
  • TypeError – If dtype of indices is neither int32 nor int64.

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

  • ValueError – If the value of input_x are not match with input indices.

  • RuntimeError – If a value of indices is not in input_x.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> 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)
>>> update = Tensor(np.array([1.0, 2.2]), mindspore.float32)
>>> op = ops.TensorScatterUpdate()
>>> output = op(input_x, indices, update)
>>> print(output)
[[ 1.   0.3  3.6]
 [ 0.4  2.2 -3.2]]