DEV Community

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

Posted on • Updated on

add(), sub() and mul() in PyTorch

*My post explains div(), remainder() and fmod().

add() can do addition with 0D or more D tensors or scalars as shown below:

*Memos:

  • add() 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).
  • The 3rd argument(tensor or scalar of int, float, complex or bool) with torch or the 2nd argument(tensor or scalar of int, float, complex or bool) is alpha(Optional-Default:1). *otheris multiplied by alpha(input+(otherxalpha)).
import torch

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

torch.add(input=tensor1, other=tensor2)
tensor1.add(other=tensor2)
torch.add(input=tensor1, other=tensor2, alpha=1)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(1))
torch.add(input=9, other=tensor2)
# tensor([[13, 5, 12], [7, 14, 4]])

torch.add(input=tensor1, other=tensor2, alpha=0)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(0))
# tensor([[9, 9, 9], [9, 9, 9]])

torch.add(input=tensor1, other=tensor2, alpha=2)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(2))
# tensor([[17, 1, 15], [5, 19, -1]])

torch.add(input=tensor1, other=tensor2, alpha=-1)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(-1))
# tensor([[5, 13, 6], [11, 4, 14]])

torch.add(input=tensor1, other=tensor2, alpha=-2)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(-2))
# tensor([[1, 17, 3], [13, -1, 19]])

torch.add(input=tensor1, other=4)
torch.add(input=tensor1, other=4, alpha=1)
torch.add(input=tensor1, other=4, alpha=torch.tensor(1))
torch.add(input=9, other=4)
torch.add(input=9, other=4, alpha=1)
torch.add(input=9, other=4, alpha=torch.tensor(1))
# tensor(13)

torch.add(input=tensor1, other=4, alpha=0)
torch.add(input=tensor1, other=4, alpha=torch.tensor(0))
torch.add(input=9, other=4, alpha=0)
torch.add(input=9, other=4, alpha=torch.tensor(0))
# tensor(9)

torch.add(input=tensor1, other=4, alpha=2)
torch.add(input=tensor1, other=4, alpha=torch.tensor(2))
torch.add(input=9, other=4, alpha=2)
torch.add(input=9, other=4, alpha=torch.tensor(2))
# tensor(17)

torch.add(input=tensor1, other=4, alpha=-1)
torch.add(input=tensor1, other=4, alpha=torch.tensor(-1))
torch.add(input=9, other=4, alpha=-1)
torch.add(input=9, other=4, alpha=torch.tensor(-1))
# tensor(5)

torch.add(input=tensor1, other=4, alpha=-2)
torch.add(input=tensor1, other=4, alpha=torch.tensor(-2))
# tensor(-1)

torch.add(input=9, other=4, alpha=-2)
torch.add(input=9, other=4, alpha=torch.tensor(-2))
# tensor(1)

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[[4, -4, 3], [-2, 5, -5]],
                        [[6, 2, 8], [3, 9, 1]]])
torch.add(input=tensor1, other=tensor2)
torch.add(input=tensor1, other=tensor2, alpha=1)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(1))
# tensor([[[13, 3, 9], [7, 12, 1]],
#         [[15, 9, 14], [12, 16, 7]]])

tensor1 = torch.tensor([9., 7., 6.])
tensor2 = torch.tensor([[[4., -4., 3.], [-2., 5., -5.]],
                        [[6., 2., 8.], [3., 9., 1.]]])
torch.add(input=tensor1, other=tensor2)
torch.add(input=tensor1, other=tensor2, alpha=1.)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(1.))
# tensor([[[13., 3., 9.], [7., 12., 1.]],
#         [[15., 9., 14.], [12., 16., 7.]]])

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.add(input=tensor1, other=tensor2)
torch.add(input=tensor1, other=tensor2, alpha=1.+0.j)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(1.+0.j))
# tensor([[[13.+0.j, 3.+0.j, 9.+0.j],
#          [7.+0.j, 12.+0.j, 1.+0.j]],
#         [[15.+0.j, 9.+0.j, 14.+0.j],
#          [12.+0.j, 16.+0.j, 7.+0.j]]])

tensor1 = torch.tensor([True, False, True])
tensor2 = torch.tensor([[[True, False, True], [True, False, True]],
                        [[False, True, False], [False, True, False]]])
torch.add(input=tensor1, other=tensor2)
torch.add(input=tensor1, other=tensor2, alpha=True)
torch.add(input=tensor1, other=tensor2, alpha=torch.tensor(True))
# tensor([[[True, False, True], [True, False, True]],
#         [[True, True, True], [True, True, True]]])

torch.add(input=9, other=4)
torch.add(input=9, other=4, alpha=1)
torch.add(input=9, other=4, alpha=torch.tensor(1))
# tensor(13)

torch.add(input=9., other=4.)
torch.add(input=9., other=4., alpha=1.)
torch.add(input=9., other=4., alpha=torch.tensor(1.))
# tensor(13.)

torch.add(input=9+0j, other=4+0j)
torch.add(input=9+0j, other=4+0j, alpha=1+0j)
torch.add(input=9+0j, other=4+0j, alpha=torch.tensor(1+0j))
# tensor(13.+0.j)

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

sub() can do subtraction with 0D or more D tensors or scalars as shown below:

*Memos:

  • sub() can be used with torch or a tensor.
  • The 1st argument(tensor or scalar of int, float or complex) with torch or using a tensor(tensor of int, float or complex) is input(Required).
  • The 2nd argument(tensor or scalar of int, float or complex) with torch or the 1st argument(tensor or scalar of int, float or complex) is other(Required).
  • The 3rd argument(tensor or scalar of int, float or complex) with torch or the 2nd argument(tensor or scalar of int, float or complex) is alpha(Optional-Default:1). *otheris multiplied by alpha(input-(otherxalpha)).
  • subtract() is the alias of sub().
import torch

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

torch.sub(input=tensor1, other=tensor2)
tensor1.sub(other=tensor2)
torch.sub(input=tensor1, other=tensor2, alpha=1)
torch.sub(input=tensor1, other=tensor2, alpha=torch.tensor(1))
torch.sub(input=9, other=tensor2)
# tensor([[5, 13, 6], [11, 4, 14]])

torch.sub(input=tensor1, other=tensor2, alpha=0)
torch.sub(input=tensor1, other=tensor2, alpha=torch.tensor(0))
# tensor([[9, 9, 9], [9, 9, 9]])

torch.sub(input=tensor1, other=tensor2, alpha=2)
torch.sub(input=tensor1, other=tensor2, alpha=torch.tensor(2))
# tensor([[1, 17, 3], [13, -1, 19]])

torch.sub(input=tensor1, other=tensor2, alpha=-1)
torch.sub(input=tensor1, other=tensor2, alpha=torch.tensor(-1))
# tensor([[13, 5, 12], [7, 14, 4]])

torch.sub(input=tensor1, other=tensor2, alpha=-2)
torch.sub(input=tensor1, other=tensor2, alpha=torch.tensor(-2))
# tensor([[17, 1, 15], [5, 19, -1]])

torch.sub(input=tensor1, other=4)
torch.sub(input=tensor1, other=4, alpha=1)
torch.sub(input=tensor1, other=4, alpha=torch.tensor(1))
torch.sub(input=9, other=4)
torch.sub(input=9, other=4, alpha=1)
torch.sub(input=9, other=4, alpha=torch.tensor(1))
# tensor(5)

torch.sub(input=tensor1, other=4, alpha=0)
torch.sub(input=tensor1, other=4, alpha=torch.tensor(0))
torch.sub(input=9, other=4, alpha=0)
torch.sub(input=9, other=4, alpha=torch.tensor(0))
# tensor(6)

torch.sub(input=tensor1, other=4, alpha=2)
torch.sub(input=tensor1, other=4, alpha=torch.tensor(2))
torch.sub(input=9, other=4, alpha=2)
torch.sub(input=9, other=4, alpha=torch.tensor(2))
# tensor(1)

torch.sub(input=tensor1, other=4, alpha=-1)
torch.sub(input=tensor1, other=4, alpha=torch.tensor(-1))
torch.sub(input=9, other=4, alpha=-1)
torch.sub(input=9, other=4, alpha=torch.tensor(-1))
# tensor(13)

torch.sub(input=tensor1, other=4, alpha=-2)
torch.sub(input=tensor1, other=4, alpha=torch.tensor(-2))
torch.sub(input=9, other=4, alpha=-2)
torch.sub(input=9, other=4, alpha=torch.tensor(-2))
# tensor(17)

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

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

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.sub(input=tensor1, other=tensor2)
torch.sub(input=tensor1, other=tensor2, alpha=1.+0.j)
torch.sub(input=tensor1, other=tensor2, alpha=torch.tensor(1.+0.j))
# tensor([[[5.+0.j, 11.+0.j, 3.+0.j], [11.+0.j, 2.+0.j, 11.+0.j]],
#         [[3.+0.j, 5.+0.j, -2.+0.j], [6.+0.j, -2.+0.j, 5.+0.j]]])

torch.sub(input=9, other=4)
torch.sub(input=9, other=4, alpha=1)
torch.sub(input=9, other=4, alpha=torch.tensor(1))
# tensor(5)

torch.sub(input=9., other=4.)
torch.sub(input=9., other=4., alpha=1.)
torch.sub(input=9., other=4., alpha=torch.tensor(1.))
# tensor(5.)

torch.sub(input=9+0j, other=4+0j)
torch.sub(input=9+0j, other=4+0j, alpha=1+0j)
torch.sub(input=9+0j, other=4+0j, alpha=torch.tensor(1+0j))
# tensor(5.+0.j)
Enter fullscreen mode Exit fullscreen mode

mul() can do multiplication with 0D or more D tensors or scalars as shown below:

*Memos:

  • mul() 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).
  • multiply() is the alias of mul().
import torch

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

torch.mul(input=tensor1, other=tensor2)
tensor1.mul(other=tensor2)
torch.mul(input=9, other=tensor2)
# tensor([[36, -36, 27], [-18, 45, -45]])

torch.mul(input=tensor1, other=4)
torch.mul(input=9, other=4)
# tensor(36)

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[[4, -4, 3], [-2, 5, -5]],
                        [[6, 2, 8], [3, 9, 1]]])
torch.mul(input=tensor1, other=tensor2)
# tensor([[[36, -28, 18], [-18, 35, -30]],
#         [[54, 14, 48], [27, 63, 6]]])

tensor1 = torch.tensor([9., 7., 6.])
tensor2 = torch.tensor([[[4., -4., 3.], [-2., 5., -5.]],
                        [[6., 2., 8.], [3., 9., 1.]]])
torch.mul(input=tensor1, other=tensor2)
# tensor([[[36., -28., 18.], [-18., 35., -30.]],
#         [[54., 14., 48.], [27., 63., 6.]]])

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.mul(input=tensor1, other=tensor2)
# tensor([[[36.+0.j, -28.+0.j, 18.+0.j],
#          [-18.+0.j, 35.+0.j, -30.+0.j]],
#         [[54.+0.j, 14.+0.j, 48.+0.j],
#          [27.+0.j, 63.+0.j, 6.+0.j]]])

tensor1 = torch.tensor([True, False, True])
tensor2 = torch.tensor([[[True, False, True], [True, False, True]],
                        [[False, True, False], [False, True, False]]])
torch.mul(input=tensor1, other=tensor2)
# tensor([[[True, False, True], [True, False, True]],
#         [[False, False, False], [False, False, False]]])

torch.mul(input=9, other=4)
# tensor(36)

torch.mul(input=9., other=4.)
# tensor(36.)

torch.mul(input=9.+0.j, other=4.+0.j)
# tensor(36.+0.j)

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

Top comments (0)