index_put_

paddle. index_put_ ( x, indices, value, accumulate=False, name=None ) [source]

Puts values from the tensor values into the tensor x using the indices specified in indices (which is a tuple of Tensors). The expression paddle.index_put_(x, indices, values) is equivalent to tensor[indices] = values. Returns x. If accumulate is True, the elements in values are added to x. If accumulate is False, the behavior is undefined if indices contain duplicate elements.

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

  • indices (Tuple of Tensor) – The tuple of Tensor containing the indices to index. The data type of tensor in indices must be int32, int64 or bool.

  • value (Tensor) – The tensor used to be assigned to x.

  • accummulate (Bool, optional) – Whether the elements in values are added to x. Default: False.

  • 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

>>> x = paddle.zeros([3, 3])
>>> value = paddle.ones([3])
>>> ix1 = paddle.to_tensor([0,1,2])
>>> ix2 = paddle.to_tensor([1,2,1])
>>> indices=(ix1,ix2)

>>> out = paddle.index_put_(x,indices,value)
>>> print(x)
Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 1., 0.],
 [0., 0., 1.],
 [0., 1., 0.]])
>>> print(out)
Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 1., 0.],
 [0., 0., 1.],
 [0., 1., 0.]])