mindspore.ops.ScatterNdDiv

查看源文件
class mindspore.ops.ScatterNdDiv(use_locking=False)[源代码]

将稀疏除法应用于张量中的单个值或切片。

使用给定值通过除法运算和输入索引更新 input_x 的值。为便于使用更新后的值,函数返回 input_x 的副本。

警告

这是一个实验性API,后续可能修改或删除。

更多参考详见 mindspore.ops.scatter_nd_div()

参数:
  • use_locking (bool,可选) - 是否启用锁保护。默认值: False

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

  • indices (Tensor) - 指定除法操作的索引,数据类型为mindspore.int32或mindspore.int64。索引的rank必须至少为2,并且 indices.shape[-1] <= len(shape)

  • updates (Tensor) - 指定与 input_x 进行除法操作的Tensor,数据类型与 input_x 相同,shape为 indices.shape[:-1] + x.shape[indices.shape[-1]:]

输出:

Tensor,更新后的 input_x ,shape和数据类型与 input_x 相同。

支持平台:

GPU CPU

样例:

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops, Parameter
>>> input_x = Parameter(Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8]), mindspore.float32), name="x")
>>> indices = Tensor(np.array([[2], [4], [1], [7]]), mindspore.int32)
>>> updates = Tensor(np.array([6, 7, 8, 9]), mindspore.float32)
>>> use_locking = False
>>> scatter_nd_div = ops.ScatterNdDiv(use_locking)
>>> output = scatter_nd_div(input_x, indices, updates)
>>> print(output)
[1.         0.25       0.5        4.         0.71428573 6.
 7.         0.8888889 ]
>>> input_x = Parameter(Tensor(np.ones((4, 4, 4)), mindspore.float32))
>>> indices = Tensor(np.array([[0], [2]]), mindspore.int32)
>>> updates = Tensor(np.array([[[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]],
...                            [[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]]]), mindspore.float32)
>>> use_locking = False
>>> scatter_nd_div = ops.ScatterNdDiv(use_locking)
>>> output = scatter_nd_div(input_x, indices, updates)
>>> print(output)
[[[1.         1.         1.         1.        ]
  [0.5        0.5        0.5        0.5       ]
  [0.33333334 0.33333334 0.33333334 0.33333334]
  [0.25       0.25       0.25       0.25      ]]
 [[1.         1.         1.         1.        ]
  [1.         1.         1.         1.        ]
  [1.         1.         1.         1.        ]
  [1.         1.         1.         1.        ]]
 [[0.2        0.2        0.2        0.2       ]
  [0.16666667 0.16666667 0.16666667 0.16666667]
  [0.14285715 0.14285715 0.14285715 0.14285715]
  [0.125      0.125      0.125      0.125     ]]
 [[1.         1.         1.         1.        ]
  [1.         1.         1.         1.        ]
  [1.         1.         1.         1.        ]
  [1.         1.         1.         1.        ]]]