DEV Community

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

Posted on • Edited on

1

matmul and dot in PyTorch

Buy Me a Coffee

*My post explains mv(), mm() and bmm().

matmul() can do dot, matrix-vector or matrix multiplication with two of the 1D or more D tensors of zero or more elements, getting the 0D or more D tensor of one or more elements:

*Memos:

  • matmul() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or complex). *It must be a 1D or more D tensor.
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor of int, float or complex). *It must be a 1D or more D tensor.
  • There is out argument with torch(Optional-Defaual:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • The combination of a 1D tensor(input or a tensor) and a 1D tensor(other) is done by dot multiplication.
  • The combination of a 2D or more D tensor(input or a tensor) and a 1D tensor(other) is done by matrix-vector multiplication.
import torch

# Dot multiplication

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

torch.matmul(input=tensor1, other=tensor2)
tensor1.matmul(other=tensor2)
# tensor(-28)

# Matrix-vector multiplication

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

torch.matmul(input=tensor1, other=tensor2)
# tensor([-28, -33])

# Matrix multiplication

tensor1 = torch.tensor([[2, -5, 4], [-9, 0, 6]])
tensor2 = torch.tensor([[3, 6, -1, 9],
                        [-8, 0, 7, -2],
                        [-7, -3, -4, 5]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[18, 0, -53,  48],
#         [-69, -72, -15, -51]])

tensor1 = torch.tensor([2, -5, 4])
tensor2 = torch.tensor([[3, 6, -1, 9],
                        [-8, 0, 7, -2],
                        [-7, -3, -4, 5]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([18, 0, -53, 48])

tensor1 = torch.tensor([2, -5])
tensor2 = torch.tensor([[[3, 6, -1, 9],
                         [-8, 0, 7, -2]],
                        [[-7, -3, -4, 5],
                         [-9, 4, -6, 0]]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[46, 12, -37, 28],
#         [31, -26, 22, 10]])

tensor1 = torch.tensor([[2, -5], [4, 3]])
tensor2 = torch.tensor([[[3, 6, -1, 9],
                         [-8, 0, 7, -2]],
                        [[-7, -3, -4, 5],
                         [-9, 4, -6, 0]]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[[46, 12, -37, 28],
#          [-12, 24, 17, 30]],
#         [[31, -26, 22, 10],
#          [-55, 0, -34, 20]]])

tensor1 = torch.tensor([[2., -5.], [4., 3.]])
tensor2 = torch.tensor([[[3., 6., -1., 9.],
                         [-8., 0., 7., -2.]],
                        [[-7., -3., -4., 5.],
                         [-9., 4., -6., 0.]]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[[46., 12., -37., 28.],
#          [-12., 24., 17., 30.]],
#         [[31., -26., 22., 10.],
#          [-55., 0., -34., 20.]]])

tensor1 = torch.tensor([[2.+0.j, -5.+0.j], [4.+0.j, 3.+0.j]])
tensor2 = torch.tensor([[[3.+0.j, 6.+0.j, -1.+0.j, 9.+0.j],
                         [-8.+0.j, 0.+0.j, 7.+0.j, -2.+0.j]],
                        [[-7.+0.j, -3.+0.j, -4.+0.j, 5.+0.j],
                         [-9.+0.j, 4.+0.j, -6.+0.j, 0.+0.j]]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[[46.+0.j, 12.+0.j, -37.+0.j, 28.+0.j],
#          [-12.+0.j, 24.+0.j, 17.+0.j, 30.+0.j]],
#         [[31.+0.j, -26.+0.j, 22.+0.j, 10.+0.j],
#          [-55.+0.j, 0.+0.j, -34.+0.j, 20.+0.j]]])

tensor1 = torch.tensor([])
tensor2 = torch.tensor([])

torch.matmul(input=tensor1, other=tensor2)
# tensor(0.)
Enter fullscreen mode Exit fullscreen mode

dot() can do dot multiplication with two of the 1D tensors of zero or more elements, getting the 0D tensor of one element:

*Memos:

  • dot() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or complex). *It must be a 1D tesnor.
  • The 2nd argument with torch or the 1st argument with a tensor is tensor(Required-Type:tensor of int, float or complex). *It must be a 1D tesnor.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

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

torch.dot(input=tensor1, tensor=tensor2)
tensor1.dot(tensor=tensor2)
# tensor(-28)

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

torch.dot(input=tensor1, tensor=tensor2)
# tensor(-28.)

tensor1 = torch.tensor([2.+0.j, -5.+0.j, 4.+0.j])
tensor2 = torch.tensor([3.+0.j, 6.+0.j, -1.+0.j])

torch.dot(input=tensor1, tensor=tensor2)
# tensor(-28.+0.j)

tensor1 = torch.tensor([])
tensor2 = torch.tensor([])

torch.dot(input=tensor1, tensor=tensor2)
# tensor(0.)
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more