Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added docs for the .max() method closes #8251 #8263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
abdultalha0862 wants to merge 3 commits into Codecademy:main
base: main
Choose a base branch
Loading
from abdultalha0862:add-max-function
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions content/pytorch/concepts/tensor-operations/terms/max/max.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
Title: '.max()'
Description: 'Returns the maximum value of a tensor, either across the entire tensor or along a specified dimension.'
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 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, dim=None, keepdim=False, *, out=None) → 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`.
- `out` (tuple of Tensors, optional): Optional tuple of two tensors to store the output values and indices.

**Return value:**

- 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

The following example demonstrates how to use the `.max()` method to find the maximum value in a tensor:

```py
import torch

# Create a 2D tensor
tensor = torch.tensor([[1.5, -2.3, 0.0],
[4.8, -1.2, 3.6]])

# Find the maximum value of the entire tensor
max_value = 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 (entire tensor):")
print(max_value)

print("\nMaximum Values (along columns, dim=0):")
print("Values:", max_cols.values)
print("Indices:", max_cols.indices)

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, 3.6000]])

Maximum Value (entire tensor):
tensor(4.8000)

Maximum Values (along columns, dim=0):
Values: tensor([4.8000, -1.2000, 3.6000])
Indices: tensor([1, 1, 1])

Maximum Values (along rows, dim=1):
Values: tensor([1.5000, 4.8000])
Indices: tensor([0, 0])
```

In this example:

- **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.

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