*Memos:
- My post explains isnan(), isinf(), isposinf() and isneginf().
- My post explains is_floating_point(), is_complex() and is_nonzero().
isfinite() can check if the zero or more elements of a 0D or more D tensor are finity as shown below:
*Memos:
-
isfinite()
can be used with torch or a tensor. - The 1st argument(
tensor
ofint
,float
,complex
orbool
) withtorch
or using a tensor(tensor
ofint
,float
,complex
orbool
) isinput
(Required).
import torch
my_tensor = torch.tensor(float('nan'))
torch.isfinite(input=my_tensor)
my_tensor.isfinite()
# tensor(False)
my_tensor = torch.tensor([float('nan'),
-5,
float('inf'),
8.,
float('-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([[float('nan'), -5, float('inf'), 8.],
[float('-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([[[float('nan'), -5], [float('inf'), 8.]],
[[float('-inf'), 3.+0.j], [3.+7.j, True]]])
torch.isfinite(input=my_tensor)
# tensor([[[False, True], [False, True]],
# [[False, True], [True, True]]])
isreal() can check if the zero or more elements of a 0D or more D tensor are real-valued as shown below:
*Memos:
-
isreal()
can be used withtorch
or a tensor. - The 1st argument(
tensor
ofint
,float
,complex
orbool
) withtorch
or using a tensor(tensor
ofint
,float
,complex
orbool
) isinput
(Required).
import torch
my_tensor = torch.tensor(float('nan'))
torch.isreal(input=my_tensor)
my_tensor.isreal()
# tensor(True)
my_tensor = torch.tensor([float('nan'),
-5,
float('inf'),
8.,
float('-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([[float('nan'), -5, float('inf'), 8.],
[float('-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([[[float('nan'), -5], [float('inf'), 8.]],
[[float('-inf'), 3.+0.j], [3.+7.j, True]]])
torch.isreal(input=my_tensor)
# tensor([[[True, True], [True, True]],
# [[True, True], [False, True]]])
isin() can check if the zero or more elements of the 1st 0D or more D tensor contain the zero or more elements of the 2nd 0D or more D tensor as shown below:
*Memos:
-
isin()
can be used withtorch
but not with a tensor. - The 1st argument(
tensor
orscalar
ofint
orfloat
) withtorch
iselements
(Required). *torch
must use a scalar withoutelements=
. - The 2nd argument(
tensor
orscalar
ofint
orfloat
) withtorch
istest_elements
(Required). *torch
must use a scalar withouttest_elements=
. - The 3rd argument(
bool
) withtorch
isassume_unique
(Optional-Default=False
). *IfTrue
, assumes bothelements
andtest_elements
contain unique elements, which can speed up the calculation. - The 4th argument(
bool
) withtorch
isinvert
(Optional-Default=False
). *IfTrue
, inverts the boolean return tensor, resulting inTrue
values for elements not intest_elements
. - The combination of a scalar(
elements
) and ascalar
(test_elements
) cannot be used.
import torch
tensor1 = torch.tensor([0, 1, 2, 3])
tensor2 = torch.tensor(2)
torch.isin(elements=tensor1, test_elements=tensor2)
torch.isin(tensor1, 2)
# tensor([False, False, True, False])
torch.isin(elements=tensor1, test_elements=tensor2,
assume_unique=True, invert=True)
torch.isin(tensor1, 2,
assume_unique=True, invert=True)
# tensor([True, True, False, True])
torch.isin(elements=tensor2, test_elements=tensor1)
torch.isin(tensor2, 2)
torch.isin(2, test_elements=tensor1)
torch.isin(2, test_elements=tensor2)
# tensor(True)
torch.isin(elements=tensor2, test_elements=tensor1,
assume_unique=True, invert=True)
torch.isin(tensor2, 2,
assume_unique=True, invert=True)
torch.isin(2, test_elements=tensor1,
assume_unique=True, invert=True)
torch.isin(2, test_elements=tensor2,
assume_unique=True, invert=True)
# tensor(False)
tensor1 = torch.tensor([[[0., 1., 2.], [3., 4., 5.]],
[[6., 7., 8.], [9., 10., 11.]]])
tensor2 = torch.tensor([[3., 5.],
[7., 11.]])
torch.isin(elements=tensor1, test_elements=tensor2)
# tensor([[[False, False, False],
# [True, False, True]],
# [[False, True, False],
# [False, False, True]]])
torch.isin(elements=tensor1, test_elements=tensor2,
assume_unique=True, invert=True)
# tensor([[[True, True, True],
# [False, True, False]],
# [[True, False, True],
# [True, True, False]]])
torch.isin(elements=tensor2, test_elements=tensor1)
# tensor([[True, True],
# [True, True]])
torch.isin(elements=tensor2, test_elements=tensor1,
assume_unique=True, invert=True)
# tensor([[False, False],
# [False, False]])
torch.isin(tensor1, 3.)
# tensor([[[False, False, False],
# [True, False, False]],
# [[False, False, False],
# [False, False, False]]])
torch.isin(tensor1, 3.,
assume_unique=True, invert=True)
# tensor([[[True, True, True],
# [False, True, True]],
# [[True, True, True],
# [True, True, True]]])
torch.isin(tensor2, 3.)
# tensor([[True, False],
# [False, False]])
torch.isin(tensor2, 3.,
assume_unique=True, invert=True)
# tensor([[False, True],
# [True, True]])
torch.isin(3., test_elements=tensor1)
torch.isin(3., test_elements=tensor2)
# tensor(True)
torch.isin(3., test_elements=tensor1,
assume_unique=True, invert=True)
torch.isin(3., test_elements=tensor2,
assume_unique=True, invert=True)
# tensor(False)
Top comments (0)