DEV Community

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

Posted on • Edited on

isinf, isposinf and isneginf in PyTorch

Buy Me a Coffee

*Memos:

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

*Memos:

  • isinf() 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.isinf(input=my_tensor)
my_tensor.isinf()
# tensor([False, False, False, True, 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.isinf(input=my_tensor)
# tensor([[False, False, False, True],
#         [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.isinf(input=my_tensor)
# tensor([[[False, False], [False, True]],
#         [[False, False], [True, False]]])
Enter fullscreen mode Exit fullscreen mode

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

*Memos:

  • isposinf() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

my_tensor = torch.tensor([8,
                          5.,
                          torch.nan,
                          torch.inf,
                          3.,
                          3.7,
                          -torch.inf,
                          True])
torch.isposinf(input=my_tensor)
my_tensor.isposinf()
# tensor([False, False, False, True, False, False, False, False])

my_tensor = torch.tensor([[8,
                           5.,
                           torch.nan,
                           torch.inf],
                          [3.,
                           3.7,
                           -torch.inf,
                           True]])
torch.isposinf(input=my_tensor)
# tensor([[False, False, False,  True],
#         [False, False, False, False]])

my_tensor = torch.tensor([[[8,
                            5.],
                           [torch.nan,
                            torch.inf]],
                          [[3.,
                            3.7],
                           [-torch.inf,
                            True]]])
torch.isposinf(input=my_tensor)
# tensor([[[False, False], [False, True]],
#         [[False, False], [False, False]]])
Enter fullscreen mode Exit fullscreen mode

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

*Memos:

  • isneginf() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

my_tensor = torch.tensor([8,
                          5.,
                          torch.nan,
                          torch.inf,
                          3.,
                          3.7,
                          -torch.inf,
                          True])
torch.isneginf(input=my_tensor)
my_tensor.isneginf()
# tensor([False, False, False, False, False, False, True, False])

my_tensor = torch.tensor([[8,
                           5.,
                           torch.nan,
                           torch.inf],
                          [3.,
                           3.7,
                           -torch.inf,
                           True]])
torch.isneginf(input=my_tensor)
# tensor([[False, False, False, False],
#         [False, False, True, False]])

my_tensor = torch.tensor([[[8,
                            5.],
                           [torch.nan,
                            torch.inf]],
                          [[3.,
                            3.7],
                           [-torch.inf,
                            True]]])
torch.isneginf(input=my_tensor)
# tensor([[[False, False], [False, False]],
#         [[False, False], [True, False]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)