mindspore.Tensor.masked_scatter

View Source On Gitee
Tensor.masked_scatter(mask, x)[source]

Returns a Tensor. Updates the value in the “self Tensor” with the tensor value according to the mask. The shape of mask and the “self Tensor” must be the same or mask is broadcastable.

Warning

This is an experimental API that is subject to change or deletion.

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

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

Returns

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

Raises
  • TypeError – If mask or x 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” less than the dim of mask.

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

  • ValueError – If the number of elements in x is less than the number required for the updates.

Supported Platforms:

Ascend CPU

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.]