From c46824cd7b5c176e307de4dceaa3b526c812a705 Mon Sep 17 00:00:00 2001 From: Rishabh Ranjan Singh Date: Tue, 18 Nov 2025 12:24:37 +0530 Subject: [PATCH 1/2] docs: Add PyTorch tensor method .logical_xor() entry (#7787) --- .../terms/logical-xor/logical-xor.md | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md diff --git a/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md b/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md new file mode 100644 index 00000000000..e3c027e93ef --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md @@ -0,0 +1,99 @@ +--- +Title: '.logical_xor()' +Description: 'Computes the element-wise logical XOR (Exclusive OR) between two tensors.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Functions' + - 'PyTorch' + - 'Tensor' + - 'Boolean Logic' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- + +The **`.logical_xor()`** method computes the **Exclusive OR (XOR)** operation element-wise between two input tensors. The resulting tensor contains Boolean (`True` or `False`) values. + +The XOR operation returns `True` if **exactly one** of the corresponding elements in the input tensors evaluates to `True`. It returns `False` if both elements are `True` or if both are `False` (zero). + +Inputs are treated as Boolean: non-zero values are interpreted as `True`, and zero values are interpreted as `False`. + +## Syntax + +The method is called on the first tensor and takes the second tensor as an argument. + +```pseudo +input_tensor_a.logical_xor(input_tensor_b) +``` + +## Parameters + +* `input_tensor_b` (Tensor): The second tensor to compare. Must be broadcastable with the first tensor. + +## Return Value + +Returns a tensor of type `torch.bool` (Boolean) containing the element-wise result of the logical XOR operation. + +## Example + +This example demonstrates the logical XOR operation using various combinations of non-zero (True) and zero (False) values. + +```python +import torch + +# Tensor A: +# [True, False] +# [True, True] +tensor_a = torch.tensor([[1, 0], [5, 10]]) + +# Tensor B: +# [True, True] +# [False, True] +tensor_b = torch.tensor([[2, 3], [0, 1]]) + +# Compute logical XOR: A XOR B +# ----------------------------- +# [1 XOR 2] -> True XOR True -> False +# [0 XOR 3] -> False XOR True -> True +# [5 XOR 0] -> True XOR False -> True +# [10 XOR 1] -> True XOR True -> False +result_tensor = tensor_a.logical_xor(tensor_b) + +print("Tensor A:\n", tensor_a) +print("\nTensor B:\n", tensor_b) +print("\nA.logical_xor(B) Result (Boolean):\n", result_tensor) +``` + +Output: + +``` +Tensor A: +tensor([[ 1, 0], + [ 5, 10]]) + +Tensor B: +tensor([[ 2, 3], + [ 0, 1]]) + +A.logical_xor(B) Result (Boolean): +tensor([[False, True], + [ True, False]]) +``` + +## Codebyte + +Use the Codebyte below to experiment with the `logical_xor()` method on two 1-D tensors. + +```python +import torch + +x = torch.tensor([1, 0, 10, 0]) +y = torch.tensor([0, 5, 0, 0]) + +# Compute x logical XOR y +xor_result = x.logical_xor(y) + +print(xor_result) +``` \ No newline at end of file From c913c0fd2295767acf87532937c3be488ac22b25 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 18 Nov 2025 13:11:11 +0530 Subject: [PATCH 2/2] updated the codebyte as Pytorch entries dont have codebytes --- .../terms/logical-xor/logical-xor.md | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md b/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md index e3c027e93ef..71c7cc9fa1a 100644 --- a/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md +++ b/content/pytorch/concepts/tensor-operations/terms/logical-xor/logical-xor.md @@ -5,10 +5,10 @@ Subjects: - 'Computer Science' - 'Data Science' Tags: + - 'Booleans' - 'Functions' - 'PyTorch' - 'Tensor' - - 'Boolean Logic' CatalogContent: - 'intro-to-py-torch-and-neural-networks' - 'paths/data-science' @@ -16,37 +16,35 @@ CatalogContent: The **`.logical_xor()`** method computes the **Exclusive OR (XOR)** operation element-wise between two input tensors. The resulting tensor contains Boolean (`True` or `False`) values. -The XOR operation returns `True` if **exactly one** of the corresponding elements in the input tensors evaluates to `True`. It returns `False` if both elements are `True` or if both are `False` (zero). - -Inputs are treated as Boolean: non-zero values are interpreted as `True`, and zero values are interpreted as `False`. +According to the XOR rule, an element is `True` only if exactly one of the corresponding inputs is truthy. Inputs are interpreted as Boolean values where non-zero means `True` and zero means `False`. ## Syntax -The method is called on the first tensor and takes the second tensor as an argument. - ```pseudo -input_tensor_a.logical_xor(input_tensor_b) +torch.logical_xor(input, other, out) ``` -## Parameters +**Parameters:** -* `input_tensor_b` (Tensor): The second tensor to compare. Must be broadcastable with the first tensor. +- `input`: A boolean tensor (or a tensor that can be cast to boolean). +- `other`: A boolean tensor of the same shape or broadcastable to `input`. +- `out` (optional): A tensor for storing the result in-place. -## Return Value +**Return value:** -Returns a tensor of type `torch.bool` (Boolean) containing the element-wise result of the logical XOR operation. +Returns a boolean tensor where each element is `True` when exactly one of the corresponding elements in `input` and `other` is true. -## Example +## Example 1 -This example demonstrates the logical XOR operation using various combinations of non-zero (True) and zero (False) values. +This example shows the logical XOR applied to two 2×2 tensors: -```python +```py import torch -# Tensor A: +# Tensor A: # [True, False] # [True, True] -tensor_a = torch.tensor([[1, 0], [5, 10]]) +tensor_a = torch.tensor([[1, 0], [5, 10]]) # Tensor B: # [True, True] @@ -59,16 +57,16 @@ tensor_b = torch.tensor([[2, 3], [0, 1]]) # [0 XOR 3] -> False XOR True -> True # [5 XOR 0] -> True XOR False -> True # [10 XOR 1] -> True XOR True -> False -result_tensor = tensor_a.logical_xor(tensor_b) +result_tensor = torch.logical_xor(tensor_a, tensor_b) print("Tensor A:\n", tensor_a) print("\nTensor B:\n", tensor_b) print("\nA.logical_xor(B) Result (Boolean):\n", result_tensor) ``` -Output: +The output of this code is: -``` +```shell Tensor A: tensor([[ 1, 0], [ 5, 10]]) @@ -82,18 +80,24 @@ tensor([[False, True], [ True, False]]) ``` -## Codebyte +## Example 2 -Use the Codebyte below to experiment with the `logical_xor()` method on two 1-D tensors. +This example demonstrates XOR on two 1-D tensors so each result maps cleanly to a single pair of elements: -```python +```py import torch x = torch.tensor([1, 0, 10, 0]) y = torch.tensor([0, 5, 0, 0]) # Compute x logical XOR y -xor_result = x.logical_xor(y) +xor_result = torch.logical_xor(x, y) print(xor_result) -``` \ No newline at end of file +``` + +The output of this code is: + +```shell +tensor([ True, True, True, False]) +```