scatter_reduce
- paddle. scatter_reduce ( input: Tensor, dim: int, index: Tensor, src: Tensor, reduce: Literal['sum', 'prod', 'mean', 'amin', 'amax'], *, include_self: bool = True ) Tensor [source]
-
Scatter the values of the source tensor to the target tensor according to the given indices, and perform a reduction operation along the designated axis.
- Parameters
-
input (Tensor) – The Input Tensor. Supported data types are bfloat16, float16, float32, float64, int32, int64, uint8.
dim (int) – The axis to scatter 1d slices along.
index (Tensor) – Indices to scatter along each 1d slice of input. This must match the dimension of input, Supported data type are int32 and int64.
src (Tensor) – The value element(s) to scatter. The data types should be same as input.
reduce (str) – The reduce operation, support ‘sum’, ‘prod’, ‘mean’, ‘amin’, ‘amax’.
include_self (bool, optional) – whether to reduce with the elements of input, default is ‘True’.
- Returns
-
Tensor, The indexed element, same dtype with input
Examples
>>> import paddle >>> x = paddle.to_tensor([[10, 20, 30], [40, 50, 60]]) >>> indices = paddle.zeros((2,3)).astype("int32") >>> values = paddle.to_tensor([[1, 2, 3],[4, 5, 6]]).astype(x.dtype) >>> result = paddle.scatter_reduce(x, 0, indices, values, "sum", include_self=True) >>> print(result) Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, [[15, 27, 39], [40, 50, 60]]) >>> result = paddle.scatter_reduce(x, 0, indices, values, "prod", include_self=True) >>> print(result) Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, [[40 , 200, 540], [40 , 50 , 60 ]]) >>> result = paddle.scatter_reduce(x, 0, indices, values, "mean", include_self=True) >>> print(result) Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, [[5 , 9 , 13], [40, 50, 60]])