- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PyTorch – How to compute element-wise logical XOR of tensors?
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])
- Related Articles
- How to perform element-wise addition on tensors in PyTorch?
- How to perform element-wise subtraction on tensors in PyTorch?
- How to perform element-wise multiplication on tensors in PyTorch?
- How to perform element-wise division on tensors in PyTorch?
- How to compute elementwise logical AND, OR and NOT of given input tensors in PyTorch?
- PyTorch – How to compute element-wise entropy of an input tensor?
- Compute the bit-wise XOR of two Numpy arrays element-wise
- Compute the bit-wise XOR of two One-Dimensional Numpy arrays element-wise
- Compute the bit-wise XOR of two boolean arrays element-wise in Numpy
- Compute the bit-wise XOR of two Two-Dimensional arrays element-wise in Numpy
- How to compute the element-wise angle of the given input tensor in PyTorch?
- How to compute the Cosine Similarity between two tensors in PyTorch?
- Compute the bit-wise XOR of a 1D and a 2D array element-wise in Numpy
- How to compute bitwise AND, OR and NOT of given input tensors in PyTorch?
- Compute the truth value of an array XOR another array element-wise in Numpy

Advertisements