From 5980b2bb398a17989700d3dc985f30dd5dd825e0 Mon Sep 17 00:00:00 2001 From: Greg Walker Date: Thu, 25 Sep 2025 15:21:39 -0400 Subject: [PATCH 1/2] Create logical_and.md --- .../tensors/terms/logical_and/logical_and.md | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 content/pytorch/concepts/tensors/terms/logical_and/logical_and.md diff --git a/content/pytorch/concepts/tensors/terms/logical_and/logical_and.md b/content/pytorch/concepts/tensors/terms/logical_and/logical_and.md new file mode 100644 index 00000000000..aa5a336cc67 --- /dev/null +++ b/content/pytorch/concepts/tensors/terms/logical_and/logical_and.md @@ -0,0 +1,113 @@ +--- +Title: '.logical_and()' +Description: 'Performs element-wise logical AND operation between two tensors.' +Subjects: + - 'Data Science' + - 'Machine Learning' +Tags: + - 'Python' + - 'PyTorch' + - 'Tensors' + - 'Logical Operations' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'py-torch-for-classification' +--- + +The **`torch.logical_and()`** function in PyTorch performs an element-wise logical AND operation between two [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors). It returns a new tensor with boolean values (`True` or `False`) depending on whether the corresponding elements in both input tensors evaluate to `True`. + +This operation is often used in tensor-based computations where conditional checks need to be applied element-wise, such as in [masking](https://pytorch.org/docs/stable/notes/broadcasting.html) or filtering data. + +## Syntax + +```py +torch.logical_and(input, other, *, out=None) +``` + +**Parameters:** + +- `input` (Tensor): The first tensor for the logical AND operation. +- `other` (Tensor): The second tensor, must be broadcastable to the shape of `input`. +- `out` (Tensor, optional): The output tensor to store the result. + +**Return value:** + +A tensor of type `torch.bool` containing the result of the element-wise logical AND operation. + +## Example 1: Basic Usage + +This example shows how `torch.logical_and()` compares two tensors element-wise: + +```py +import torch + +a = torch.tensor([True, False, True]) +b = torch.tensor([True, True, False]) + +result = torch.logical_and(a, b) +print(result) +``` + +Output: + +```shell +tensor([True, False, False]) +``` + +## Example 2: Using with Integer Tensors + +Nonzero values in integer tensors are treated as `True`, while `0` is treated as `False`: + +```py +import torch + +x = torch.tensor([1, 0, 3]) +y = torch.tensor([2, 0, 0]) + +result = torch.logical_and(x, y) +print(result) +``` + +Output: + +```shell +tensor([True, False, False]) +``` + +## Example 3: Broadcasting in `torch.logical_and()` + +The function supports [broadcasting](https://pytorch.org/docs/stable/notes/broadcasting.html): + +```py +import torch + +m = torch.tensor([[1, 0], [0, 1]]) +n = torch.tensor([1, 0]) + +result = torch.logical_and(m, n) +print(result) +``` + +Output: + +```shell +tensor([[True, False], + [False, False]]) +``` + +## Codebyte Example + +```codebyte/python +import torch + +a = torch.tensor([True, False, True, False]) +b = torch.tensor([True, True, False, False]) + +print(torch.logical_and(a, b)) +``` + +Expected Output: + +```shell +tensor([True, False, False, False]) +``` From 16da9deea49cef6bce85c19a36f927f36d91e60e Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 9 Oct 2025 12:58:25 +0530 Subject: [PATCH 2/2] minor content fixes --- .../tensors/terms/logical_and/logical_and.md | 38 ++++++------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/content/pytorch/concepts/tensors/terms/logical_and/logical_and.md b/content/pytorch/concepts/tensors/terms/logical_and/logical_and.md index aa5a336cc67..04a59cf61ae 100644 --- a/content/pytorch/concepts/tensors/terms/logical_and/logical_and.md +++ b/content/pytorch/concepts/tensors/terms/logical_and/logical_and.md @@ -1,14 +1,15 @@ --- Title: '.logical_and()' -Description: 'Performs element-wise logical AND operation between two tensors.' +Description: 'Performs an element-wise logical AND operation on two input tensors, returning a tensor of Boolean values.' Subjects: - 'Data Science' - 'Machine Learning' Tags: + - 'Logical' + - 'Operators' - 'Python' - 'PyTorch' - 'Tensors' - - 'Logical Operations' CatalogContent: - 'intro-to-py-torch-and-neural-networks' - 'py-torch-for-classification' @@ -16,11 +17,11 @@ CatalogContent: The **`torch.logical_and()`** function in PyTorch performs an element-wise logical AND operation between two [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors). It returns a new tensor with boolean values (`True` or `False`) depending on whether the corresponding elements in both input tensors evaluate to `True`. -This operation is often used in tensor-based computations where conditional checks need to be applied element-wise, such as in [masking](https://pytorch.org/docs/stable/notes/broadcasting.html) or filtering data. +This operation is often used in tensor-based computations where conditional checks need to be applied element-wise, such as in masking or filtering data. ## Syntax -```py +```pseudo torch.logical_and(input, other, *, out=None) ``` @@ -36,7 +37,7 @@ A tensor of type `torch.bool` containing the result of the element-wise logical ## Example 1: Basic Usage -This example shows how `torch.logical_and()` compares two tensors element-wise: +In this example, two Boolean tensors are compared element-wise using `torch.logical_and()`: ```py import torch @@ -48,7 +49,7 @@ result = torch.logical_and(a, b) print(result) ``` -Output: +The output of this code is as follows: ```shell tensor([True, False, False]) @@ -56,7 +57,7 @@ tensor([True, False, False]) ## Example 2: Using with Integer Tensors -Nonzero values in integer tensors are treated as `True`, while `0` is treated as `False`: +In this example, integer tensors are treated as Boolean values, with nonzero as `True` and 0 as `False`: ```py import torch @@ -68,7 +69,7 @@ result = torch.logical_and(x, y) print(result) ``` -Output: +The output of this code is: ```shell tensor([True, False, False]) @@ -76,7 +77,7 @@ tensor([True, False, False]) ## Example 3: Broadcasting in `torch.logical_and()` -The function supports [broadcasting](https://pytorch.org/docs/stable/notes/broadcasting.html): +In this example, [broadcasting](https://pytorch.org/docs/stable/notes/broadcasting.html) allows a smaller tensor to be compared across the rows of a larger tensor. ```py import torch @@ -88,26 +89,9 @@ result = torch.logical_and(m, n) print(result) ``` -Output: +The output of this code will be: ```shell tensor([[True, False], [False, False]]) ``` - -## Codebyte Example - -```codebyte/python -import torch - -a = torch.tensor([True, False, True, False]) -b = torch.tensor([True, True, False, False]) - -print(torch.logical_and(a, b)) -``` - -Expected Output: - -```shell -tensor([True, False, False, False]) -```