paddle. scatter_nd ( index, updates, shape, name=None ) [source]

Scatter_nd Layer

Output is obtained by scattering the updates in a new tensor according to index . This op is similar to scatter_nd_add, except the tensor of shape is zero-initialized. Correspondingly, scatter_nd(index, updates, shape) is equal to scatter_nd_add(paddle.zeros(shape, updates.dtype), index, updates) . If index has repeated elements, then the corresponding updates are accumulated. Because of the numerical approximation issues, the different order of repeated elements in index may cause different results. The specific calculation method can be seen scatter_nd_add . This op is the inverse of the gather_nd op.

  • index (Tensor) – The index input with ndim > 1 and index.shape[-1] <= len(shape). Its dtype should be int32 or int64 as it is used as indexes.

  • updates (Tensor) – The updated value of scatter_nd op. Its dtype should be float32, float64. It must have the shape index.shape[:-1] + shape[index.shape[-1]:]

  • shape (tuple|list) – Shape of output tensor.

  • name (str|None) – The output Tensor name. If set None, the layer will be named automatically.


The output is a tensor with the same type as updates .

Return type

output (Tensor)


import paddle
import numpy as np

index_data = np.array([[1, 1],
                       [0, 1],
                       [1, 3]]).astype(np.int64)
index = paddle.to_tensor(index_data)
updates = paddle.rand(shape=[3, 9, 10], dtype='float32')
shape = [3, 5, 9, 10]

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