Shortcuts

torch.round

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

Rounds elements of input to the nearest integer.

For integer inputs, follows the array-api convention of returning a copy of the input tensor. The return type of output is same as that of input’s dtype.

Note

This function implements the “round half to even” to break ties when a number is equidistant from two integers (e.g. round(2.5) is 2).

When the :attr:`decimals` argument is specified the algorithm used is similar to NumPy’s around. This algorithm is fast but inexact and it can easily overflow for low precision dtypes. Eg. round(tensor([10000], dtype=torch.float16), decimals=3) is inf.

See also

torch.ceil(), which rounds up. torch.floor(), which rounds down. torch.trunc(), which rounds towards zero.

Parameters
  • input (Tensor) – the input tensor.

  • decimals (int) – Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.

Keyword Arguments

out (Tensor, optional) – the output tensor.

Example:

>>> torch.round(torch.tensor((4.7, -2.3, 9.1, -7.7)))
tensor([ 5.,  -2.,  9., -8.])

>>> # Values equidistant from two integers are rounded towards the
>>> #   the nearest even value (zero is treated as even)
>>> torch.round(torch.tensor([-0.5, 0.5, 1.5, 2.5]))
tensor([-0., 0., 2., 2.])

>>> # A positive decimals argument rounds to the to that decimal place
>>> torch.round(torch.tensor([0.1234567]), decimals=3)
tensor([0.1230])

>>> # A negative decimals argument rounds to the left of the decimal
>>> torch.round(torch.tensor([1200.1234567]), decimals=-3)
tensor([1000.])

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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy