Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.max

Returns the maximum value of a tensor, either globally or along a chosen dimension. Dimension-wise reduction also returns the index of each maximum.

How it works

torch.max reduces a tensor either across every element or along one selected dimension. With no dimension, it returns one maximum tensor; with a dimension, it returns named values and indices, optionally preserving the reduced dimension.

Syntax

torch.max(input, *, out=None) → Tensor
torch.max(input, dim, keepdim=False, *, out=None) → (Tensor, LongTensor)

Parameters

NameTypeDescription
inputTensorInput tensor to reduce.
dimint, optionalDimension to reduce. If omitted, all dimensions are reduced; explicit None is unsupported.
keepdimbool, optionalWhether to retain the reduced dimension. Defaults to False.
outTensor or tuple, optionalOptional output tensor or tuple of output tensors, depending on the overload.

Returns

Without dim, a tensor containing the maximum of all input elements. With dim, a namedtuple (values, indices), where values contains dimension-wise maxima and indices contains their locations.

Key points

  • Calling torch.max(input) reduces every element and returns one maximum-value tensor.
  • A dimension-wise call returns named values and indices outputs for the reduced slices.
  • keepdim=True preserves the input rank by retaining the reduced axis at size one.
  • The default keepdim=False removes the reduced dimension from the outputs.
  • The optional dim argument controls the reduction axis; omitting it reduces across the entire tensor.
  • The out argument can receive the two dimension-wise result tensors.

Common mistakes

  • When a reduction contains ties, the returned index is the first occurrence of the maximum.
  • Do not pass None explicitly for dim; omit the argument when reducing all dimensions.

Example: Row-wise maximum and index

import torch

x = torch.tensor([[1.0, 4.0, 2.0], [5.0, 3.0, 5.0]])
values, indices = torch.max(x, dim=1)
print(values)   # tensor([4., 5.])
print(indices)  # tensor([1, 0])

Reduces each row and returns both its maximum value and the index of that value.

When to use it

Classification predictions

Choose the winning class for each item from model scores.

Dimension reduction

Collapse spatial, temporal, or batch axes while retaining optional singleton dimensions.

Value-and-index selection

Retrieve both best scores and their positions for downstream selection.

Interview questions

QWhat does torch.max return with and without dim?

With only input, torch.max returns the maximum over all elements as a scalar tensor. Supplying dim changes the result to a named tuple containing values and indices for each reduced slice.

Follow-up: How do you retain the reduced axis? Pass keepdim=True, which leaves that dimension at size one.

QHow does keepdim affect dimensionality?

keepdim=False is the default, so the reduced dimension is removed from both outputs. With keepdim=True, the outputs retain the input rank and use size one along dim.

Follow-up: What happens if dim is omitted? All dimensions are reduced, producing a single maximum value.

QWhat do the two fields of the dimension-wise result represent?

For a dimension-wise reduction, values contains the maxima and indices contains their locations along dim. If equal maxima occur, indices selects the first maximal value.

Follow-up: Which related operation returns only locations? torch.argmax returns maximum-value indices.

  • torch.argmax
  • torch.squeeze
  • torch.Tensor.max

Source: torch.max — PyTorch 2.14 documentation. Checked 2026-10-16.