mindspore.ops.scatter_update

mindspore.ops.scatter_update(input_x, indices, updates)[源代码]

使用给定的更新值和输入索引更新输入Tensor的值。

indices 的shape为(i, …, j),则

\[\text{input_x}[\text{indices}[i, ..., j], :] = \text{updates}[i, ..., j, :]\]

输入的 input_xupdates 遵循隐式类型转换规则,以确保数据类型一致。如果它们具有不同的数据类型,则低精度数据类型将转换为高精度数据类型。因Parameter对象不支持类型转换,当 input_x 为低精度数据类型时,会抛出异常。

参数:
  • input_x (Parameter) - scatter_update的输入,数据类型为Parameter。

  • indices (Tensor) - 指定更新操作的索引。数据类型为int32或者int64。如果索引中存在重复项,则更新的顺序无法得知。

  • updates (Tensor) - 指定与 input_x 更新操作的Tensor,其数据类型与 input_x 相同,shape为 indices.shape + input_x.shape[1:]

返回:

Tensor,shape和数据类型与输入 input_x 相同。

异常:
  • TypeError - indices 不是int32或者int64。

  • ValueError - updates 的shape不等于 indices.shape + input_x.shape[1:]

  • RuntimeError - 当 input_xupdates 类型不一致,需要进行类型转换时,如果 updates 不支持转成参数 input_x 需要的数据类型,就会报错。

支持平台:

Ascend GPU CPU

样例:

>>> np_x = np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]])
>>> input_x = mindspore.Parameter(Tensor(np_x, mindspore.float32), name="x")
>>> indices = Tensor(np.array([0, 1]), mindspore.int32)
>>> np_updates = np.array([[2.0, 1.2, 1.0], [3.0, 1.2, 1.0]])
>>> updates = Tensor(np_updates, mindspore.float32)
>>> output = ops.scatter_update(input_x, indices, updates)
>>> print(output)
[[2. 1.2  1.]
 [3. 1.2  1.]]