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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more