PyTorch torch.nan_to_num Function
Pytorch torch Reference Manual
torch.nan_to_numIt is a function in PyTorch used to replace NaN values in tensors with a specified value. 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)
print(result)
# 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)
print(result)
Other Extensions