From 40737e4d73d32a7e036c18b05d4de790c9a3a8f7 Mon Sep 17 00:00:00 2001 From: Bhuswarna Kashyap Date: Thu, 23 Oct 2025 15:38:40 +0530 Subject: [PATCH 1/5] add PyTorch logical-not --- .../terms/logical-not/logical-not.md | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md diff --git a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md new file mode 100644 index 00000000000..06a8226fb4e --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md @@ -0,0 +1,88 @@ +````markdown +--- +Title: '.logical_not()' +Description: 'Performs element-wise logical NOT on boolean tensors, returning a tensor where each element is the boolean negation of the input.' +Subjects: + - 'AI' + - 'Data Science' + - 'Computer Science' + - 'Machine Learning' +Tags: + - 'Booleans' + - 'PyTorch' + - 'Tensor' + - 'Functions' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- + +In PyTorch, the **`.logical_not()`** function computes the element-wise logical negation of a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). It returns a tensor where each element is `True` when the corresponding input element is `False` and vice versa. If the input tensor is not a boolean tensor, then zeros are treated as `False` and non-zeros are treated as `True`. + +This operation is often used in masking, boolean indexing, and constructing complement conditions. + +## Syntax + +```pseudo +torch.logical_not(input, *, out=None) +``` + +**Parameters:** + +- `input`: A tensor of any numeric or boolean dtype. The function returns a boolean tensor. + - `out` (Optional): A tensor to store the result. Must have the same shape as the expected output. The typical dtype for the output is boolean (`torch.bool`), but integer dtypes that can represent `0`/`1` (for example, `torch.int16`) are accepted; results will be stored as `1` (True) and `0` (False) in that tensor. + +**Return value:** + +Returns a new boolean tensor containing the element-wise logical negation of `input`. + +## Example + +The following example shows typical usage of `.logical_not()` for masking and boolean inversion: + +```py +import torch + +# Create a boolean tensor +mask = torch.tensor([True, False, True, False]) + +# Element-wise logical NOT +inv = torch.logical_not(mask) + +print('mask:', mask) +print('logical_not(mask):', inv) + +# Use logical_not to invert a condition from a numeric tensor +vals = torch.tensor([0.0, 1.5, -2.0, 0.0]) +cond = vals > 0 +cond_inv = torch.logical_not(cond) + +print('\nvals:', vals) +print('cond (vals > 0):', cond) +print('cond inverted with logical_not:', cond_inv) + +# Numeric input — zeros are False, non-zeros are True +print(torch.logical_not(torch.tensor([0., 1.5, -10.], dtype=torch.double))) + +# Using an integer out tensor (int16) to store 0/1 results +out_buf = torch.empty(3, dtype=torch.int16) +print(torch.logical_not(torch.tensor([0., 1.5, -10.], dtype=torch.double), out=out_buf)) +``` + +The above code produces the following output: + +```shell +mask: tensor([ True, False, True, False]) +logical_not(mask): tensor([False, True, False, True]) + +vals: tensor([ 0.0000, 1.5000, -2.0000, 0.0000]) +cond (vals > 0): tensor([False, True, False, False]) +cond inverted with logical_not: tensor([ True, False, True, True]) + +tensor([ True, False, False]) +tensor([1, 0, 0], dtype=torch.int16) +``` + +> **Note:** For integer or floating-point tensors, first create a boolean condition (for example, `x > 0`) or cast with `.to(torch.bool)` before calling `.logical_not()`.``` + +```` From 3dc16f123a4b019cfe3fae2c408cd0a9c046d782 Mon Sep 17 00:00:00 2001 From: Bhuswarna Kashyap Date: Thu, 23 Oct 2025 15:47:28 +0530 Subject: [PATCH 2/5] remove markdown --- .../tensor-operations/terms/logical-not/logical-not.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md index 06a8226fb4e..cd482a4e30b 100644 --- a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md +++ b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md @@ -1,4 +1,3 @@ -````markdown --- Title: '.logical_not()' Description: 'Performs element-wise logical NOT on boolean tensors, returning a tensor where each element is the boolean negation of the input.' @@ -83,6 +82,4 @@ tensor([ True, False, False]) tensor([1, 0, 0], dtype=torch.int16) ``` -> **Note:** For integer or floating-point tensors, first create a boolean condition (for example, `x > 0`) or cast with `.to(torch.bool)` before calling `.logical_not()`.``` - -```` +> **Note:** For integer or floating-point tensors, first create a boolean condition (for example, `x > 0`) or cast with `.to(torch.bool)` before calling `.logical_not()`. From 31209f17694bb86c198a6dfacc173e9d6ae27e8f Mon Sep 17 00:00:00 2001 From: Bhuswarna Kashyap Date: Thu, 23 Oct 2025 15:49:58 +0530 Subject: [PATCH 3/5] remove note --- .../tensor-operations/terms/logical-not/logical-not.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md index cd482a4e30b..677724ca8c8 100644 --- a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md +++ b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md @@ -80,6 +80,4 @@ cond inverted with logical_not: tensor([ True, False, True, True]) tensor([ True, False, False]) tensor([1, 0, 0], dtype=torch.int16) -``` - -> **Note:** For integer or floating-point tensors, first create a boolean condition (for example, `x > 0`) or cast with `.to(torch.bool)` before calling `.logical_not()`. +``` \ No newline at end of file From f19b768ca731188305be993b14a0cdcbf8ed7993 Mon Sep 17 00:00:00 2001 From: Bhuswarna Kashyap Date: Thu, 23 Oct 2025 16:14:35 +0530 Subject: [PATCH 4/5] modify python example --- .../terms/logical-not/logical-not.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md index 677724ca8c8..2ec6dfc5c56 100644 --- a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md +++ b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md @@ -61,11 +61,11 @@ print('cond (vals > 0):', cond) print('cond inverted with logical_not:', cond_inv) # Numeric input — zeros are False, non-zeros are True -print(torch.logical_not(torch.tensor([0., 1.5, -10.], dtype=torch.double))) +print(torch.logical_not(torch.tensor([0., 2.45, -20., 3.8], dtype=torch.double))) # Using an integer out tensor (int16) to store 0/1 results -out_buf = torch.empty(3, dtype=torch.int16) -print(torch.logical_not(torch.tensor([0., 1.5, -10.], dtype=torch.double), out=out_buf)) +out_buf = torch.empty(4, dtype=torch.int16) +print(torch.logical_not(torch.tensor([0., 2.45, -20., 3.8], dtype=torch.double), out=out_buf)) ``` The above code produces the following output: @@ -77,7 +77,6 @@ logical_not(mask): tensor([False, True, False, True]) vals: tensor([ 0.0000, 1.5000, -2.0000, 0.0000]) cond (vals > 0): tensor([False, True, False, False]) cond inverted with logical_not: tensor([ True, False, True, True]) - -tensor([ True, False, False]) -tensor([1, 0, 0], dtype=torch.int16) +tensor([ True, False, False, False]) +tensor([1, 0, 0, 0], dtype=torch.int16) ``` \ No newline at end of file From 2c640a33cf289bb2721a8c6890b50b1296909779 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 27 Oct 2025 22:37:31 +0530 Subject: [PATCH 5/5] Refine .logical_not() documentation Updated the description and parameters of the .logical_not() function for clarity and consistency. Adjusted examples and terminology for improved readability. --- .../terms/logical-not/logical-not.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md index 2ec6dfc5c56..c4f19f1b38a 100644 --- a/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md +++ b/content/pytorch/concepts/tensor-operations/terms/logical-not/logical-not.md @@ -3,22 +3,22 @@ Title: '.logical_not()' Description: 'Performs element-wise logical NOT on boolean tensors, returning a tensor where each element is the boolean negation of the input.' Subjects: - 'AI' - - 'Data Science' - 'Computer Science' + - 'Data Science' - 'Machine Learning' Tags: - 'Booleans' + - 'Functions' - 'PyTorch' - 'Tensor' - - 'Functions' CatalogContent: - 'intro-to-py-torch-and-neural-networks' - 'paths/data-science' --- -In PyTorch, the **`.logical_not()`** function computes the element-wise logical negation of a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). It returns a tensor where each element is `True` when the corresponding input element is `False` and vice versa. If the input tensor is not a boolean tensor, then zeros are treated as `False` and non-zeros are treated as `True`. +In PyTorch, the **`.logical_not()`** function performs an element-wise logical negation on a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). It returns a tensor where each element is `True` if the corresponding input is `False`, and `False` otherwise. For non-boolean tensors, zeros are treated as `False` and non-zeros as `True`. -This operation is often used in masking, boolean indexing, and constructing complement conditions. +This function is commonly used in masking, boolean indexing, and creating complement conditions. ## Syntax @@ -28,16 +28,16 @@ torch.logical_not(input, *, out=None) **Parameters:** -- `input`: A tensor of any numeric or boolean dtype. The function returns a boolean tensor. - - `out` (Optional): A tensor to store the result. Must have the same shape as the expected output. The typical dtype for the output is boolean (`torch.bool`), but integer dtypes that can represent `0`/`1` (for example, `torch.int16`) are accepted; results will be stored as `1` (True) and `0` (False) in that tensor. +- `input`: The input tensor containing boolean or numeric values. +- `out` (Optional): A tensor to store the result. It must have the same shape as the output. The dtype is typically `torch.bool`, but integer types that can represent `0` and `1` (like `torch.int16`) are also supported. The results are stored as `1` for `True` and `0` for `False`. **Return value:** -Returns a new boolean tensor containing the element-wise logical negation of `input`. +Returns a new tensor containing the element-wise logical negation of the input tensor. ## Example -The following example shows typical usage of `.logical_not()` for masking and boolean inversion: +The following example demonstrates the use of `.logical_not()` for masking and boolean inversion: ```py import torch @@ -79,4 +79,4 @@ cond (vals > 0): tensor([False, True, False, False]) cond inverted with logical_not: tensor([ True, False, True, True]) tensor([ True, False, False, False]) tensor([1, 0, 0, 0], dtype=torch.int16) -``` \ No newline at end of file +```