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

Neon image

Serverless Postgres in 300ms (!)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (0)

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay