nan_to_num() in PyTorch



This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee☕

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-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

my_tensor = torch.tensor([float('-inf'), 7., -5., float('inf'),
                          8., float('nan'), float('inf'), float('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([[float('-inf'), 7., -5., float('inf')],
                          [8., float('nan'), float('inf'), float('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([[[float('-inf'), 7.],
                           [-5., float('inf')]],
                          [[8., float('nan')],
                           [float('inf'), float('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('-inf+infj'), 7.+0.j,
                          -5.+0.j, complex('inf-infj'),
                          8.+0.j, complex('nan+nanj'),
                          complex('inf'), float('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('-inf+infj'), 7.+0.j,
                           -5.+0.j, complex('inf-infj')],
                          [8.+0.j, complex('nan+nanj'),
                           complex('inf'), float('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('-inf+infj'), 7.+0.j],
                           [-5.+0.j, complex('inf-infj')]],
                          [[8.+0.j, complex('nan+nanj')],
                           [complex('inf'), float('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]]])


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)