DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

round, ceil, floor and trunc in PyTorch

Buy Me a Coffee

round() can round the zero or more elements of a 0D or more D tensor up to 4 decimal places, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • round() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int or float).
  • The 2nd argument with torch or the 1st argument with a tensor is decimals(Optional-Default:0-Type:int): Memos:
    • It is a decimal place.
    • You must use decimals=.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

my_tensor = torch.tensor([4.735277, -3.370677, 1.888877, -7.888877])

torch.round(input=my_tensor)
my_tensor.round()
torch.round(input=my_tensor, decimals=0)
# tensor([5., -3., 2., -8.])

torch.round(input=my_tensor, decimals=1)
# tensor([4.7000, -3.4000, 1.9000, -7.9000])

torch.round(input=my_tensor, decimals=2)
# tensor([4.7400, -3.3700, 1.8900, -7.8900])

torch.round(input=my_tensor, decimals=3)
# tensor([4.7350, -3.3710, 1.8890, -7.8890])

torch.round(input=my_tensor, decimals=4)
torch.round(input=my_tensor, decimals=5)
torch.round(input=my_tensor, decimals=6)
etc...
# tensor([4.7353, -3.3707, 1.8889, -7.8889])

my_tensor = torch.tensor([4, -3, 1, -7])

torch.round(input=my_tensor)
# tensor([4, -3, 1, -7])
Enter fullscreen mode Exit fullscreen mode

ceil() can round up the zero or more elements of a 0D or more D tensor, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • ceil() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int or float).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • ceil() doesn't have decimals argument.
import torch

my_tensor = torch.tensor([4.735277, -3.370677, 1.888877, -7.888877])

torch.ceil(input=my_tensor)
my_tensor.ceil()
# tensor([5., -3., 2., -7.])

my_tensor = torch.tensor([4, -3, 1, -7])

torch.ceil(input=my_tensor)
# tensor([4, -3, 1, -7])
Enter fullscreen mode Exit fullscreen mode

floor() can round down the zero or more elements of a 0D or more D tensor, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • floor() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int or float).
  • There is out argument with torch(Optional-Default:None:Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • floor() doesn't have decimals argument.
import torch

my_tensor = torch.tensor([4.735277, -3.370677, 1.888877, -7.888877])

torch.floor(input=my_tensor)
my_tensor.floor()
# tensor([4., -4., 1., -8.])

my_tensor = torch.tensor([4, -3, 1, -7])

torch.floor(input=my_tensor)
# tensor([4, -3, 1, -7])
Enter fullscreen mode Exit fullscreen mode

trunc() can truncate the decimal part of zero or more elements of a 0D or more D tensor, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • trunc() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int or float).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • trunc() doesn't have decimals argument.
  • fix() is the alias of trunc().
import torch

my_tensor = torch.tensor([4.735277, -3.370677, 1.888877, -7.888877])

torch.trunc(input=my_tensor)
my_tensor.trunc()
# tensor([4., -3., 1., -7.])

my_tensor = torch.tensor([4, -3, 1, -7])

torch.trunc(input=my_tensor)
# tensor([4, -3, 1, -7])
Enter fullscreen mode Exit fullscreen mode

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay