*My post explains mv(), mm() and bmm().
matmul() can do dot, matrix-vector or matrix multiplication with two of 1D or more D tensors:
*Memos:
-
matmul()
can be used with torch or a tensor. - The 1st argument(
tensor
ofint
,float
orcomplex
) withtorch
or using a tensor(tensor
ofint
,float
orcomplex
) isinput
(Required). *It must be a 1D or more D tensor. - The 2nd argument(
tensor
ofint
,float
orcomplex
) withtorch
or the 1st argument(tensor
ofint
,float
orcomplex
) isother
(Required). *It must be a 1D or more D tensor. - The combination of a 1D tensor(
input
) and a 1D tensor(other
) is done by dot multiplication. - The combination of a 2D or more D tensor(
input
) 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(tensor1, 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(tensor1, 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]]])
dot() can do dot multiplication with two of 1D tensors:
*Memos:
-
dot()
can be used withtorch
or a tensor. - The 1st argument(
tensor
ofint
,float
orcomplex
) withtorch
or using a tensor(tensor
ofint
,float
orcomplex
) isinput
(Required). *It must be a 1D tesnor. - The 2nd argument(
tensor
ofint
,float
orcomplex
) withtorch
or the 1st argument(tensor
ofint
,float
orcomplex
) istensor
(Required). *It must be a 1D tesnor.
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)
Top comments (0)