Skip to content

Commit acfdb31

Browse files
Revise .round() function documentation
Updated the documentation for the .round() function in PyTorch to clarify its behavior and parameters.
1 parent 26de85d commit acfdb31

File tree

1 file changed

+41
-20
lines changed
  • content/pytorch/concepts/tensor-operations/terms/round

1 file changed

+41
-20
lines changed
Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,66 @@
11
---
2-
Title: 'round.()'
2+
Title: '.round()'
33
Description: 'Rounds each element to the nearest integer or specified number of decimals.'
44
Subjects:
55
- 'Computer Science'
66
- 'Machine Learning'
77
- 'Data Science'
88
Tags:
9-
- 'Round'
9+
- 'Integers'
1010
- 'Python'
1111
- 'Tensor'
12-
- 'Computer Science'
1312
CatalogContent:
1413
- 'intro-to-py-torch-and-neural-networks'
1514
- 'paths/computer-science'
16-
- 'paths/machine-learning'
17-
- 'paths/data-science'
1815
---
1916

20-
In PyTorch, the **`.round`** function returns a new [tensor](https://docs.pytorch.org/docs/stable/generated/torch.round.html#torch.round) rounded to the nearest integer.
17+
In PyTorch, the **`.round()`** function returns a new [tensor](https://docs.pytorch.org/docs/stable/generated/torch.round.html#torch.round) with each element rounded to the nearest integer or to a specified number of decimal places. Values exactly halfway between two integers follow “round half to even,” also known as banker's rounding.
2118

2219
## Syntax
2320

21+
```pseudo
22+
torch.round(input, *, decimals=0, out=None)
2423
```
25-
torch.round(input, decimals=0)
26-
```
2724

28-
- `input`: (tensor) the input tensor to be rounded.
29-
- `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.
25+
**Parameters:**
26+
27+
- `input` (tensor): The input tensor to be rounded.
28+
- `decimals` (int, optional): Number of decimal places to round to.
29+
- Positive values round to the right of the decimal.
30+
- Negative values round to the left of the decimal.
31+
- Default is 0.
32+
- `out` (Tensor, optional): A tensor to store the result.
33+
34+
**Return value:**
35+
36+
Returns a tensor containing the rounded values. If `out` is provided, the result is written into it and returned.
37+
38+
## Example
3039

31-
## Examples
40+
In this example:
3241

33-
```python
34-
torch.round(torch.tensor((4.7, -2.3, 9.1, -7.7)))
42+
In this example, `.round()` is used with default rounding, half-even rounding, and rounding with positive and negative decimal values:
3543

36-
# Values equidistant from two integers are rounded towards the
37-
# the nearest even value (zero is treated as even)
38-
torch.round(torch.tensor([-0.5, 0.5, 1.5, 2.5]))
44+
```py
45+
import torch
46+
47+
print(torch.round(torch.tensor([4.7, -2.3, 9.1, -7.7])))
48+
49+
# Halfway values follow round-half-to-even
50+
print(torch.round(torch.tensor([-0.5, 0.5, 1.5, 2.5])))
51+
52+
# Rounding to a specific number of decimal places
53+
print(torch.round(torch.tensor([0.1234567]), decimals=3))
54+
55+
# Rounding to the left of the decimal
56+
print(torch.round(torch.tensor([1200.1234567]), decimals=-3))
57+
```
3958

40-
# A positive decimals argument rounds to the to that decimal place
41-
torch.round(torch.tensor([0.1234567]), decimals=3)
59+
The output of this code is:
4260

43-
# A negative decimals argument rounds to the left of the decimal
44-
torch.round(torch.tensor([1200.1234567]), decimals=-3)
61+
```shell
62+
tensor([ 5., -2., 9., -8.])
63+
tensor([-0., 0., 2., 2.])
64+
tensor([0.1230])
65+
tensor([1000.])
4566
```

0 commit comments

Comments
 (0)