
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compute Element-wise Logical XOR of Tensors in PyTorch
torch.logical_xor() computes the element-wise logical XOR of the given two input tensors. In a tensor, the elements with zero values are treated as False and non-zero elements are treated as True. It takes two tensors as input parameters and returns a tensor with values after computing the logical XOR.
Syntax
torch.logical_xor(tensor1, tensor2)
where tensor1 and tensor2 are the two input tensors.
Steps
To compute element-wise logical XOR of given input tensors, one could follow the steps given below −
Import the torch library. Make sure you have it already installed.
Create two tensors, tensor1 and tensor2, and print the tensors.
Compute torch.logical_xor(tesnor1, tesnor2) and assign the value to a variable.
Print the final result after performing the element-wise logical XOR operation.
Example 1
# import torch library import torch # define two Boolean tensors tensor1 = torch.tensor([True, True, True, False, False]) tensor2 = torch.tensor([True, False, False, True, True]) # display the defined tensors print("Tensor 1:
", tensor1) print("Tensor 2:
", tensor2) # compute XOR of tensor1 and tensor2 and display tensor_xor = torch.logical_xor(tensor1, tensor2) print("XOR result:
", tensor_xor)
Output
Tensor 1: tensor([ True, True, True, False, False]) Tensor 2: tensor([ True, False, False, True, True]) XOR result: tensor([False, True, True, True, True])
Example 2
# import torch library import torch # define two tensors tensor1 = torch.tensor([True, True, True, False, False]) tensor2 = torch.tensor([1, 0, 123, 23, -12]) # display the defined tensors print("Tensor 1:
", tensor1) print("Tensor 2:
", tensor2) # compute XOR of tensor1 and tensor2 and display tensor_xor = torch.logical_xor(tensor1, tensor2) print("XOR result:
", tensor_xor)
Output
Tensor 1: tensor([ True, True, True, False, False]) Tensor 2: tensor([ 1, 0, 123, 23, -12]) XOR result: tensor([False, True, False, True, True])
Example 3
# import torch library import torch # define two tensors tensor1 = torch.tensor([12, 3, 11, 21, -12]) tensor2 = torch.tensor([1, 0, 123, 0, -2]) # display the defined tensors print("Tensor 1:
", tensor1) print("Tensor 2:
", tensor2) # compute XOR of tensor1 and tensor2 and display tensor_xor = torch.logical_xor(tensor1, tensor2) print("XOR result:
", tensor_xor)
Output
Tensor 1: tensor([ 12, 3, 11, 21, -12]) Tensor 2: tensor([ 1, 0, 123, 0, -2]) XOR result: tensor([False, True, False, True, False])
Advertisements