mindspore.ops.inplace_add

mindspore.ops.inplace_add(x, v, indices)[源代码]

根据 indices,将 x 中的对应位置加上 v 。计算 y = x; y[i,] += v

说明

indices 只能沿着最高轴进行索引。

参数:
  • x (Tensor) - 待更新的Tensor,支持的数据类型包括float16、float32、float64、int32。

  • v (Tensor) - 待加上的值,除第零维外每一维度需与 x 相同。数据类型应与 x 相同。

  • indices (Union[int, tuple]) - 待更新值在原Tensor中的索引。取值范围[0, len(x))。若为tuple,则大小与 v 的第一维度大小相同。

返回:

Tensor,更新后的Tensor。

异常:
  • TypeError - indices 不是int或tuple。

  • TypeError - indices 是元组,但是其中的元素不是int。

  • ValueError - x 的维度与 v 的维度不相等。

  • ValueError - indices 的长度与 v.shape[0] 不相等。

  • ValueError - indices 的值不属于范围 [0, x.shape[0])

支持平台:

Ascend GPU CPU

样例:

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor, ops
>>> indices = (0, 1)
>>> x = Tensor(np.array([[1, 2], [3, 4], [5, 6]]), mindspore.float32)
>>> input_v = Tensor(np.array([[0.5, 1.0], [1.0, 1.5]]), mindspore.float32)
>>> output = ops.inplace_add(x, input_v, indices)
>>> print(output)
[[1.5 3. ]
 [4.  5.5]
 [5.  6. ]]