index_add

paddle. index_add ( x, index, axis, value, name=None ) [source]

Adds the elements of the input tensor with value tensor by selecting the indices in the order given in index.

Parameters
  • x (Tensor) – The Destination Tensor. Supported data types are int32, int64, float16, float32, float64.

  • index (Tensor) – The 1-D Tensor containing the indices to index. The data type of index must be int32 or int64.

  • axis (int) – The dimension in which we index.

  • value (Tensor) – The tensor used to add the elements along the target axis.

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Returns

Tensor, same dimention and dtype with x.

Examples

>>> 
>>> import paddle
>>> paddle.device.set_device('gpu')

>>> input_tensor = paddle.to_tensor(paddle.ones((3, 3)), dtype="float32")
>>> index = paddle.to_tensor([0, 2], dtype="int32")
>>> value = paddle.to_tensor([[1, 1, 1], [1, 1, 1]], dtype="float32")
>>> outplace_res = paddle.index_add(input_tensor, index, 0, value)
>>> print(outplace_res)
Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[2., 2., 2.],
 [1., 1., 1.],
 [2., 2., 2.]])