Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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([8,
                          5.,
                          torch.nan,
                          torch.inf,
                          3.+0.j,
                          3.+7.j,
                          complex(torch.nan, torch.inf),
                          True])
torch.isreal(input=my_tensor)
my_tensor.isreal()
# tensor([True, True, True, True, True, False, False, True])

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

my_tensor = torch.tensor([[[8,
                            5.],
                           [torch.nan,
                            torch.inf]],
                          [[3.+0.j,
                            3.+7.j],
                           [complex(torch.nan, torch.inf),
                            True]]])
torch.isreal(input=my_tensor)
# tensor([[[True, True], [True, True]],
#         [[True, False], [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([8,
                          5.,
                          torch.nan,
                          torch.inf,
                          3.+0.j,
                          3.+7.j,
                          complex(torch.nan, torch.inf),
                          True])
torch.isnan(input=my_tensor)
my_tensor.isreal()
# tensor([False, False, True, False, False, False, True, False])

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

my_tensor = torch.tensor([[[8,
                            5.],
                           [torch.nan,
                            torch.inf]],
                          [[3.+0.j,
                            3.+7.j],
                           [complex(torch.nan, torch.inf),
                            True]]])
torch.isnan(input=my_tensor)
# tensor([[[False, False], [True, False]],
#         [[False, False], [True, 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([8,
                          5.,
                          torch.nan,
                          torch.inf,
                          3.+0.j,
                          3.+7.j,
                          complex(torch.nan, torch.inf),
                          True])
torch.isfinite(input=my_tensor)
my_tensor.isfinite()
# tensor([True, True, False, False, True, True, False, True])

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

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

Top comments (0)