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)
>>> out1
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 0.                                      ,
  0.30000001                              ,
  340282346638528859811704183484516925440.,
 -340282346638528859811704183484516925440.])
>>> out2 = paddle.nan_to_num(x, nan=1)
>>> out2
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 1.                                      ,
  0.30000001                              ,
  340282346638528859811704183484516925440.,
 -340282346638528859811704183484516925440.])
>>> out3 = paddle.nan_to_num(x, posinf=5)
>>> out3
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 0.                                      ,
  0.30000001                              ,
  5.                                      ,
 -340282346638528859811704183484516925440.])
>>> out4 = paddle.nan_to_num(x, nan=10, neginf=-99)
>>> out4
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[ 10.                                    ,
  0.30000001                             ,
 340282346638528859811704183484516925440.,
 -99.                                    ])