round

Documentation

treetensor.torch.round(input, *args, **kwargs)[source]

Returns a tree of new tensors with each of the elements of input rounded to the closest integer.

Examples:

>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.round(ttorch.tensor([[1.2, -1.8], [-2.3, 2.8]]))
tensor([[ 1., -2.],
        [-2.,  3.]])

>>> ttorch.round(ttorch.tensor({
...     'a': [[1.2, -1.8], [-2.3, 2.8]],
...     'b': {'x': [[1.0, -3.9, 1.3], [-4.8, -2.0, 2.8]]},
... }))
<Tensor 0x7fbf5333bc10>
├── a --> tensor([[ 1., -2.],
│                 [-2.,  3.]])
└── b --> <Tensor 0x7fbf5333bcd0>
    └── x --> tensor([[ 1., -4.,  1.],
                      [-5., -2.,  3.]])

Torch Version Related

This documentation is based on torch.round in torch v2.4.1+cu121. 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.4.1+cu121 is listed below.

Description From Torch v2.4.1+cu121

torch.round(input, *, out=None)Tensor

Returns a new tensor with each of the elements of input rounded to the closest integer.

Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.9920,  0.6077,  0.9734, -1.0362])
>>> torch.round(a)
tensor([ 1.,  1.,  1., -1.])