DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

nan_to_num in PyTorch

Buy Me a Coffee

*My post explains torch.nan and torch.inf.

nan_to_num() can get the 0D or more D tensor of zero or more elements, replacing zero or more NaNs(Not a Numbers), positive infinities and negative infinities with zero or more zeros, the greatest finities and the least finities respectively(Default) or specified values from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • nan_to_num() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is nan(Optional-Default:Zero-Type:int, float or bool).
  • The 3rd argument with torch or the 2nd argument with a tensor is posinf(Optional-Default:The greatest finite-Type:int, float or bool).
  • The 4th argument with torch or the 2nd argument with a tensor is neginf(Optional-Default:The lowest finite-Type:int, float or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

my_tensor = torch.tensor([-torch.inf, 7., -5., torch.inf,
                          8., torch.nan, torch.inf, torch.nan])
torch.nan_to_num(input=my_tensor)
my_tensor.nan_to_num()
# tensor([-3.4028e+38, 7.0000e+00, -5.0000e+00, 3.4028e+38,
#         8.0000e+00, 0.0000e+00, 3.4028e+3, 0.0000e+00])

torch.nan_to_num(input=my_tensor, nan=2., posinf=-6., neginf=9.)
# tensor([9., 7., -5., -6., 8., 2., -6., 2.])

my_tensor = torch.tensor([[-torch.inf, 7., -5., torch.inf],
                          [8., torch.nan, torch.inf, torch.nan]])
torch.nan_to_num(input=my_tensor, nan=2., posinf=-6., neginf=9.)
# tensor([[9., 7., -5., -6.],
#         [8., 2., -6., 2.]])

my_tensor = torch.tensor([[[-torch.inf, 7.], [-5., torch.inf]],
                          [[8., torch.nan], [torch.inf, torch.nan]]])
torch.nan_to_num(input=my_tensor, nan=2., posinf=-6., neginf=9.)
# tensor([[[9., 7.], [-5., -6.]],
#         [[8., 2.], [-6., 2.]]])

my_tensor = torch.tensor([complex(-torch.inf, torch.inf), 7.+0.j,
                          -5.+0.j, complex(torch.inf, -torch.inf),
                          8.+0.j, complex(torch.nan, torch.nan),
                          complex(torch.inf), complex(torch.nan)])
torch.nan_to_num(input=my_tensor)
# tensor([-3.4028e+38+3.4028e+38j, 7.0000e+00+0.0000e+00j,
#         -5.0000e+00+0.0000e+00j, 3.4028e+38-3.4028e+38j,
#         8.0000e+00+0.0000e+00j, 0.0000e+00+0.0000e+00j,
#         3.4028e+38+0.0000e+00j, 0.0000e+00+0.0000e+00j])

torch.nan_to_num(input=my_tensor, nan=2., posinf=-6., neginf=9.)
# tensor([9.-6.j, 7.+0.j,
#         -5.+0.j, -6.+9.j,
#         8.+0.j, 2.+2.j,
#         -6.+0.j, 2.+0.j])

my_tensor = torch.tensor([[complex(-torch.inf, torch.inf), 7.+0.j,
                           -5.+0.j, complex(torch.inf, -torch.inf)],
                          [8.+0.j, complex(torch.nan, torch.nan),
                           complex(torch.inf), complex(torch.nan)]])
torch.nan_to_num(input=my_tensor, nan=2., posinf=-6., neginf=9.)
# tensor([[9.-6.j, 7.+0.j,
#          -5.+0.j, -6.+9.j],
#         [8.+0.j, 2.+2.j,
#          -6.+0.j, 2.+0.j]])

my_tensor = torch.tensor([[[complex(-torch.inf, torch.inf), 7.+0.j],
                           [-5.+0.j, complex(torch.inf, -torch.inf)]],
                          [[8.+0.j, complex(torch.nan, torch.nan)],
                           [complex(torch.inf), complex(torch.nan)]]])
torch.nan_to_num(input=my_tensor, nan=2., posinf=-6., neginf=9.)
# tensor([[[9.-6.j, 7.+0.j],
#          [-5.+0.j, -6.+9.j]],
#         [[8.+0.j, 2.+2.j],
#          [-6.+0.j, 2.+0.j]]])

my_tensor = torch.tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])

torch.nan_to_num(input=my_tensor, nan=2, posinf=-6, neginf=9)
# tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.nan_to_num(input=my_tensor, nan=True, posinf=False, neginf=True)
# tensor([[[True, False], [True, False]],
#         [[False, True], [False, True]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)