nan_to_num

paddle. nan_to_num ( x, nan=0.0, posinf=None, neginf=None, name=None ) [source]

Replaces NaN, positive infinity, and negative infinity values in input tensor.

Parameters
  • x (Tensor) – An N-D Tensor, the data type is float32, float64.

  • nan (float, optional) – the value to replace NaNs with. Default is 0.

  • posinf (float, optional) – if a Number, the value to replace positive infinity values with. If None, positive infinity values are replaced with the greatest finite value representable by input’s dtype. Default is None.

  • neginf (float, optional) – if a Number, the value to replace negative infinity values with. If None, negative infinity values are replaced with the lowest finite value representable by input’s dtype. Default is None.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Results of nan_to_num operation input Tensor x.

Return type

Tensor

Examples

import paddle

x = paddle.to_tensor([float('nan'), 0.3, float('+inf'), float('-inf')], dtype='float32')
out1 = paddle.nan_to_num(x)  # [0, 0.3, 3.4028235e+38, -3.4028235e+38]
out2 = paddle.nan_to_num(x, nan=1)  # [1, 0.3, 3.4028235e+38, -3.4028235e+38]
out3 = paddle.nan_to_num(x, posinf=5)  # [0, 0.3, 5, -3.4028235e+38]
out4 = paddle.nan_to_num(x, nan=10, neginf=-99)  # [10, 0.3, 3.4028235e+38, -99]