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

PyTorch · Function

torch.min

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

How it works

torch.min computes the smallest value in a tensor, either across every element or along one dimension. With a dimension specified, it returns a named tuple containing the reduced values and their indices; keepdim controls whether the reduced dimension remains with size one.

Syntax

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

Parameters

NameTypeDescription
inputTensorTensor to reduce.
dimint, optionalDimension to reduce; omit it to reduce all dimensions.
keepdimbool, optionalWhether to retain the reduced dimension; defaults to False.
outtuple, optionalOptional tuple of output tensors for the dimensional form, or output tensor for the all-elements form.

Returns

Without dim, a Tensor containing the global minimum. With dim, a namedtuple (values, indices), where values contains minima and indices contains their locations.

Key points

  • Calling torch.min with only the input reduces every dimension and returns a scalar minimum.
  • Supplying dim returns a named tuple with the minimum values and their positions.
  • keepdim=True preserves the input rank by making the reduced dimension size one.
  • With the default keepdim=False, the reduced dimension is removed from both outputs.

Common mistakes

  • When a reduced slice contains equal minima, the returned index is the first occurrence.
  • Do not pass dim=None explicitly; omit dim when reducing all dimensions.

Example: Reduce rows and keep the dimension

import torch

x = torch.tensor([[3.0, 1.0, 2.0], [0.5, 4.0, 0.5]])
result = torch.min(x, dim=1, keepdim=True)
print(result.values)  # tensor([[1.], [0.5]])
print(result.indices) # tensor([[1], [0]])

Reduces each row and returns both the minimum values and their positions.

When to use it

Global minimum

Find one lowest value across scores, losses, or measurements.

Axis reduction

Select the smallest value independently across rows, channels, or another axis.

Minimum positions

Retrieve both the minimum values and their locations for routing or masking decisions.

Interview questions

QWhat does torch.min return with and without dim?

Without dim, torch.min returns one scalar containing the minimum over every element. With dim, it returns a named tuple of values and indices for each reduced slice.

Follow-up: How can the reduced dimension be preserved? Set keepdim=True, which leaves that dimension at size one.

QWhat is the effect of keepdim?

keepdim defaults to False. When false, the reduced dimension is removed; when true, it remains with size one in both returned tensors.

Follow-up: What shape results from a tensor of shape (2, 3, 4) reduced over dim=1 with keepdim=True? Both outputs have shape (2, 1, 4).

QHow are indices handled when minimum values tie?

The dimensional form returns values and indices, where indices identify the location of each minimum and correspond to argmin results. If minima tie within a reduced row, the first minimum's index is selected.

Follow-up: What operation provides only those locations? torch.argmin returns the indices and documents the same first-index tie behavior.

  • torch.argmin
  • torch.Tensor.min
  • torch.squeeze

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