div() in PyTorch

*Memos:

My post explains remainder() and fmod().

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

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 sca…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

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


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-14T20:55:05+00:00) div() in PyTorch. Retrieved from https://www.scien.cx/2024/07/14/div-in-pytorch/

MLA
" » div() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Sunday July 14, 2024, https://www.scien.cx/2024/07/14/div-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday July 14, 2024 » div() in PyTorch., viewed ,<https://www.scien.cx/2024/07/14/div-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » div() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/14/div-in-pytorch/
CHICAGO
" » div() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/14/div-in-pytorch/
IEEE
" » div() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/14/div-in-pytorch/. [Accessed: ]
rf:citation
» div() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/14/div-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.