*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
orscalar
ofint
,float
,complex
orbool
) withtorch
or using a tensor(tensor
ofint
,float
,complex
orbool
) isinput
(Required). - The 2nd argument(
tensor
orscalar
ofint
,float
,complex
orbool
) withtorch
or the 1st argument(tensor
orscalar
ofint
,float
,complex
orbool
) isother
(Required). - There is
rounding_mode
argument(str
) (Optional-Default:None
) withtorch
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
orbool
cannot be used with"trunc"
or"floor"
. - Setting
0
(int
) toother
getsZeroDivisionError
with"trunc"
or"floor"
.
-
-
divide() is the alias of
div()
. -
true_divide() is the alias of
div()
withrounding_mode=None
. -
floor_divide() is the same as
div()
withrounding_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)
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
orscalar
ofint
orfloat
) withtorch
or using a tensor(tensor
ofint
orfloat
) isinput
(Required). *You must use a scalar withoutinput=
. - The 2nd argument(
tensor
orscalar
ofint
orfloat
) withtorch
or the 1st argument(tensor
orscalar
ofint
orfloat
) isother
(Required). - Setting
0
(int
) toother
getsZeroDivisionError
. - 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])
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
ofint
orfloat
) withtorch
or using a tensor(tensor
ofint
orfloat
) isinput
(Required). - The 2nd argument(
tensor
orscalar
ofint
orfloat
) withtorch
or the 1st argument(tensor
orscalar
ofint
orfloat
) isother
(Required). - Setting
0
(int
) toother
getsZeroDivisionError
. - 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])
Top comments (0)