mindspore.Tensor.index_add

查看源文件
mindspore.Tensor.index_add(indices, y, axis, use_lock=True, check_index_bound=True)

将Tensor y 加到Parameter或Tensor self 的指定 axis 轴和指定 indices 位置。要求 axis 轴的取值范围 为[0, len(self.dim) - 1], indices 中元素的取值范围为[0, self.shape[axis] - 1]。

参数:
  • indices (Tensor) - 指定Tensor y 加到 selfaxis 轴的指定下标位置,要求数据类型为int32。 要求 indices shape的维度为一维,并且 indices shape的大小与 y shape在 axis 轴上的大小一致。 indices 中元素 取值范围为[0, b),其中b的值为 self shape在 axis 轴上的大小。

  • y (Tensor) - 与 self 相加的Tensor。

  • axis (int) - 指定沿哪根轴相加。

  • use_lock (bool,可选) - 是否对参数更新过程加锁保护。如果为 True ,在更新参数 self 的值时使用原子操作以实现加锁保护,如果为 Falseself 的值可能会不可预测。默认值: True

  • check_index_bound (bool,可选) - True 表示检查 indices 边界, False 表示不检查。默认值: True

返回:

相加后的Tensor。shape和数据类型与输入 self 相同。

异常:
  • TypeError - indices 或者 y 的类型不是Tensor。

  • ValueError - axis 的值超出 self shape的维度范围。

  • ValueError - self shape的维度和 y shape的维度不一致。

  • ValueError - indices shape的维度不是一维,或者 indices shape的大小与 y shape在 axis 轴上的大小不一致。

  • ValueError - 除 axis 轴外, self shape和 y shape的大小不一致。

支持平台:

Ascend GPU CPU

样例:

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> indices = Tensor(np.array([0, 2]), mindspore.int32)
>>> y = Tensor(np.array([[0.5, 1.0], [1.0, 1.5], [2.0, 2.5]]), mindspore.float32)
>>> output = x.index_add(indices, y, axis = 1)
>>> print(output)
[[ 1.5  2.   4. ]
 [ 5.   5.   7.5]
 [ 9.   8.  11.5]]
mindspore.Tensor.index_add(dim, index, source, *, alpha=1)

详情请参考 mindspore.ops.index_add()。 其中 Tensor.index_addmindspore.ops.index_add() 参数对应关系如下: dim -> axisindex -> indicessource * alpha -> y