You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 27, 2026. It is now read-only.
Copy file name to clipboardExpand all lines: content/pytorch/concepts/tensor-operations/terms/max/max.md
+36-24Lines changed: 36 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,21 +14,24 @@ CatalogContent:
14
14
- 'paths/data-science'
15
15
---
16
16
17
-
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.
17
+
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.
18
18
19
19
## Syntax
20
20
21
21
```pseudo
22
-
torch.max(input) → Tensor
22
+
torch.max(input, dim=None, keepdim=False) → Tensor or (Tensor, LongTensor)
23
23
```
24
24
25
25
**Parameters:**
26
26
27
27
-`input` (Tensor): The input tensor.
28
+
-`dim` (int, optional): The dimension along which to find the maximum values. If not specified, returns the maximum value of the entire tensor.
29
+
-`keepdim` (bool, optional): Whether the output tensor retains the reduced dimension. Defaults to `False`.
28
30
29
31
**Return value:**
30
32
31
-
Returns a tensor containing the maximum value from the `input`.
33
+
- When `dim` is not specified: Returns a tensor containing the single maximum value from the entire tensor.
34
+
- 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.
32
35
33
36
## Example
34
37
@@ -37,47 +40,56 @@ The following example demonstrates how to use the `.max()` method to find the ma
0 commit comments