scatter

paddle. scatter ( x, index, updates, overwrite=True, name=None ) [source]

Scatter Layer Output is obtained by updating the input on selected indices based on updates.

>>> import paddle
>>> #input:
>>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')
>>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')
>>> # shape of updates should be the same as x
>>> # shape of updates with dim > 1 should be the same as input
>>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')
>>> overwrite = False
>>> # calculation:
>>> if not overwrite:
...     for i in range(len(index)):
...         x[index[i]] = paddle.zeros([2])
>>> for i in range(len(index)):
...     if (overwrite):
...         x[index[i]] = updates[i]
...     else:
...         x[index[i]] += updates[i]
>>> # output:
>>> out = paddle.to_tensor([[3, 3], [6, 6], [1, 1]])
>>> print(out.shape)
[3, 2]

NOTICE: The order in which updates are applied is nondeterministic, so the output will be nondeterministic if index contains duplicates.

Parameters
  • x (Tensor) – The input N-D Tensor with ndim>=1. Data type can be float32, float64.

  • index (Tensor) – The index is a 1-D or 0-D Tensor. Data type can be int32, int64. The length of index cannot exceed updates’s length, and the value in index cannot exceed input’s length.

  • updates (Tensor) – Update input with updates parameter based on index. When the index is a 1-D tensor, the updates shape should be the same as input, and dim value with dim > 1 should be the same as input. When the index is a 0-D tensor, the updates should be a (N-1)-D tensor, the ith dim of the updates should be queal with the (i+1)th dim of the input.

  • overwrite (bool, optional) – The mode that updating the output when there are same indices.If True, use the overwrite mode to update the output of the same index,if False, use the accumulate mode to update the output of the same index. Default value is True.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name .

Returns

Tensor, The output is a Tensor with the same shape as x.

Examples

>>> import paddle

>>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')
>>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')
>>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')

>>> output1 = paddle.scatter(x, index, updates, overwrite=False)
>>> print(output1)
Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[3., 3.],
 [6., 6.],
 [1., 1.]])

>>> output2 = paddle.scatter(x, index, updates, overwrite=True)
>>> # CPU device:
>>> # [[3., 3.],
>>> #  [4., 4.],
>>> #  [1., 1.]]
>>> # GPU device maybe have two results because of the repeated numbers in index
>>> # result 1:
>>> # [[3., 3.],
>>> #  [4., 4.],
>>> #  [1., 1.]]
>>> # result 2:
>>> # [[3., 3.],
>>> #  [2., 2.],
>>> #  [1., 1.]]