mindspore.ops.inplace_index_add

mindspore.ops.inplace_index_add(var, indices, updates, axis)[source]

Adds Tensor updates to specified axis and indices of Tensor var element-wise.

Parameters
  • var (Parameter) – The input Parameter to add to, with data type uint8, int8, int16, int32, float16, float32, float64.

  • indices (Tensor) – The indies along axis to perform the addition. A 1D Tensor of shape \((updates.shape[axis],)\), every value of it should be in range \([0, var.shape[axis])\) with data type int32.

  • updates (Tensor) – The input Tensor with the value to add. Must have same data type as var. The shape must be the same as var except the axis th dimension.

  • axis (int) – The dimension along which to index. It should be in range \([0, len(var.dim))\).

Returns

Tensor, updated result, has the same shape and dtype as var.

Raises
  • TypeError – If var is not a Parameter.

  • TypeError – If neither indices nor updates is a Tensor.

  • ValueError – If axis is out of valid range.

  • ValueError – If var rank is not the same as updates rank.

  • ValueError – If shape of indices is not \((updates.shape[axis],)\).

  • ValueError – If updates’s shape is not the same as var except the axis th dimension.

Supported Platforms:

Ascend CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops, Parameter
>>> var = Parameter(Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32))
>>> indices = Tensor(np.array([0, 1]), mindspore.int32)
>>> updates = Tensor(np.array([[0.5, 1.0], [1.0, 1.5]]), mindspore.float32)
>>> var = ops.inplace_index_add(var, indices, updates, axis=0)
>>> print(var)
[[1.5 3. ]
 [4.  5.5]
 [5.  6. ]]