scatter_nd

paddle. scatter_nd ( index, updates, shape, name=None ) [源代码]

根据 index,将 updates 添加到一个新的 Tensor 中,从而得到输出的 Tensor。这个操作与 scatter_nd_add 类似,除了形状为 shape 的 Tensor 是通过零初始化的。相应地,scatter_nd(index, updates, shape) 等价于 scatter_nd_add(fluid.layers.zeros(shape, updates.dtype), index, updates)。如果 index 有重复元素,则将累积相应的更新,因此,由于数值近似问题,索引中重复元素的顺序不同可能会导致不同的输出结果。具体的计算方法可以参见 scatter_nd_add。该 OP 是 gather_nd 的反函数。

参数

  • index (Tensor) - 输入的索引 Tensor,数据类型为非负 int32 或非负 int64。它的维度 index.ndim 必须大于等于 1,并且 index.shape[-1] <= len(shape)

  • updates (Tensor) - 输入的更新 Tensor。形状必须是 index.shape[:-1] + shape[index.shape[-1]:]。数据类型可以是 float32,float64。

  • shape (tuple|list) - 要求输出 Tensor 的形状。类型是 tuple 或者 list。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

Tensor,数据类型与 updates 相同,形状是 shape

代码示例

>>> import paddle

>>> index = paddle.to_tensor([[1, 1],
...                           [0, 1],
...                           [1, 3]], dtype="int64")
>>> updates = paddle.rand(shape=[3, 9, 10], dtype='float32')
>>> shape = [3, 5, 9, 10]

>>> output = paddle.scatter_nd(index, updates, shape)