Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

torch.round(): Rounding Elements of a Tensor

Home torch.round(): Rounding Elements of a Tensor
PyTorch torch.round() Method
  • Written by krunallathiya21
  • May 27, 2025
  • 0 Com
PyTorch

The torch.round() method rounds each element of an input tensor to the nearest integer. For halfway cases (e.g., 2.5), it rounds to the nearest even integer, aligning with IEEE 754 “round-half-to-even” (also known as banker’s rounding).

For example,

  1. 3.2 rounds to 3.0
  2. 3.5 rounds to 4.0,
  3. 2.5 rounds to 2.0,
  4. -2.5 rounds to -2.
torch.round()  

If you are using PyTorch version ≤ 2.6, it used the “round-half-away-from-zero” approach. This approach is now deprecated in PyTorch >= 2.7.

Here is the table that summarizes well:

PyTorch Version Rounding Tie-Break Rule round(2.5) round(-2.5)
≤ 2.6 Round half away from zero 3.0 -3.0
≥ 2.7 Round half to even (IEEE 754) 2.0 -2.0

The return type is a tensor with the same shape and data type as the input.

The related methods are torch.ceil(), torch.floor(), and torch.trunc().

Function signature

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

Parameters

Argument Description
input (Tensor) It is an input tensor that contains elements to round.
decimals (int, optional) It represents several decimals to round to.

The default value is 0 (round to the nearest integer). It can be positive or negative.

out (Tensor, optional) It represents an output tensor to store the results in. If you don’t provide, a new tensor will be created.

Rounding of a 1D Tensor

import torch

tensor = torch.tensor([1.2, 2.5, 3.5, 3.7, -1.5, -2.8])

rounded = torch.round(tensor)

print(tensor)
print(rounded)

# Output:
# tensor([ 1.2000,  2.5000, 3.5000,  3.7000, -1.5000, -2.8000])
# tensor([ 1.,  2., 4., 4., -2., -3.])

In the above code, 1.2 becomes 1.0 because 1.0 is the nearest integer. In the case of 2.5, since the value is halfway, it checks for the nearest even value, which is 2. That’s why it rounds to 2.

For 3.5, it is again halfway, and its nearest even value is 4, so it returns 4.0. For -1.5, the nearest even value is -2.0. Other output values are understandable.

Rounding of a 2D tensor

Rounding of a 2D tensor
import torch

tensor_2d = torch.tensor([[1.22, 3.55], [-4.5, -4.55]])

rounding_2d = torch.round(tensor_2d)

print(rounding_2d)

# Output:
# tensor([[ 1.,  4.],
#         [-4.,  -5.]])

Rounding to specific decimal places

specific rounding
import torch

tensor = torch.tensor([1.11456, 2.55555, 3.98765])

specific_rounded_tensor = torch.round(tensor, decimals=2)

print(tensor)
print(specific_rounded_tensor)

# Output:
# tensor([1.1146, 2.5556, 3.9876])
# tensor([1.1100, 2.5600, 3.9900])

In the above code, we specifically mentioned that elements should be rounded to 2 decimal places.

For example, 2.55555 rounds to 2.56, 1.1146 rounds to 1.1100, and 3.9875 rounds to 3.9900.

Negative decimal places

negative rounding

Let’s round to a multiple of a power of 10 (e.g., nearest 10, 100).

import torch

tensor = torch.tensor([123.456, 567.89, 12.34])

negative_rounding = torch.round(tensor, decimals=-1)

print(tensor)
print(negative_rounding)

# Output:
# tensor([123.4560, 567.8900,  12.3400])
# tensor([120., 570.,  10.])

In the above code, decimals=-1 rounds to the nearest 10. For example, 123.456 rounds to 120, 567.8900 rounds to 570, and 12.3400 rounds to 10.

In-place rounding

You can modify the original input tensor by using its variant method called “torch.round_()”.

import torch

tensor = torch.tensor([1.2, 2.5, 3.7])

print(tensor)
print(tensor.round_())

# Output:
# tensor([1.2000, 2.5000, 3.7000])
# tensor([1., 2., 4.])

You can see that it does not return a new tensor; instead, it modifies the original tensor.

Post Views: 12
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
site logo

Address:  TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

sprintchasetechnologies@gmail.com

(+91) 8000107255.

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact

Copyright by @SprintChase  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS