mindspore.ops.index_add

mindspore.ops.index_add(x, indices, y, axis, use_lock=True, check_index_bound=True)[源代码]

按照指定轴和索引将输入 y 的元素加到输入 x 中。

说明

  • indices 为一维tensor,并且 \(indices.shape[0] = y.shape[axis]\)

  • indices 中元素的取值范围为 \([0, x.shape[axis] - 1]\)

参数:
  • x (Union[Parameter, Tensor]) - 输入的parameter或tensor。

  • indices (Tensor) - 指定索引。

  • y (Tensor) - 与 x 相加的tensor。

  • axis (int) - 指定轴。

  • use_lock (bool,可选) - 是否对参数更新过程加锁保护。默认 True

  • check_index_bound (bool,可选) - 是否检查 indices 边界。默认 True

返回:

Tensor

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore
>>> x = mindspore.Parameter(mindspore.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], mindspore.float32),
...                         name="name_x")
>>> indices = mindspore.tensor([0, 2], mindspore.int32)
>>> y = mindspore.tensor([[0.5, 1.0], [1.0, 1.5], [2.0, 2.5]], mindspore.float32)
>>> output = mindspore.ops.index_add(x, indices, y, 1)
>>> print(output)
[[ 1.5  2.   4. ]
 [ 5.   5.   7.5]
 [ 9.   8.  11.5]]