mindspore.ops.nan_to_num

mindspore.ops.nan_to_num(input, nan=0.0, posinf=None, neginf=None)[source]

Replace the NaN, positive infinity and negative infinity values in 'input' with the specified values in nan, posinf and neginf respectively.

Parameters
  • input (Tensor) – The shape of tensor is \((input_1, input_2, ..., input_R)\). With float32 or float16 data type.

  • nan (float) – The replace value of 'NaN'. Default value is 0.0.

  • posinf (float) – the value to replace positive infinity values with. Default: None, replacing positive infinity with the maximum value supported by the data type of input.

  • neginf (float) – the value to replace negative infinity values with. Default: None, replacing negative infinity with the minimum value supported by the data type of input.

Returns

Tensor, has the same shape and dtype as the input.

Raises
  • TypeError – If input is not a Tensor.

  • TypeError – If dtype of input is not float16 or float32.

Supported Platforms:

Ascend CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input = Tensor(np.array([float('nan'), float('inf'), -float('inf'), 5.0]), mindspore.float32)
>>> output = ops.nan_to_num(input, 1.0, 2.0, 3.0)
>>> print(output)
[1.  2.  3.  5.0]