From e3823f990b1653e8dd2f31e65dd41a056c8bbfdb Mon Sep 17 00:00:00 2001 From: abdultalha0862 <20211a0405@bvrit.ac.in> Date: 2026年2月24日 11:06:59 +0000 Subject: [PATCH 1/3] Added docs for the .max() method --- .../tensor-operations/terms/max/max.md | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/max/max.md diff --git a/content/pytorch/concepts/tensor-operations/terms/max/max.md b/content/pytorch/concepts/tensor-operations/terms/max/max.md new file mode 100644 index 00000000000..6759b080e7d --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/max/max.md @@ -0,0 +1,83 @@ +--- +Title: '.max()' +Description: 'Returns the maximum value of a tensor.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Deep Learning' + - 'Methods' + - 'PyTorch' + - 'Tensor' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/data-science' +--- + +The **`.max()`** method in PyTorch returns the maximum value from a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). It can find the maximum value across the entire tensor. This method is commonly used in data analysis, finding peak values, and various neural network operations. + +## Syntax + +```pseudo +torch.max(input) → Tensor +``` + +**Parameters:** + +- `input` (Tensor): The input tensor. + +**Return value:** + +Returns a tensor containing the maximum value from the `input`. + +## Example + +The following example demonstrates how to use the `.max()` method to find the maximum value in a tensor: + +```py +import torch + +# Create a tensor with various values +tensor = torch.tensor([1.5, -2.3, 0.0, 4.8, -1.2]) + +# Find the maximum value using the method form +max_value = tensor.max() + +# Alternative: use the functional form +max_functional = torch.max(tensor) + +print("Original Tensor:") +print(tensor) + +print("\nMaximum Value (using .max()):") +print(max_value) + +print("\nMaximum Value (using torch.max()):") +print(max_functional) + +print("\nMaximum as Python number (using .item()):") +print(max_value.item()) +``` + +This example results in the following output: + +```shell +Original Tensor: +tensor([ 1.5000, -2.3000, 0.0000, 4.8000, -1.2000]) + +Maximum Value (using .max()): +tensor(4.8000) + +Maximum Value (using torch.max()): +tensor(4.8000) + +Maximum as Python number (using .item()): +4.800000190734863 +``` + +In this example: + +- The tensor contains five values: `1.5`, `-2.3`, `0.0`, `4.8`, and `-1.2` +- The `.max()` method identifies `4.8` as the maximum value in the tensor +- Both `.max()` and `torch.max()` produce identical results: `tensor(4.8000)` +- The `.item()` method converts the tensor result to a Python float: `4.800000190734863` From 6485e5fd8219dc170b4c20307040ef90448d2fe6 Mon Sep 17 00:00:00 2001 From: abdultalha0862 <20211a0405@bvrit.ac.in> Date: 2026年2月24日 11:20:40 +0000 Subject: [PATCH 2/3] update docs for .max() method --- .../tensor-operations/terms/max/max.md | 60 +++++++++++-------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/max/max.md b/content/pytorch/concepts/tensor-operations/terms/max/max.md index 6759b080e7d..7d5849efb79 100644 --- a/content/pytorch/concepts/tensor-operations/terms/max/max.md +++ b/content/pytorch/concepts/tensor-operations/terms/max/max.md @@ -14,21 +14,24 @@ CatalogContent: - 'paths/data-science' --- -The **`.max()`** method in PyTorch returns the maximum value from a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). It can find the maximum value across the entire tensor. This method is commonly used in data analysis, finding peak values, and various neural network operations. +The **`.max()`** method in PyTorch returns the maximum value from a [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). It can find the maximum value across the entire tensor or along a specified dimension. This method is commonly used in data analysis, finding peak values, and various neural network operations. ## Syntax ```pseudo -torch.max(input) → Tensor +torch.max(input, dim=None, keepdim=False) → Tensor or (Tensor, LongTensor) ``` **Parameters:** - `input` (Tensor): The input tensor. +- `dim` (int, optional): The dimension along which to find the maximum values. If not specified, returns the maximum value of the entire tensor. +- `keepdim` (bool, optional): Whether the output tensor retains the reduced dimension. Defaults to `False`. **Return value:** -Returns a tensor containing the maximum value from the `input`. +- When `dim` is not specified: Returns a tensor containing the single maximum value from the entire tensor. +- When `dim` is specified: Returns a named tuple `(values, indices)` where `values` contains the maximum values along the specified dimension, and `indices` contains the indices of those maximum values. ## Example @@ -37,47 +40,56 @@ The following example demonstrates how to use the `.max()` method to find the ma ```py import torch -# Create a tensor with various values -tensor = torch.tensor([1.5, -2.3, 0.0, 4.8, -1.2]) +# Create a 2D tensor +tensor = torch.tensor([[1.5, -2.3, 0.0], + [4.8, -1.2, 3.6]]) -# Find the maximum value using the method form -max_value = tensor.max() +# Find the maximum value of the entire tensor +max_value = torch.max(tensor) -# Alternative: use the functional form -max_functional = torch.max(tensor) +# Find maximum values along each column (dim=0) +max_cols = torch.max(tensor, dim=0) + +# Find maximum values along each row (dim=1) +max_rows = torch.max(tensor, dim=1) print("Original Tensor:") print(tensor) -print("\nMaximum Value (using .max()):") +print("\nMaximum Value (entire tensor):") print(max_value) -print("\nMaximum Value (using torch.max()):") -print(max_functional) +print("\nMaximum Values (along columns, dim=0):") +print("Values:", max_cols.values) +print("Indices:", max_cols.indices) -print("\nMaximum as Python number (using .item()):") -print(max_value.item()) +print("\nMaximum Values (along rows, dim=1):") +print("Values:", max_rows.values) +print("Indices:", max_rows.indices) ``` This example results in the following output: ```shell Original Tensor: -tensor([ 1.5000, -2.3000, 0.0000, 4.8000, -1.2000]) +tensor([[ 1.5000, -2.3000, 0.0000], + [ 4.8000, -1.2000, 3.6000]]) -Maximum Value (using .max()): +Maximum Value (entire tensor): tensor(4.8000) -Maximum Value (using torch.max()): -tensor(4.8000) +Maximum Values (along columns, dim=0): +Values: tensor([4.8000, -1.2000, 3.6000]) +Indices: tensor([1, 1, 1]) -Maximum as Python number (using .item()): -4.800000190734863 +Maximum Values (along rows, dim=1): +Values: tensor([1.5000, 4.8000]) +Indices: tensor([0, 0]) ``` In this example: -- The tensor contains five values: `1.5`, `-2.3`, `0.0`, `4.8`, and `-1.2` -- The `.max()` method identifies `4.8` as the maximum value in the tensor -- Both `.max()` and `torch.max()` produce identical results: `tensor(4.8000)` -- The `.item()` method converts the tensor result to a Python float: `4.800000190734863` +- **Entire tensor**: The maximum value across all elements is `4.8000`. +- **Along columns (`dim=0`)**: The maximum values in each column are `4.8000`, `-1.2000`, and `3.6000`, all found in row `1` (index `1`). +- **Along rows (`dim=1`)**: The maximum values in each row are `1.5000` (at index `0`) and `4.8000` (at index `0`). +- When `dim` is specified, the method returns both the maximum values and their indices as a named tuple. From bc9170580dcfff9827ae7e3d50dfd799ca8613d5 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: 2026年2月25日 11:15:24 +0530 Subject: [PATCH 3/3] Enhance .max() documentation with additional details Updated the description to clarify that .max() can return maximum values across a specified dimension. Added the 'out' parameter to the syntax and clarified return values. --- .../pytorch/concepts/tensor-operations/terms/max/max.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/max/max.md b/content/pytorch/concepts/tensor-operations/terms/max/max.md index 7d5849efb79..0bc6d8311a1 100644 --- a/content/pytorch/concepts/tensor-operations/terms/max/max.md +++ b/content/pytorch/concepts/tensor-operations/terms/max/max.md @@ -1,6 +1,6 @@ --- Title: '.max()' -Description: 'Returns the maximum value of a tensor.' +Description: 'Returns the maximum value of a tensor, either across the entire tensor or along a specified dimension.' Subjects: - 'Computer Science' - 'Data Science' @@ -19,7 +19,7 @@ The **`.max()`** method in PyTorch returns the maximum value from a [tensor](htt ## Syntax ```pseudo -torch.max(input, dim=None, keepdim=False) → Tensor or (Tensor, LongTensor) +torch.max(input, dim=None, keepdim=False, *, out=None) → Tensor or (Tensor, LongTensor) ``` **Parameters:** @@ -27,11 +27,12 @@ torch.max(input, dim=None, keepdim=False) → Tensor or (Tensor, LongTensor) - `input` (Tensor): The input tensor. - `dim` (int, optional): The dimension along which to find the maximum values. If not specified, returns the maximum value of the entire tensor. - `keepdim` (bool, optional): Whether the output tensor retains the reduced dimension. Defaults to `False`. +- `out` (tuple of Tensors, optional): Optional tuple of two tensors to store the output values and indices. **Return value:** -- When `dim` is not specified: Returns a tensor containing the single maximum value from the entire tensor. -- When `dim` is specified: Returns a named tuple `(values, indices)` where `values` contains the maximum values along the specified dimension, and `indices` contains the indices of those maximum values. +- When `dim` is not specified, it returns a tensor containing the single maximum value from the entire tensor. +- When `dim` is specified, it returns a named tuple `(values, indices)` where `values` contains the maximum values along the specified dimension, and `indices` contains the indices of those maximum values. ## Example

AltStyle によって変換されたページ (->オリジナル) /