DEV Community

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

Posted on

div(), remainder() and fmod() in PyTorch

*My post explains add(), sub() and mul().

div() can do division with 0D or more D tensors or scalars as shown below:

*Memos:

  • div() can be used with torch or a tensor.
  • The 1st argument(tensor or scalar of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(tensor or scalar of int, float, complex or bool) with torch or the 1st argument(tensor or scalar of int, float, complex or bool) is other(Required).
  • There is rounding_mode argument(str) (Optional-Default:None) with torch or a tensor. *Memos:
    • None, "trunc" or "floor" can be set.
    • None performs no rounding and, if both input and other are integer types, promotes the inputs to the default scalar type. *Equivalent to true division in Python (the / operator) and NumPyโ€™s np.true_divide().
    • "trunc" rounds the results of the division towards zero. *Equivalent to C-style integer division.
    • "floor" rounds the results of the division down. *Equivalent to floor division in Python (the // operator) and NumPyโ€™s np.floor_divide().
    • The tensor or scalar of complex or bool cannot be used with "trunc" or "floor".
    • Setting 0(int) to other gets ZeroDivisionError with "trunc" or "floor".
  • divide() is the alias of div().
  • true_divide() is the alias of div() with rounding_mode=None.
  • floor_divide() is the same as div() with rounding_mode="trunc" as long as I experimented.
import torch

tensor1 = torch.tensor(9.75)
tensor2 = torch.tensor([[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]])
torch.div(input=tensor1, other=tensor2)
tensor1.div(other=tensor2)
# tensor([[2.2887, -2.1476, 2.8932], [-4.5139, 1.7956, -1.6304]])

torch.div(input=9.75, other=tensor2)
# tensor([[2.2887, -2.1476, 2.8932], [-4.5139, 1.7956, -1.6304]])

torch.div(input=tensor1, other=4.26)
# tensor(2.2887)

torch.div(input=9.75, other=4.26)
# tensor(2.2887)

torch.div(input=tensor1, other=tensor2, rounding_mode="trunc")
# tensor([[2., -2., 2.], [-4., 1., -1.]])

torch.div(input=9.75, other=tensor2, rounding_mode="trunc")
# tensor([[2., -2., 2.], [-4., 1., -1.]])

torch.div(input=tensor1, other=4.26, rounding_mode="trunc")
# tensor(2.)

torch.div(input=9.75, other=4.26, rounding_mode="trunc")
# tensor(2.)

torch.div(input=tensor1, other=tensor2, rounding_mode="floor")
# tensor([[2., -3., 2.], [-5., 1., -2.]])

torch.div(input=9.75, other=tensor2, rounding_mode="floor")
# tensor([[2., -3., 2.], [-5., 1., -2.]])

torch.div(input=tensor1, other=4.26, rounding_mode="floor")
# tensor(2.)

torch.div(input=9.75, other=4.26, rounding_mode="floor")
# tensor(2.)

tensor1 = torch.tensor([9.75, 7.08, 6.26])
tensor2 = torch.tensor([[[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]],
                        [[6.04, 2.21, 8.55], [3.63, 9.21, 1.39]]])
torch.div(input=tensor1, other=tensor2)
# tensor([[[2.2887, -1.5595, 1.8576], [-4.5139, 1.3039, -1.0468]],
#         [[1.6142, 3.2036, 0.7322], [2.6860, 0.7687, 4.5036]]])

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[[4, -4, 3], [-2, 5, -5]],
                        [[6, 2, 8], [3, 9, 1]]])
torch.div(input=tensor1, other=tensor2)
# tensor([[[2.2500, -1.7500, 2.0000], [-4.5000, 1.4000, -1.2000]],
#         [[1.5000, 3.5000, 0.7500], [3.0000, 0.7778, 6.0000]]])

tensor1 = torch.tensor([9.+0.j, 7.+0.j, 6.+0.j])
tensor2 = torch.tensor([[[4.+0.j, -4.+0.j, 3.+0.j],
                         [-2.+0.j, 5.+0.j, -5.+0.j]],
                        [[6.+0.j, 2.+0.j, 8.+0.j],
                         [3.+0.j, 9.+0.j, 1.+0.j]]])
torch.div(input=tensor1, other=tensor2)
# tensor([[[2.2500+0.j, -1.7500-0.j, 2.0000+0.j],
#          [-4.5000-0.j, 1.4000+0.j, -1.2000-0.j]],
#         [[1.5000+0.j, 3.5000+0.j, 0.7500+0.j],
#          [3.0000+0.j, 0.7778+0.j, 6.0000+0.j]]])

tensor1 = torch.tensor([True, False, True])
tensor2 = torch.tensor([[[True, False, True], [True, False, True]],
                        [[False, True, False], [False, True, False]]])
torch.div(input=tensor1, other=tensor2)
# tensor([[[1., nan, 1.], [1., nan, 1.]],
#         [[inf, 0., inf], [inf, 0., inf]]])

torch.div(input=9.75, other=4.26)
# tensor(2.2887)

torch.div(input=9, other=4)
# tensor(2.2500)

torch.div(input=9.+0.j, other=4.+0.j)
# tensor(2.2500+0.j)

torch.div(input=True, other=False)
# tensor(inf)
Enter fullscreen mode Exit fullscreen mode

remainder() can do the modulo(mod) calculation of Pythonโ€™s modulus operation with 0D or more D tensors or scalars as shown below:

*Memos:

  • remainder() can be used with torch or a tensor.
  • The 1st argument(tensor or scalar of int or float) with torch or using a tensor(tensor of int or float) is input(Required). *You must use a scalar without input=.
  • The 2nd argument(tensor or scalar of int or float) with torch or the 1st argument(tensor or scalar of int or float) is other(Required).
  • Setting 0(int) to other gets ZeroDivisionError.
  • The combination of a scalar(input) and scalar(other) is not allowed.
  • The result has the same sign as other.
import torch

tensor1 = torch.tensor(9)
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.remainder(input=tensor1, other=tensor2)
tensor1.remainder(other=tensor2)
torch.remainder(9, other=tensor2)
# tensor([[1, -3, 0], [-1, 4, -1]])

torch.remainder(input=tensor1, other=4)
# tensor(1)

tensor1 = torch.tensor(-9)
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.remainder(input=tensor1, other=tensor2)
torch.remainder(-9, other=tensor2)
# tensor([[3, -1, 0], [-1, 1, -4]])

torch.remainder(input=tensor1, other=4)
# tensor(3)

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[[4, -4, 3], [-2, 5, -5]],
                        [[6, 2, 8], [3, 9, 1]]])
torch.remainder(input=tensor1, other=tensor2)
# tensor([[[1, -1, 0], [-1, 2, -4]],
#         [[3, 1, 6], [0, 7, 0]]])

torch.remainder(9, other=tensor2)
# tensor([[[1, -3, 0], [-1, 4, -1]],
#         [[3, 1, 1], [0, 0, 0]]])

torch.remainder(input=tensor1, other=4)
# tensor([1, 3, 2])

tensor1 = torch.tensor([9.75, 7.08, 6.26])
tensor2 = torch.tensor([[[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]],
                        [[6.04, 2.21, 8.55], [3.63, 9.21, 1.39]]])
torch.remainder(input=tensor1, other=tensor2)
# tensor([[[1.2300, -2.0000, 2.8900], [-1.0500, 1.6500, -5.7000]],
#         [[3.7100, 0.4500, 6.2600], [2.4900, 7.0800, 0.7000]]])

torch.remainder(9.75, other=tensor2)
# tensor([[[1.2300, -3.8700, 3.0100], [-1.0500, 4.3200, -2.2100]],
#         [[3.7100, 0.9100, 1.2000], [2.4900, 0.5400, 0.0200]]])

torch.remainder(input=tensor1, other=4.26)
# tensor([1.2300, 2.8200, 2.0000])
Enter fullscreen mode Exit fullscreen mode

fmod() can do the modulo(mod) calculation of C++โ€™s std::fmod with 0D or more D tensors or scalars as shown below:

*Memos:

  • fmod() can be used with torch or a tensor.
  • The 1st argument(tensor of int or float) with torch or using a tensor(tensor of int or float) is input(Required).
  • The 2nd argument(tensor or scalar of int or float) with torch or the 1st argument(tensor or scalar of int or float) is other(Required).
  • Setting 0(int) to other gets ZeroDivisionError.
  • The result has the same sign as input.
import torch

tensor1 = torch.tensor(9)
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.fmod(input=tensor1, other=tensor2)
tensor1.fmod(other=tensor2)
# tensor([[1, 1, 0], [1, 4, 4]])

torch.fmod(input=tensor1, other=4)
# tensor(1)

tensor1 = torch.tensor(-9)
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.fmod(input=tensor1, other=tensor2)
# tensor([[-1, -1, 0], [-1, -4, -4]])

torch.fmod(input=tensor1, other=4)
# tensor(-1)

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[[4, -4, 3], [-2, 5, -5]],
                        [[6, 2, 8], [3, 9, 1]]])
torch.fmod(input=tensor1, other=tensor2)
# tensor([[[1, 3, 0], [1, 2, 1]],
#         [[3, 1, 6], [0, 7, 0]]])

torch.fmod(input=tensor1, other=4)
# tensor([1, 3, 2])

tensor1 = torch.tensor([9.75, 7.08, 6.26])
tensor2 = torch.tensor([[[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]],
                        [[6.04, 2.21, 8.55], [3.63, 9.21, 1.39]]])
torch.fmod(input=tensor1, other=tensor2)
# tensor([[[1.2300, 2.5400, 2.8900], [1.1100, 1.6500, 0.2800]],
#         [[3.7100, 0.4500, 6.2600], [2.4900, 7.0800, 0.7000]]])

torch.fmod(input=tensor1, other=4.26)
# tensor([1.2300, 2.8200, 2.0000])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)