mindspore.ops.masked_fill

View Source On Gitee
mindspore.ops.masked_fill(input_x, mask, value)[source]

Fills elements of Tensor with value where mask is True. The shapes of input_x and mask need to be the same or broadcastable.

Parameters
  • input_x (Tensor) – The source Tensor whose data type is one of bool, uint8, int8, int16, int32, int64, float16, float32, float64, complex64, complex128.

  • mask (Tensor[bool]) – The boolean mask.

  • value (Union[Number, Tensor]) – The value to fill in with, which dtype is the same as input_x.

Returns

Tensor, has the same type and shape as input_x.

Raises
  • TypeError – If dtype of mask is not bool.

  • TypeError – If input_x or mask is not a Tensor.

  • ValueError – If the shapes of input_x and mask could not be broadcast.

  • TypeError – If dtype of input_x or value is not one of bool, uint8, int8, int16, int32, int64, float16, float32, float64, complex64, complex128.

  • TypeError – If dtype of value is different from that of input_x in CPU and GPU.

  • TypeError – If value is neither float number nor Tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input_x = Tensor(np.array([1., 2., 3., 4.]), mindspore.float32)
>>> mask = Tensor(np.array([True, True, False, True]), mindspore.bool_)
>>> output = ops.masked_fill(input_x, mask, 0.5)
>>> print(output)
[0.5 0.5 3.  0.5]