logical_and() and logical_or() in PyTorch

*My post explains logical_xor() and logical_not().

logical_and() can do logical AND with two of the 0D or more D tensors of zero or more elements, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:

logical_and()…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

*My post explains logical_xor() and logical_not().

logical_and() can do logical AND with two of the 0D or more D tensors of zero or more elements, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:

  • logical_and() 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).
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor of int, float, complex or bool).
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • Zero is True and nonzero is False.
import torch

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_and(input=tensor1, other=tensor2)
tensor1.logical_and(other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[True, False, True, False],
#         [False, True, False, True]])

tensor1 = torch.tensor(False)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[False, False, False, False],
#         [False, False, False, False]])

tensor1 = torch.tensor([True, False])
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[[True, False], [True, False]],
#         [[False, False], [False, False]]])

tensor1 = torch.tensor([7, 0])
tensor2 = torch.tensor([[[1, 0], [-1, 0]],
                        [[0, 2], [0, -2]]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[[True, False], [True, False]],
#         [[False, False], [False, False]]])

tensor1 = torch.tensor([7., 0.3])
tensor2 = torch.tensor([[[1.3, 0.3], [-1.0, 0.0]],
                        [[0.3, 2.3], [0.0, -2.0]]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[[True, True], [True, False]],
#         [[True, True], [False, True]]])

tensor1 = torch.tensor([7.+3.j, 0.+3.j])
tensor2 = torch.tensor([[[1.+3.j, 0.+3.j], [-1.+0.j, 0.+0.j]],
                        [[0.+3.j, 2.+3.j], [0.+0.j, -2.+0.j]]])
torch.logical_and(input=tensor1, other=tensor2)
torch.logical_and(input=tensor2, other=tensor1)
# tensor([[[True, True], [True, False]],
#         [[True, True], [False, True]]])

logical_or() can do logical OR with two of the 0D or more D tensors of zero or more elements, getting the 0D or more D tensor of zero or more boolean values as shown below:

*Memos:

  • logical_or() 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).
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor of int, float, complex or bool).
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • Zero is True and nonzero is False.
import torch

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_or(input=tensor1, other=tensor2)
tensor1.logical_or(other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[True, True, True, True],
#         [True, True, True, True]])

tensor1 = torch.tensor(False)
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[True, False, True, False],
#         [False, True, False, True]])

tensor1 = torch.tensor([True, False])
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[[True, False], [True, False]],
#         [[True, True], [True, True]]])

tensor1 = torch.tensor([7, 0])
tensor2 = torch.tensor([[[1, 0], [-1, 0]],
                        [[0, 2], [0, -2]]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[[True, False], [True, False]],
#         [[True, True], [True, True]]])

tensor1 = torch.tensor([7.3, 0.3])
tensor2 = torch.tensor([[[1.3, 0.3], [-1.0, 0.0]],
                        [[0.3, 2.3], [0.0, -2.0]]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[[True, True], [True, True]],
#         [[True, True], [True, True]]])

tensor1 = torch.tensor([7.+3.j, 0.+3.j])
tensor2 = torch.tensor([[[1.+3.j, 0.+3.j], [-1.+0.j, 0.+0.j]],
                        [[0.+3.j, 2.+3.j], [0.+0.j, -2.+0.j]]])
torch.logical_or(input=tensor1, other=tensor2)
torch.logical_or(input=tensor2, other=tensor1)
# tensor([[[True, True], [True, True]],
#         [[True, True], [True, True]]])


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-15T01:50:50+00:00) logical_and() and logical_or() in PyTorch. Retrieved from https://www.scien.cx/2024/07/15/logical_and-and-logical_or-in-pytorch/

MLA
" » logical_and() and logical_or() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Monday July 15, 2024, https://www.scien.cx/2024/07/15/logical_and-and-logical_or-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Monday July 15, 2024 » logical_and() and logical_or() in PyTorch., viewed ,<https://www.scien.cx/2024/07/15/logical_and-and-logical_or-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » logical_and() and logical_or() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/15/logical_and-and-logical_or-in-pytorch/
CHICAGO
" » logical_and() and logical_or() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/15/logical_and-and-logical_or-in-pytorch/
IEEE
" » logical_and() and logical_or() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/15/logical_and-and-logical_or-in-pytorch/. [Accessed: ]
rf:citation
» logical_and() and logical_or() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/15/logical_and-and-logical_or-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.