mindspore.ops.nan_to_num

View Source On Gitee
mindspore.ops.nan_to_num(input, nan=None, 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.

Warning

For Ascend, it is only supported on Atlas A2 Training Series Products. This is an experimental API that is subject to change or deletion.

Parameters
  • input (Tensor) – The shape of tensor is \((input_1, input_2, ..., input_R)\).

  • nan (number, optional) – The replace value of NaN. Default value is None.

  • posinf (number, optional) – 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 (number, optional) – 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.

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]