From 4832cb40683ef188c085b6584c9a6e9c20f745e8 Mon Sep 17 00:00:00 2001 From: John Griffin Date: Thu, 11 Dec 2025 10:46:00 -0600 Subject: [PATCH 1/4] creates new tensor operation .round() file and description and usage of .round() --- .../tensor-operations/terms/round/round.md | 41 +++++++++++++++++++ contribute/markdown.md | 0 markdown.md | 1 + 3 files changed, 42 insertions(+) create mode 100644 content/pytorch/concepts/tensor-operations/terms/round/round.md create mode 100644 contribute/markdown.md create mode 100644 markdown.md diff --git a/content/pytorch/concepts/tensor-operations/terms/round/round.md b/content/pytorch/concepts/tensor-operations/terms/round/round.md new file mode 100644 index 00000000000..b91f7b84bc2 --- /dev/null +++ b/content/pytorch/concepts/tensor-operations/terms/round/round.md @@ -0,0 +1,41 @@ +--- +Title: 'round.()' +Description: 'Rounds each element to the nearest integer or specified number of decimals. +Subjects: + - 'Computer Science' + - 'Machine Learning' + - 'Data Science' +Tags: + - 'Round' + - 'Python' + - 'Tensor' + - 'Computer Science' +Catalog Content: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/computer-science' + - 'paths/machine-learning' + - 'paths/data-science +--- + +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. + +## Syntax + +```torch.round(input, decimals=0) +``` +- `input`: (tensor) the input tensor to be rounded. +- `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. + +## Examples + +torch.round(torch.tensor((4.7, -2.3, 9.1, -7.7))) + +# 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])) + +# A positive decimals argument rounds to the to that decimal place +torch.round(torch.tensor([0.1234567]), decimals=3) + +# A negative decimals argument rounds to the left of the decimal +torch.round(torch.tensor([1200.1234567]), decimals=-3) \ No newline at end of file diff --git a/contribute/markdown.md b/contribute/markdown.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/markdown.md b/markdown.md new file mode 100644 index 00000000000..a585d09f958 --- /dev/null +++ b/markdown.md @@ -0,0 +1 @@ + From 85bae8db4800fcd8b59ac568fade421ef0610801 Mon Sep 17 00:00:00 2001 From: John Griffin Date: Thu, 11 Dec 2025 12:19:18 -0600 Subject: [PATCH 2/4] tensor operations round --- .../tensor-operations/terms/round/round.md | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/round/round.md b/content/pytorch/concepts/tensor-operations/terms/round/round.md index b91f7b84bc2..e6fb7ec98aa 100644 --- a/content/pytorch/concepts/tensor-operations/terms/round/round.md +++ b/content/pytorch/concepts/tensor-operations/terms/round/round.md @@ -1,41 +1,45 @@ --- Title: 'round.()' -Description: 'Rounds each element to the nearest integer or specified number of decimals. +Description: 'Rounds each element to the nearest integer or specified number of decimals.' Subjects: - - 'Computer Science' - - 'Machine Learning' - - 'Data Science' + - 'Computer Science' + - 'Machine Learning' + - 'Data Science' Tags: - - 'Round' - - 'Python' - - 'Tensor' - - 'Computer Science' -Catalog Content: - - 'intro-to-py-torch-and-neural-networks' - - 'paths/computer-science' - - 'paths/machine-learning' - - 'paths/data-science + - 'Round' + - 'Python' + - 'Tensor' + - 'Computer Science' +CatalogContent: + - 'intro-to-py-torch-and-neural-networks' + - 'paths/computer-science' + - 'paths/machine-learning' + - 'paths/data-science' --- -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. +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. ## Syntax -```torch.round(input, decimals=0) ``` +torch.round(input, decimals=0) +``` + - `input`: (tensor) the input tensor to be rounded. - `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. ## Examples +```python torch.round(torch.tensor((4.7, -2.3, 9.1, -7.7))) # Values equidistant from two integers are rounded towards the -# the nearest even value (zero is treated as even) +# the nearest even value (zero is treated as even) torch.round(torch.tensor([-0.5, 0.5, 1.5, 2.5])) # A positive decimals argument rounds to the to that decimal place torch.round(torch.tensor([0.1234567]), decimals=3) # A negative decimals argument rounds to the left of the decimal -torch.round(torch.tensor([1200.1234567]), decimals=-3) \ No newline at end of file +torch.round(torch.tensor([1200.1234567]), decimals=-3) +``` From acfdb31bf215f8cf531944bfd4474d5da757d304 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 12 Dec 2025 18:02:30 +0530 Subject: [PATCH 3/4] Revise .round() function documentation Updated the documentation for the .round() function in PyTorch to clarify its behavior and parameters. --- .../tensor-operations/terms/round/round.md | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/content/pytorch/concepts/tensor-operations/terms/round/round.md b/content/pytorch/concepts/tensor-operations/terms/round/round.md index e6fb7ec98aa..96406d074e4 100644 --- a/content/pytorch/concepts/tensor-operations/terms/round/round.md +++ b/content/pytorch/concepts/tensor-operations/terms/round/round.md @@ -1,45 +1,66 @@ --- -Title: 'round.()' +Title: '.round()' Description: 'Rounds each element to the nearest integer or specified number of decimals.' Subjects: - 'Computer Science' - 'Machine Learning' - 'Data Science' Tags: - - 'Round' + - 'Integers' - 'Python' - 'Tensor' - - 'Computer Science' CatalogContent: - 'intro-to-py-torch-and-neural-networks' - 'paths/computer-science' - - 'paths/machine-learning' - - 'paths/data-science' --- -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. +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. ## Syntax +```pseudo +torch.round(input, *, decimals=0, out=None) ``` -torch.round(input, decimals=0) -``` -- `input`: (tensor) the input tensor to be rounded. -- `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. +**Parameters:** + +- `input` (tensor): The input tensor to be rounded. +- `decimals` (int, optional): Number of decimal places to round to. + - Positive values round to the right of the decimal. + - Negative values round to the left of the decimal. + - Default is 0. +- `out` (Tensor, optional): A tensor to store the result. + +**Return value:** + +Returns a tensor containing the rounded values. If `out` is provided, the result is written into it and returned. + +## Example -## Examples +In this example: -```python -torch.round(torch.tensor((4.7, -2.3, 9.1, -7.7))) +In this example, `.round()` is used with default rounding, half-even rounding, and rounding with positive and negative decimal values: -# 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])) +```py +import torch + +print(torch.round(torch.tensor([4.7, -2.3, 9.1, -7.7]))) + +# Halfway values follow round-half-to-even +print(torch.round(torch.tensor([-0.5, 0.5, 1.5, 2.5]))) + +# Rounding to a specific number of decimal places +print(torch.round(torch.tensor([0.1234567]), decimals=3)) + +# Rounding to the left of the decimal +print(torch.round(torch.tensor([1200.1234567]), decimals=-3)) +``` -# A positive decimals argument rounds to the to that decimal place -torch.round(torch.tensor([0.1234567]), decimals=3) +The output of this code is: -# A negative decimals argument rounds to the left of the decimal -torch.round(torch.tensor([1200.1234567]), decimals=-3) +```shell +tensor([ 5., -2., 9., -8.]) +tensor([-0., 0., 2., 2.]) +tensor([0.1230]) +tensor([1000.]) ``` From c0479237dba664888d90a7cb14013c3b0185a2c5 Mon Sep 17 00:00:00 2001 From: John Griffin <116393056+Chainbreaker7747@users.noreply.github.com> Date: Fri, 12 Dec 2025 07:47:40 -0600 Subject: [PATCH 4/4] Delete contribute/markdown.md --- contribute/markdown.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 contribute/markdown.md diff --git a/contribute/markdown.md b/contribute/markdown.md deleted file mode 100644 index e69de29bb2d..00000000000