mindspore.ops.tensor_scatter_elements

mindspore.ops.tensor_scatter_elements(input_x, indices, updates, axis=0, reduction='none')[source]

Updates the value of the input tensor through the reduction operation.

tensor_scatter_elements takes three inputs data, updates, and indices of the same rank r >= 1, an optional attribute axis that identifies an axis of data (default is 0), and another optional attribute reduction that identifies reduction operation. When reduction is set to “none”, the update value will be assigned to the output value according to the indices. When reduction is set to “add”, the update value will be added to the output value according to the indices.

For a 3-D tensor, the output is:

output[indices[i][j][k]][j][k] = updates[i][j][k]  # if axis == 0, reduction == "none"

output[i][indices[i][j][k]][k] += updates[i][j][k]  # if axis == 1, reduction == "add"

output[i][j][indices[i][j][k]] = updates[i][j][k]  # if axis == 2, reduction == "none"

Warning

  • 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.

  • On Ascend, the reduction only support set to “none” for now.

  • On Ascend, the data type of input_x must be float16 or float32.

Note

If some values of the indices are out of bound, instead of raising an index error, the corresponding updates will not be updated to input_x.

Parameters
  • input_x (Tensor) – The target tensor. The shape is \((N,*)\) where \(*\) means,any number of additional dimensions.

  • indices (Tensor) – The index to do add operation whose data type must be mindspore.int32 or mindspore.int64. Same rank as input_x. And accepted range is [-s, s) where s is the size along axis.

  • updates (Tensor) – The tensor doing the add operation with input_x, has the same type as input_x, and update.shape should be equal to indices.shape.

  • axis (int) – Which axis to scatter, default is 0. Accepted range is [-r, r) where r = rank(input_x).

  • reduction (str) – Which reduction operation to scatter, default is “none”. Other option: “add”.

Returns

Tensor, has the same shape and type as input_x.

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

  • ValueError – If anyone of the rank among input_x, indices and updates less than 1.

  • ValueError – If the shape of updates is not equal to the shape of indices.

  • ValueError – If the rank of updates is not equal to the rank of input_x.

  • RuntimeError – If the data type of input_x and updates conversion of Parameter is required when data type conversion of Parameter is not supported.

Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import ops
>>> input_x = Parameter(Tensor(np.array([[1, 2, 3, 4, 5]]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([[2, 4]]), mindspore.int32)
>>> updates = Tensor(np.array([[8, 8]]), mindspore.float32)
>>> axis = 1
>>> reduction = "none"
>>> output = ops.tensor_scatter_elements(input_x, indices, updates, axis, reduction)
>>> print(output)
[[ 1  2  8  4  8]]