DEV Community

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

Posted on • Edited on

div in PyTorch

Buy Me a Coffee

*Memos:

div() can do division with two of the 0D or more D tensors of zero or more elements or scalars or the 0D or more D tensor of zero or more elements and a scalar, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • div() can be used with torch or a tensor.
  • The 1st argument(input) with torch(Type:tensor or scalar of int, float, complex or bool) or using a tensor(Type:tensor of int, float, complex or bool)(Required).
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor or scalar of int, float, complex or bool).
  • There is rounding_mode argument with torch or a tensor(Optional-Default:None-Type:str): *Memos:
    • None, "trunc" or "floor" can be set.
    • rounding_mode= must be used.
    • 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".
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • 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, 7.08, 6.26])
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, -1.5595, 1.8576], [-4.5139, 1.3039, -1.0468]])

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, 1.6620, 1.4695])

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

torch.div(input=tensor1, other=tensor2, rounding_mode="trunc")
# tensor([[2., -1., 1.], [-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., 1., 1.])

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

torch.div(input=tensor1, other=tensor2, rounding_mode="floor")
# tensor([[2., -2., 1.], [-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., 1., 1.])

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

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

torch.div(input=tensor1, other=tensor2)
# tensor([[2.2500, -1.7500, 2.0000], [-4.5000, 1.4000, -1.2000]])

torch.div(input=9, other=tensor2)
# tensor([[2.2500, -2.2500, 3.0000], [-4.5000, 1.8000, -1.8000]])

torch.div(input=tensor1, other=4)
# tensor([2.2500, 1.7500, 1.5000])

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

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]])
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]])

torch.div(input=9.+0.j, other=tensor2)
# tensor([[2.2500+0.j, -2.2500-0.j, 3.0000+0.j],
#         [-4.5000-0.j, 1.8000+0.j, -1.8000-0.j]])

torch.div(input=tensor1, other=4.+0.j)
# tensor([2.2500+0.j, 1.7500+0.j, 1.5000+0.j])

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

tensor1 = torch.tensor([True, False, True])
tensor2 = torch.tensor([[False, True, False], [True, False, True]])

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

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

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

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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay