Global minimum
Find one lowest value across scores, losses, or measurements.
PyTorch · Function
Returns the minimum value of a tensor, globally or along a selected dimension. Dimension-wise reduction also returns the index of each minimum.
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.
torch.min(*input*, ***, out=None) → Tensor
torch.min(*input*, dim, keepdim=False, ***, out=None) → (Tensor, LongTensor)| Name | Type | Description |
|---|---|---|
input | Tensor | Tensor to reduce. |
dim | int, optional | Dimension to reduce; omit it to reduce all dimensions. |
keepdim | bool, optional | Whether to retain the reduced dimension; defaults to False. |
out | tuple, optional | Optional tuple of output tensors for the dimensional form, or output tensor for the all-elements form. |
Without dim, a Tensor containing the global minimum. With dim, a namedtuple (values, indices), where values contains minima and indices contains their locations.
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.
Find one lowest value across scores, losses, or measurements.
Select the smallest value independently across rows, channels, or another axis.
Retrieve both the minimum values and their locations for routing or masking decisions.
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.
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).
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.argmintorch.Tensor.mintorch.squeeze