mindspore.Tensor.index_put_

View Source On Gitee
Tensor.index_put_(indices, values, accumulate=False)[source]

Returns a Tensor. According to the index number of indices , replace the value corresponding to the "self Tensor" with the value in values.

Parameters
  • indices (tuple[Tensor], list[Tensor]) – the indices of type is bool, uint8, int32 or int64, used to index into the "self Tensor". The rank of tensors in indices should be 1-D, size of indices should <= the rank of "self Tensor" and the tensors in indices should be broadcastable. When tensor type is bool and uint8, the shape will be the match as the input dimension in turn. For example, the first tensor of indices is a bool, shape (1, 2), input shape (1, 2, 3), and then (1, 2) matches the shape of input (1, 2) at index 0, 1.

  • values (Tensor) – 1-D Tensor of the same type as "self Tensor". If size == 1, it will be broadcastable.

  • accumulate (bool, optional) – If accumulate is True, the elements in values are added to "self Tensor", else the elements in values replace the corresponding element in the "self Tensor". Default: False.

Returns

Tensor, with the same type and shape as the "self Tensor".

Raises
  • TypeError – If the dtype of the "self Tensor" is not equal to the dtype of values.

  • TypeError – If the dtype of indices is not tuple[Tensor], list[Tensor].

  • TypeError – If the dtype of tensors in indices are not bool, uint8, int32 or int64.

  • TypeError – If the dtypes of tensors in indices are inconsistent.

  • TypeError – If the dtype of accumulate is not bool.

  • ValueError – If rank(values) is not 1-D.

  • ValueError – If size(values) is not 1 or max size of the tensors in indices when rank("self Tensor") == size(indices).

  • ValueError – If size(values) is not 1 or "self Tensor".shape[-1] when rank("self Tensor") > size(indices).

  • ValueError – If the rank of tensors in indices is not 1-D.

  • ValueError – If the tensors in indices is not be broadcastable.

  • ValueError – If size(indices) > rank("self Tensor").

Supported Platforms:

Ascend CPU

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6]]).astype(np.int32))
>>> values = Tensor(np.array([3]).astype(np.int32))
>>> indices = [Tensor(np.array([0, 1, 1]).astype(np.int32)), Tensor(np.array([1, 2, 1]).astype(np.int32))]
>>> accumulate = True
>>> output = x.index_put_(indices, values, accumulate)
>>> print(output)
[[1 5 3]
 [4 8 9]]