DEV Community

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

Posted on • Edited 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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay