DEV Community

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

Posted on

round(), ceil(), floor(), trunc() and frac() in PyTorch

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

*Memos:

  • round() can be called both from torch and a tensor.
  • Only zero or more floating-point numbers or integers can be used so zero or more complex numbers or boolean values cannot be used.
  • The 2nd argument with torch or the 1st argument with a tensor is a decimal place(decimals). *You must use decimals= to set a decimal place.
  • Zero or more integers cannot be used with decimals=.
import torch

my_tensor = torch.tensor([4.7352, -2.3706, 9.1648, -7.7054])

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

torch.round(my_tensor, decimals=1)
my_tensor.round(decimals=1)
# tensor([4.7000, -2.4000, 9.2000, -7.7000])

torch.round(my_tensor, decimals=2)
my_tensor.round(decimals=2)
# tensor([4.7400, -2.3700, 9.1600, -7.7100])

torch.round(my_tensor, decimals=3)
my_tensor.round(decimals=3)
# tensor([4.7350, -2.3710, 9.1650, -7.7050])

torch.round(my_tensor, decimals=4)
my_tensor.round(decimals=4)
# tensor([4.7352, -2.3706, 9.1648, -7.7054])

my_tensor = torch.tensor([4, -2, 9, -7])
torch.round(my_tensor)
my_tensor.round()
# tensor([4, -2, 9, -7])
Enter fullscreen mode Exit fullscreen mode

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

*Memos:

  • ceil() can be called both from torch and a tensor.
  • Only zero or more floating-point numbers or integers can be used so zero or more complex numbers or boolean values cannot be used.
  • ceil() doesn't have decimals argument.
import torch

my_tensor = torch.tensor([4.7352, -2.3706, 9.1648, -7.7054])

torch.ceil(my_tensor)
my_tensor.ceil()
# tensor([5., -2., 10., -7.])

my_tensor = torch.tensor([4, -2, 9, -7])

torch.ceil(my_tensor)
my_tensor.ceil()
# tensor([4, -2, 9, -7])
Enter fullscreen mode Exit fullscreen mode

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

*Memos:

  • floor() can be called both from torch and a tensor.
  • Only zero or more floating-point numbers or integers can be used so zero or more complex numbers or boolean values cannot be used.
  • floor() doesn't have decimals argument.
import torch

my_tensor = torch.tensor([4.7352, -2.3706, 9.1648, -7.7054])

torch.floor(my_tensor)
my_tensor.floor()
# tensor([4., -3., 9., -8.])

my_tensor = torch.tensor([4, -2, 9, -7])

torch.floor(my_tensor)
my_tensor.floor()
# tensor([4, -2, 9, -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 as shown below:

*Memos:

  • trunc() can be called both from torch and a tensor.
  • Only zero or more floating-point numbers or integers can be used so zero or more complex numbers or boolean values cannot be used.
  • trunc() doesn't have decimals argument.
import torch

my_tensor = torch.tensor([4.7352, -2.3706, 9.1648, -7.7054])

torch.trunc(my_tensor)
my_tensor.trunc()
# tensor([4., -2., 9., -7.])

my_tensor = torch.tensor([4, -2, 9, -7])

torch.trunc(my_tensor)
my_tensor.trunc()
# tensor([4, -2, 9, -7])
Enter fullscreen mode Exit fullscreen mode

frac() can get the decimal part of zero or more elements of a 0D or more D tensor as shown below:

*Memos:

  • frac() can be called both from torch and a tensor.
  • Only zero or more floating-point numbers can be used so zero or more integers, complex numbers or boolean values cannot be used.
  • frac() doesn't have decimals argument.
import torch

my_tensor = torch.tensor([4.7352, -2.3706, 9.1648, -7.7054])

torch.frac(my_tensor)
my_tensor.frac()
# tensor([0.7352, -0.3706, 0.1648, -0.7054])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)