mindspore.Tensor.masked_scatter_

Tensor.masked_scatter_(mask, source) Tensor[source]

Updates the value in the self with the source value according to the mask, and returns a Tensor. The shape of mask and the self must be the same or mask is broadcastable.

Note

When the total number of elements in source is less than the number of True elements in mask, the NPU may not be able to detect this invalid input; therefore, the correctness of the output cannot be guaranteed.

Parameters
  • mask (Tensor[bool]) – A bool tensor with a shape broadcastable to the self.

  • source (Tensor) – A tensor with the same data type as the self. The number of elements must be greater than or equal to the number of True elements in mask.

Returns

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

Raises
  • TypeError – If mask or source is not a Tensor.

  • TypeError – If data type of the "self Tensor" is not be supported.

  • TypeError – If dtype of mask is not bool.

  • TypeError – If the dim of the "self Tensor" is less than the dim of mask.

  • ValueError – If mask can not be broadcastable to the "self Tensor".

Supported Platforms:

Ascend

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor
>>> x = Tensor(np.array([1., 2., 3., 4.]), mindspore.float32)
>>> mask = Tensor(np.array([True, True, False, True]), mindspore.bool_)
>>> tensor = Tensor(np.array([5., 6., 7.]), mindspore.float32)
>>> output = x.masked_scatter_(mask, tensor)
>>> print(output)
[5. 6. 3. 7.]
>>> print(x)
[5. 6. 3. 7.]