mindspore.ops.IndexAdd

class mindspore.ops.IndexAdd(axis, use_lock=True, check_index_bound=True)[源代码]

将Tensor y 加到Tensor x 的指定 axisindicesaxis 取值范围为[0, len(x.dim) - 1], indices 取值范围为[0, len(x[axis]) - 1]。

参数:
  • axis (int) - 进行索引的axis。

  • use_lock (bool) - 是否对参数更新加锁保护。默认值:True。

  • check_index_bound (bool) - 如果为True将对索引进行边界检查。默认值:True。

输入:
  • x (Parameter) - 要添加到的输入参数。

  • indices (Tensor) - 沿 axis 在指定 indices 位置进行加法运算。数据类型支持int32。indices 必须为一维且与 yaxis 维度的尺寸相同。 indices 取值范围应为[0, b),其中b为 xaxis 维度的尺寸。

  • y (Tensor) - 被添加到 x 的输入Tensor。必须与 x 的数据类型相同。除 axis 之外的维度shape必须与 x 的shape相同。

输出:

Tensor,与 x 的shape和数据类型相同。

异常:
  • TypeError - x 不是Parameter。

  • TypeError - indicesy 不是Tensor。

  • ValueError - axis 超过了 x 的秩。

  • ValueError - xy 的秩不相同。

  • ValueError - indices 不是一维或与 y[axis] 的尺寸不同。

  • ValueError - y 的shape与除 axis 之外的维度的 x 的shape不同。

支持平台:

Ascend GPU CPU

样例:

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.index_add = ops.IndexAdd(axis=1)
...         self.x = Parameter(Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32),
...                 name="name_x")
...         self.indices = Tensor(np.array([0, 2]), mindspore.int32)
...
...     def construct(self, y):
...         return self.index_add(self.x, self.indices, y)
...
>>> y = Tensor(np.array([[0.5, 1.0], [1.0, 1.5], [2.0, 2.5]]), mindspore.float32)
>>> net = Net()
>>> output = net(y)
>>> print(output)
[[ 1.5  2.   4. ]
 [ 5.   5.   7.5]
 [ 9.   8.  11.5]]