nan_to_num() in PyTorch

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 respe…


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)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-20T17:03:04+00:00) nan_to_num() in PyTorch. Retrieved from https://www.scien.cx/2024/07/20/nan_to_num-in-pytorch/

MLA
" » nan_to_num() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Saturday July 20, 2024, https://www.scien.cx/2024/07/20/nan_to_num-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Saturday July 20, 2024 » nan_to_num() in PyTorch., viewed ,<https://www.scien.cx/2024/07/20/nan_to_num-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » nan_to_num() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/20/nan_to_num-in-pytorch/
CHICAGO
" » nan_to_num() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/20/nan_to_num-in-pytorch/
IEEE
" » nan_to_num() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/20/nan_to_num-in-pytorch/. [Accessed: ]
rf:citation
» nan_to_num() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/20/nan_to_num-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.