DEV Community

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

Posted on • Updated on

isreal, isnan and isfinite in PyTorch

Buy Me a Coffee

*Memos:

isreal() can check if the zero or more elements of a 0D or more D tensor are real-valued, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:

  • isreal() 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).
import torch

my_tensor = torch.tensor([torch.nan,
                          -5,
                          torch.inf,
                          8.,
                          -torch.inf,
                          3.+0.j,
                          3.+7.j,
                          True])
torch.isreal(input=my_tensor)
my_tensor.isreal()
# tensor([True, True, True, True, True, True, False, True])

my_tensor = torch.tensor([[torch.nan, -5, torch.inf, 8.],
                          [-torch.inf, 3.+0.j, 3.+7.j, True]])
torch.isreal(input=my_tensor)
# tensor([[True, True, True, True],
#         [True, True, False, True]])

my_tensor = torch.tensor([[[torch.nan, -5], [torch.inf, 8.]],
                          [[-torch.inf, 3.+0.j], [3.+7.j, True]]])
torch.isreal(input=my_tensor)
# tensor([[[True, True], [True, True]],
#         [[True, True], [False, True]]])
Enter fullscreen mode Exit fullscreen mode

isnan() can check if the zero or more elements of a 0D or more D tensor are NaN(Not a Number), getting the 0D or more D tensor of zero or more boolean values shown below:

*Memos:

  • isnan() 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).
import torch

my_tensor = torch.tensor([torch.nan,
                          -5,
                          torch.inf,
                          8.,
                          -torch.inf,
                          3.+0.j,
                          3.+7.j,
                          True])
torch.isnan(input=my_tensor)
my_tensor.isreal()
# tensor([True, False, False, False, False, False, False, False])

my_tensor = torch.tensor([[torch.nan, -5, torch.inf, 8.],
                          [-torch.inf, 3.+0.j, 3.+7.j, True]])
torch.isnan(input=my_tensor)
# tensor([[True, False, False, False],
#         [False, False, False, False]])

my_tensor = torch.tensor([[[torch.nan, -5], [torch.inf, 8.]],
                          [[-torch.inf, 3.+0.j], [3.+7.j, True]]])
torch.isnan(input=my_tensor)
# tensor([[[True, False], [False, False]],
#         [[False, False], [False, False]]])
Enter fullscreen mode Exit fullscreen mode

isfinite() can check if the zero or more elements of a 0D or more D tensor are finity, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:

  • isfinite() 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).
import torch

my_tensor = torch.tensor([torch.nan,
                          -5,
                          torch.inf,
                          8.,
                          -torch.inf,
                          3.+0.j,
                          3.+7.j,
                          True])
torch.isfinite(input=my_tensor)
my_tensor.isfinite()
# tensor([False, True, False, True, False, True, True, True])

my_tensor = torch.tensor([[torch.nan, -5, torch.inf, 8.],
                          [-torch.inf, 3.+0.j, 3.+7.j, True]])
torch.isfinite(input=my_tensor)
# tensor([[False, True, False, True],
#         [False, True, True, True]])

my_tensor = torch.tensor([[[torch.nan, -5], [torch.inf, 8.]],
                          [[-torch.inf, 3.+0.j], [3.+7.j, True]]])
torch.isfinite(input=my_tensor)
# tensor([[[False, True], [False, True]],
#         [[False, True], [True, True]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)