From fc493a51467220b3e9a6f46d784dd35a8cb3957d Mon Sep 17 00:00:00 2001 From: Renata Silva Date: Sun, 15 Feb 2026 15:54:38 +0000 Subject: [PATCH 1/3] Add all term entry for PyTorch --- .../tensor-operations/terms/all/all.md | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/all/all.md diff --git a/content/pytorch/concepts/tensor-operations/terms/all/all.md b/content/pytorch/concepts/tensor-operations/terms/all/all.md new file mode 100644 index 00000000000..fe9c04bb344 --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/all/all.md @@ -0,0 +1,129 @@ +--- +Title: '.all()' +Description: 'Checks if all elements in the input tensor evaluate to True.' +Subjects: + - 'AI' + - 'Data Science' + - 'Machine Learning' +Tags: + - 'AI' + - 'Data Structures' + - 'Deep Learning' + - 'Functions' + - 'Machine Learning' + - 'PyTorch' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- + +The **`.all()`** function returns `True` if all elements in the input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) evaluate to `True`, otherwise it returns `False`. When a dimension `dim` is specified, the function checks all elements in that specific dimension. + + +## Syntax +```pseudo +torch.all(input: Tensor, *, out=None) +``` +Or, alternatively: + +```pseudo +torch.all(input, dim, keepdim=False, *, out=None) +``` +- `input`: The input tensor. +- `dim` (optional): An `int` or `tuple` of `int` values defining the dimension or dimensions to reduce. If `None`, all dimensions are reduced. +- `keepdim` (optional): A `bool` value that defines if the output tensor retains the reduced dimension. The default value is `False`. +- `out` (optional): The output tensor. + +## Example 1 + +Checking if all elements in a tensor are `True`: + +```py +import torch + +# Define a tensor where all elements are True +tensor = torch.tensor([True, True, True]) +print(tensor.all()) + +# Define a tensor that contains a False element +tensor = torch.tensor([True, False, True]) +print(tensor.all()) +``` + +This produces the following output: + +```shell +tensor(True) +tensor(False) +``` + +## Example 2 + +Using `.all()` with numeric values (non-zero values are treated as `True` and zero is treated as `False`): + +```py +import torch + +# Define a tensor with numeric values +tensor = torch.tensor([1, 2, 3]) +print(tensor.all()) + +# Define a tensor which contains a numeric value of zero +tensor = torch.tensor([0, 1, 2]) +print(tensor.all()) +``` + +This produces the following output: + +```shell +tensor(True) +tensor(False) +``` + +## Example 3 + +Using `.all()` to check specific dimensions in a 2D tensor: + +```py +import torch + +# Define a 2D boolean tensor +matrix = torch.tensor([[True, True], + [True, False], + [True, True]]) + +# Check dimension 1 (horizontal) +print(matrix.all(dim=1)) + +# Check dimension 0 (vertical) +print(matrix.all(dim=0)) +``` + +This produces the following output: + +```shell +tensor([ True, False, True]) +tensor([ True, False]) +``` + +## Codebyte Example + +```codebyte/python +import torch + +# Boolean tensor +x = torch.tensor([True, True, False, True]) +result = x.all() +print(f"Are all elements True? {result}") + +# Numeric comparison +y = torch.tensor([10, 15, 20, 25]) +positive_tensor = (y > 0).all() +print(f"Are all elementes positive? {positive_tensor}") + +# Check dimension 1 +matrix = torch.tensor([[1, 2], [3, 0], [4, 5]]) +check_row = (matrix > 0).all(dim=1) +print(f"Are all elements positive per row? {check_row}") +``` + From b7660fc81604eeeadf713119f80e277ee71daa94 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 16 Feb 2026 17:02:45 +0530 Subject: [PATCH 2/3] Refine .all() function documentation Updated the description and examples for the .all() function to clarify behavior and syntax. --- .../tensor-operations/terms/all/all.md | 89 ++++++------------- 1 file changed, 27 insertions(+), 62 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/all/all.md b/content/pytorch/concepts/tensor-operations/terms/all/all.md index fe9c04bb344..c7d8a30dea9 100644 --- a/content/pytorch/concepts/tensor-operations/terms/all/all.md +++ b/content/pytorch/concepts/tensor-operations/terms/all/all.md @@ -1,6 +1,6 @@ --- Title: '.all()' -Description: 'Checks if all elements in the input tensor evaluate to True.' +Description: 'Returns True if all elements in a tensor evaluate to True.' Subjects: - 'AI' - 'Data Science' @@ -17,113 +17,78 @@ CatalogContent: - 'paths/data-science' --- -The **`.all()`** function returns `True` if all elements in the input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) evaluate to `True`, otherwise it returns `False`. When a dimension `dim` is specified, the function checks all elements in that specific dimension. +The **`.all()`** function returns `True` if all elements in the input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) evaluate to `True`, otherwise it returns `False`. If a dimension is specified, the function performs a logical AND reduction along that dimension. +Non-zero numeric values evaluate as `True`, and zero evaluates as `False`. ## Syntax + ```pseudo -torch.all(input: Tensor, *, out=None) +torch.all(input, dim, keepdim=False, *, out=None) ``` -Or, alternatively: + +Or alternatively in tensor method form: ```pseudo -torch.all(input, dim, keepdim=False, *, out=None) +tensor.all(dim=None, keepdim=False) ``` + +**Parameters:** + - `input`: The input tensor. - `dim` (optional): An `int` or `tuple` of `int` values defining the dimension or dimensions to reduce. If `None`, all dimensions are reduced. - `keepdim` (optional): A `bool` value that defines if the output tensor retains the reduced dimension. The default value is `False`. - `out` (optional): The output tensor. -## Example 1 +**Return value:** + +- If `dim` is not specified, returns a zero-dimensional boolean tensor containing `True` or `False`. +- If `dim` is specified, returns a tensor reduced along the given dimension(s), containing boolean values. + +## Example 1: Reducing an Entire Tensor -Checking if all elements in a tensor are `True`: +In this example, `torch.all()` checks whether all elements in a boolean tensor are `True`: ```py import torch -# Define a tensor where all elements are True -tensor = torch.tensor([True, True, True]) -print(tensor.all()) - -# Define a tensor that contains a False element -tensor = torch.tensor([True, False, True]) -print(tensor.all()) +tensor = torch.tensor([True, True, False]) +print(torch.all(tensor)) ``` This produces the following output: ```shell -tensor(True) tensor(False) ``` -## Example 2 +## Example 2: Using Numeric Values -Using `.all()` with numeric values (non-zero values are treated as `True` and zero is treated as `False`): +In this example, non-zero values evaluate as `True` and zero evaluates as `False`: ```py import torch -# Define a tensor with numeric values -tensor = torch.tensor([1, 2, 3]) -print(tensor.all()) - -# Define a tensor which contains a numeric value of zero -tensor = torch.tensor([0, 1, 2]) -print(tensor.all()) +tensor = torch.tensor([1, 2, 0]) +print(torch.all(tensor)) ``` This produces the following output: ```shell -tensor(True) tensor(False) ``` -## Example 3 +## Codebyte Example: Reducing Along a Dimension -Using `.all()` to check specific dimensions in a 2D tensor: +In this example, `torch.all()` checks values along specific dimensions of a 2D tensor: ```py import torch -# Define a 2D boolean tensor matrix = torch.tensor([[True, True], [True, False], [True, True]]) -# Check dimension 1 (horizontal) -print(matrix.all(dim=1)) - -# Check dimension 0 (vertical) -print(matrix.all(dim=0)) -``` - -This produces the following output: - -```shell -tensor([ True, False, True]) -tensor([ True, False]) +print(torch.all(matrix, dim=1)) ``` - -## Codebyte Example - -```codebyte/python -import torch - -# Boolean tensor -x = torch.tensor([True, True, False, True]) -result = x.all() -print(f"Are all elements True? {result}") - -# Numeric comparison -y = torch.tensor([10, 15, 20, 25]) -positive_tensor = (y > 0).all() -print(f"Are all elementes positive? {positive_tensor}") - -# Check dimension 1 -matrix = torch.tensor([[1, 2], [3, 0], [4, 5]]) -check_row = (matrix > 0).all(dim=1) -print(f"Are all elements positive per row? {check_row}") -``` - From f2d72679061d8d3d3c8481f15b2da6e80a253030 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Thu, 19 Feb 2026 14:31:25 +0530 Subject: [PATCH 3/3] Update all.md --- content/pytorch/concepts/tensor-operations/terms/all/all.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/all/all.md b/content/pytorch/concepts/tensor-operations/terms/all/all.md index c7d8a30dea9..be6491c2f4b 100644 --- a/content/pytorch/concepts/tensor-operations/terms/all/all.md +++ b/content/pytorch/concepts/tensor-operations/terms/all/all.md @@ -27,7 +27,7 @@ Non-zero numeric values evaluate as `True`, and zero evaluates as `False`. torch.all(input, dim, keepdim=False, *, out=None) ``` -Or alternatively in tensor method form: +Or alternatively, in tensor method form: ```pseudo tensor.all(dim=None, keepdim=False)