|
1 | 1 | --- |
2 | | -Title: 'round.()' |
| 2 | +Title: '.round()' |
3 | 3 | Description: 'Rounds each element to the nearest integer or specified number of decimals.' |
4 | 4 | Subjects: |
5 | 5 | - 'Computer Science' |
6 | 6 | - 'Machine Learning' |
7 | 7 | - 'Data Science' |
8 | 8 | Tags: |
9 | | - - 'Round' |
| 9 | + - 'Integers' |
10 | 10 | - 'Python' |
11 | 11 | - 'Tensor' |
12 | | - - 'Computer Science' |
13 | 12 | CatalogContent: |
14 | 13 | - 'intro-to-py-torch-and-neural-networks' |
15 | 14 | - 'paths/computer-science' |
16 | | - - 'paths/machine-learning' |
17 | | - - 'paths/data-science' |
18 | 15 | --- |
19 | 16 |
|
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. |
21 | 18 |
|
22 | 19 | ## Syntax |
23 | 20 |
|
| 21 | +```pseudo |
| 22 | +torch.round(input, *, decimals=0, out=None) |
24 | 23 | ``` |
25 | | -torch.round(input, decimals=0) |
26 | | -``` |
27 | 24 |
|
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 |
30 | 39 |
|
31 | | -## Examples |
| 40 | +In this example: |
32 | 41 |
|
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: |
35 | 43 |
|
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 | +``` |
39 | 58 |
|
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: |
42 | 60 |
|
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.]) |
45 | 66 | ``` |
0 commit comments