DEV Community

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

Posted on • Updated on

Functions and operators for Dot and Matrix multiplication and Element-wise calculation in PyTorch

*My post explains Dot, Matrix and Element-wise multiplication in PyTorch.

<Dot multiplication>

  • dot() can multiply 1D tensors:
import torch

tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor

torch.dot(tensor1, tensor2) # tensor(53)
tensor1.dot(tensor2) # tensor(53)
Enter fullscreen mode Exit fullscreen mode
  • @ or matmul() can multiply 1D or more D tensors:
import torch

tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor

tensor1 @ tensor2 # tensor(53)

torch.matmul(tensor1, tensor2) # tensor(53)
tensor1.matmul(tensor2) # tensor(53)
Enter fullscreen mode Exit fullscreen mode

<Matrix multiplication>

  • mv() can multiply a 2D and 1D tensor:
import torch

tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]]) # 2D tensor
tensor2 = torch.tensor([5, 0, 8]) # 1D tensor

torch.mv(tensor1, tensor2) # tensor([42, 56])
tensor1.mv(tensor2) # tensor([42, 56])
Enter fullscreen mode Exit fullscreen mode
  • mm() can multiply 2D tensors:
import torch

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

torch.mm(tensor1, tensor2)
# tensor([[35, 58, 59, 69], [51, 26, 85, 73]])
tensor1.mm(tensor2)
# tensor([[35, 58, 59, 69], [51, 26, 85, 73]])
Enter fullscreen mode Exit fullscreen mode
  • bmm() can multiply 3D tensors:
import torch

tensor1 = torch.tensor([[[2, 7]], [[8, 3]]]) # 3D tensor
tensor2 = torch.tensor([[[5, 9], [3, 6]], # 3D tensor
                        [[7, 2], [1, 4]]])

torch.bmm(tensor1, tensor2) # tensor([[[31, 60]], [[59, 28]]])
tensor1.bmm(tensor2) # tensor([[[31, 60]], [[59, 28]]])
Enter fullscreen mode Exit fullscreen mode
  • @ or matmul() can multiply 1D or more D tensors by dot or matrix multiplication:
import torch

tensor1 = torch.tensor([[2, 7], [8, 3]]) # 2D tensor
tensor2 = torch.tensor([[[[5, 9], [3, 6]], [[7, 2], [1, 4]]],
                        [[[6, 0], [4, 6]], [[2, 9], [8, 1]]]])
                       # 4D tensor
tensor1 @ tensor2
# tensor([[[[31, 60], [49, 90]], [[21, 32], [59, 28]]],
#         [[[40, 42], [60, 18]], [[60, 25], [40, 75]]]])
torch.matmul(tensor1, tensor2)
# tensor([[[[31, 60], [49, 90]], [[21, 32], [59, 28]]],
#         [[[40, 42], [60, 18]], [[60, 25], [40, 75]]]])
tensor1.matmul(tensor2)
# tensor([[[[31, 60], [49, 90]], [[21, 32], [59, 28]]],
#         [[[40, 42], [60, 18]], [[60, 25], [40, 75]]]])
Enter fullscreen mode Exit fullscreen mode

<Element-wise calculation>

  • * or mul() can do multiplication with 0D or more D tensors. *mul() and multiply() are the same because multiply() is the alias of mul():
import torch

tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor

tensor1 * tensor2 # tensor([12, 21, 20])
torch.mul(tensor1, tensor2) # tensor([12, 21, 20])
tensor1.mul(tensor2) # tensor([12, 21, 20])
Enter fullscreen mode Exit fullscreen mode
  • / or div() can do division with 0D or more D tensors. *div(), divide() and true_divide() with rounding_mode=None are the same because divide() and true_divide() with rounding_mode=None are the aliases of div():
import torch

tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor

tensor1 / tensor2 # tensor([0.3333, 2.3333, 0.8000])
torch.div(tensor1, tensor2) # tensor([0.3333, 2.3333, 0.8000])
tensor1.div(tensor2) # tensor([0.3333, 2.3333, 0.8000])
Enter fullscreen mode Exit fullscreen mode
  • + or add() can do addition with 0D or more D tensors:
import torch

tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor

tensor1 + tensor2 # tensor([8, 10, 9])
torch.add(tensor1, tensor2) # tensor([8, 10, 9])
tensor1.add(tensor2) # tensor([8, 10, 9])
Enter fullscreen mode Exit fullscreen mode
  • - or sub() can do subtraction with 0D or more D tensors. *sub() and subtract() are the same because subtract() is the alias of sub():
import torch

tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor

tensor1 - tensor2 # tensor([-4, 4, -1])
torch.subtract(tensor1, tensor2) # tensor([-4, 4, -1])
tensor1.subtract(tensor2) # tensor([-4, 4, -1])
Enter fullscreen mode Exit fullscreen mode
  • % or remainder() can do modulo(mod) calculation with 0D or more D tensors:
import torch

tensor1 = torch.tensor([2, 7, 4]) # 1D tensor
tensor2 = torch.tensor([6, 3, 5]) # 1D tensor

tensor1 % tensor2 # tensor([2, 1, 4])
torch.remainder(tensor1, tensor2) # tensor([2, 1, 4])
tensor1.remainder(tensor2) # tensor([2, 1, 4])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)