DEV Community

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

Posted on

is_floating_point(), is_complex() and is_nonzero() in PyTorch

*Memos:

is_floating_point() can check a 0D or more D tensor is float type as shown below:

*Memos:

  • is_floating_point() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
import torch

my_tensor = torch.tensor(8.)
my_tensor = torch.tensor(float('nan'))
my_tensor = torch.tensor(float('inf'))
my_tensor = torch.tensor(float('-inf'))

torch.is_floating_point(input=my_tensor)
my_tensor.is_floating_point()
# True

my_tensor = torch.tensor(-5)
my_tensor = torch.tensor(3.+0.j)
my_tensor = torch.tensor(3.+7.j)
my_tensor = torch.tensor(True)

torch.is_floating_point(input=my_tensor)
# False

my_tensor = torch.tensor([8.,
                          float('nan'),
                          float('inf'),
                          float('-inf')])
torch.is_floating_point(input=my_tensor)
# True

my_tensor = torch.tensor([[8., float('nan')],
                          [float('inf'), float('-inf')]])
torch.is_floating_point(input=my_tensor)
# True

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

is_complex() can check a 0D or more D tensor is complex type as shown below:

*Memos:

  • is_complex() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
import torch

my_tensor = torch.tensor(3.+0.j)
my_tensor = torch.tensor(3.+7.j)

torch.is_complex(input=my_tensor)
my_tensor.is_complex()
# True

my_tensor = torch.tensor(-5)
my_tensor = torch.tensor(8.)
my_tensor = torch.tensor(float('nan'))
my_tensor = torch.tensor(float('inf'))
my_tensor = torch.tensor(float('-inf'))
my_tensor = torch.tensor(True)

torch.is_complex(input=my_tensor)
# False

my_tensor = torch.tensor([3.+0.j, 3.+7.j])

torch.is_complex(input=my_tensor)
# True

my_tensor = torch.tensor([[-5, 8., True],
                          [float('nan'), float('inf'), float('-inf')]])
my_tensor = torch.tensor([[-5, 8., float('nan')],
                          [float('inf'), float('-inf'), True]])
torch.is_complex(input=my_tensor)
# False

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

is_nonzero() can check if only one element of a 0D or more D tensor is a non-zero element as shown below:

*Memos:

  • is_nonzero() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • There must be only one element in a tensor.
import torch

my_tensor = torch.tensor(-5)
my_tensor = torch.tensor(8.)
my_tensor = torch.tensor([float('nan')])
my_tensor = torch.tensor([float('inf')])
my_tensor = torch.tensor([[float('-inf')]])
my_tensor = torch.tensor([[3.+0.j]])
my_tensor = torch.tensor([[[3.+7.j]]])
my_tensor = torch.tensor(True)

torch.is_nonzero(input=my_tensor)
my_tensor.is_nonzero()
# True

my_tensor = torch.tensor(0)
my_tensor = torch.tensor([0.0])
my_tensor = torch.tensor([[0.+.0j]])
my_tensor = torch.tensor([[[False]]])

torch.is_nonzero(input=my_tensor)
# False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)