Shortcuts

torch.aminmax

torch.aminmax(input, *, dim=None, keepdim=False, out=None) -> (Tensor min, Tensor max)

Computes the minimum and maximum values of the input tensor.

Parameters

input (Tensor) – The input tensor

Keyword Arguments
  • dim (Optional[int]) – The dimension along which to compute the values. If None, computes the values over the entire input tensor. Default is None.

  • keepdim (bool) – If True, the reduced dimensions will be kept in the output tensor as dimensions with size 1 for broadcasting, otherwise they will be removed, as if calling (torch.squeeze()). Default is False.

  • out (Optional[Tuple[Tensor, Tensor]]) – Optional tensors on which to write the result. Must have the same shape and dtype as the expected output. Default is None.

Returns

A named tuple (min, max) containing the minimum and maximum values.

Raises

RuntimeError – If any of the dimensions to compute the values over has size 0.

Note

NaN values are propagated to the output if at least one value is NaN.

See also

torch.amin() computes just the minimum value torch.amax() computes just the maximum value

Example:

>>> torch.aminmax(torch.tensor([1, -3, 5]))
torch.return_types.aminmax(
min=tensor(-3),
max=tensor(5))

>>> # aminmax propagates NaNs
>>> torch.aminmax(torch.tensor([1, -3, 5, torch.nan]))
torch.return_types.aminmax(
min=tensor(nan),
max=tensor(nan))

>>> t = torch.arange(10).view(2, 5)
>>> t
tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
>>> t.aminmax(dim=0, keepdim=True)
torch.return_types.aminmax(
min=tensor([[0, 1, 2, 3, 4]]),
max=tensor([[5, 6, 7, 8, 9]]))

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources