mindspore.Tensor.masked_fill

Tensor.masked_fill(mask, value)[source]

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

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

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

Returns

Tensor, has the same type and shape as self.

Raises
  • TypeError – If mask is not a Tensor.

  • TypeError – If dtype of mask is not bool.

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

  • TypeError – If dtype of self tensor or value is not one of float16, float32, int8, int32.

  • TypeError – If dtype of value is different from that of self.

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

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> a = Tensor(np.arange(4)).astype('float32')
>>> print(a)
[0. 1. 2. 3.]
>>> mask = Tensor([False, False, True, True])
>>> print(a.masked_fill(mask, 0.0))
[0. 1. 0. 0.]