min

Documentation

treetensor.torch.min(input, *args, reduce=None, **kwargs)[source]

In treetensor, you can get the min result of a whole tree with this function.

Example:

>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.min(torch.tensor([1.0, 2.0, 1.5]))  # the same as torch.min
tensor(1.)

>>> ttorch.min(ttorch.tensor({
...     'a': [1.0, 2.0, 1.5],
...     'b': {'x': [[1.8, 0.9], [1.3, 2.5]]},
... }))
tensor(0.9000)

>>> ttorch.min(ttorch.tensor({
...     'a': [1.0, 2.0, 1.5],
...     'b': {'x': [[1.8, 0.9], [1.3, 2.5]]},
... }), reduce=False)
<Tensor 0x7fd45b5913c8>
├── a --> tensor(1.)
└── b --> <Tensor 0x7fd45b5912e8>
    └── x --> tensor(0.9000)

>>> ttorch.min(ttorch.tensor({
...     'a': [1.0, 2.0, 1.5],
...     'b': {'x': [[1.8, 0.9], [1.3, 2.5]]},
... }), dim=0)
torch.return_types.min(
values=<Tensor 0x7fd45b52d2e8>
├── a --> tensor(1.)
└── b --> <Tensor 0x7fd45b52d208>
    └── x --> tensor([1.3000, 0.9000])
,
indices=<Tensor 0x7fd45b591cc0>
├── a --> tensor(0)
└── b --> <Tensor 0x7fd45b52d3c8>
    └── x --> tensor([1, 0])
)

Torch Version Related

This documentation is based on torch.min in torch v2.0.1+cu117. Its arguments’ arrangements depend on the version of pytorch you installed.

If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.

1
python -c 'import torch;print(torch.__version__)'

The arguments and keyword arguments supported in torch v2.0.1+cu117 is listed below.

Description From Torch v2.0.1+cu117

torch.min(input)Tensor

Returns the minimum value of all elements in the input tensor.

Warning

This function produces deterministic (sub)gradients unlike min(dim=0)

Args:

input (Tensor): the input tensor.

Example:

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.6750,  1.0857,  1.7197]])
>>> torch.min(a)
tensor(0.6750)
torch.min(input, dim, keepdim=False, *, out=None)

Returns a namedtuple (values, indices) where values is the minimum value of each row of the input tensor in the given dimension dim. And indices is the index location of each minimum value found (argmin).

If keepdim is True, the output tensors are of the same size as input except in the dimension dim where they are of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensors having 1 fewer dimension than input.

Note

If there are multiple minimal values in a reduced row then the indices of the first minimal value are returned.

Args:

input (Tensor): the input tensor. dim (int): the dimension to reduce. keepdim (bool): whether the output tensor has dim retained or not.

Keyword args:

out (tuple, optional): the tuple of two output tensors (min, min_indices)

Example:

>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.6248,  1.1334, -1.1899, -0.2803],
        [-1.4644, -0.2635, -0.3651,  0.6134],
        [ 0.2457,  0.0384,  1.0128,  0.7015],
        [-0.1153,  2.9849,  2.1458,  0.5788]])
>>> torch.min(a, 1)
torch.return_types.min(values=tensor([-1.1899, -1.4644,  0.0384, -0.1153]), indices=tensor([2, 0, 1, 0]))
torch.min(input, other, *, out=None)Tensor

See torch.minimum().