PyTorch torch.nan_to_num function
PyTorch torch reference manual
torch.nan_to_num is a function in PyTorch used to replace NaN values in a tensor with specified values. It can also replace infinite values.
Function definition
torch.nan_to_num(input, nan=0.0, posinf=None, neginf=None, out=None)
Usage examples
Example
import
torch
Replace NaN with 0
x
=
torch.
tensor
(
[
1.0
,
float
(
'nan'
)
,
3.0
,
float
(
'inf'
)
,
float
(
'-inf'
)
]
)
result
=
torch.
nan_to_num
(
x
,
nan
=
0.0
)
(
result
)