mindspore.ops.TensorScatterSub

View Source On Gitee
class mindspore.ops.TensorScatterSub[source]

Creates a new tensor by subtracting the values from the positions in input_x indicated by indices, with values from updates. When multiple values are provided for the same index, the result of the update will subtract these values respectively. This operation is almost equivalent to using mindspore.ops.ScatterNdSub , except that the updates are applied on output Tensor instead of input Parameter.

# Iterate through all index
for i in range(indices.shape[0]):
    for j in range(indices.shape[1]):
        ...
        for k in range(indices.shape[-2]):  # The last dimension is coordinate dimension
            # Get current index combination
            index_tuple = (i, j, ..., k)
            # Get target position
            target_index = indices[index_tuple]
            # Get corresponding update value
            update_value = updates[index_tuple]
            # Perform subtraction operation
            output[target_index] -= update_value

Refer to mindspore.ops.tensor_scatter_sub() for more details.

Inputs:
  • input_x (Tensor) - The target tensor. The dimension of input_x must be no less than indices.shape[-1].

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

  • updates (Tensor) - The tensor to update the input tensor, has the same type as input_x, and the shape of updates should be equal to indices.shape[:1]+input_x.shape[indices.shape[1]:].

Outputs:

Tensor, has the same shape and type as 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], [0, 0]]), mindspore.int32)
>>> updates = Tensor(np.array([1.0, 2.2]), mindspore.float32)
>>> # Next, demonstrate the approximate operation process of this operator:
>>> # 1, indices[0] = [0, 0], indices[1] = [0, 0]
>>> # 2, And input_x[0, 0] = -0.1
>>> # 3, So input_x[indices] = [-0.1, -0.1]
>>> # 4, Satisfy the above formula: input_x[indices].shape=(2) == updates.shape=(2)
>>> op = ops.TensorScatterSub()
>>> # 5, Perform the subtract operation for the first time:
>>> #      first_input_x = input_x[0][0] - updates[0] = [[-1.1, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> # 6, Perform the subtract operation for the second time:
>>> #      second_input_x = input_x[0][0] - updates[1] = [[-3.3, 0.3, 3.6], [0.4, 0.5, -3.2]]
>>> output = op(input_x, indices, updates)
>>> print(output)
[[-3.3000002  0.3        3.6      ]
 [ 0.4        0.5       -3.2      ]]