*My post explains matmul() and dot().
mv() can do matrix-vector multiplication with the 2D and 1D tensor of zero or more elements, getting the 1D tensor of one or more elements:
*Memos:
-
mv()can be used with torch or a tensor. - The 1st argument(
input) withtorchor using a tensor(Required-Type:tensorofint,floatorcomplex). *It must be a 2D tensor. - The 2nd argument with
torchor the 1st argument with a tensor isvec(Required-Type:tensorofint,floatorcomplex). *It must be a 1D tensor. - There is
outargument withtorch(Optional-Default:None-Type:tensor): *Memos:-
out=must be used. -
My post explains
outargument.
-
import torch
tensor1 = torch.tensor([[2, -5, 4], [-9, 0, 6]])
tensor2 = torch.tensor([3, 6, -1])
torch.mv(input=tensor1, vec=tensor2)
tensor1.mv(vec=tensor2)
# tensor([-28, -33])
tensor1 = torch.tensor([[2., -5., 4.], [-9., 0., 6.]])
tensor2 = torch.tensor([3., 6., -1.])
torch.mv(input=tensor1, vec=tensor2)
# tensor([-28., -33.])
tensor1 = torch.tensor([[2.+0.j, -5.+0.j, 4.+0.j],
[-9.+0.j, 0.+0.j, 6.+0.j]])
tensor2 = torch.tensor([3.+0.j, 6.+0.j, -1.+0.j])
torch.mv(input=tensor1, vec=tensor2)
# tensor([-28.+0.j, -33.+0.j])
tensor1 = torch.tensor([[]])
tensor2 = torch.tensor([])
torch.mv(input=tensor1, vec=tensor2)
# tensor([0.])
mm() can do matrix multiplication with two of the 2D tensor of one or more elements and the 2D tensor of zero or more elements, getting the 2D tensor of zero or more elements:
*Memos:
-
mm()can be used withtorchor a tensor. - The 1st argument(
input) withtorchor using a tensor(Required-Type:tensorofint,floatorcomplex). *It must be a 2D tesnor. - The 2nd argument with
torchor the 1st argument with a tensor ismat2(Required-Type:tensorofint,floatorcomplex). *It must be a 2D tesnor. - There is
outargument withtorch(Optional-Default:None-Type:tensor): *Memos:-
out=must be used. -
My post explains
outargument.
-
import torch
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.mm(input=tensor1, mat2=tensor2)
tensor1.mm(mat2=tensor2)
# tensor([[18, 0, -53, 48],
# [-69, -72, -15, -51]])
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.mm(input=tensor1, mat2=tensor2)
# tensor([[18., 0., -53., 48.],
# [-69., -72., -15., -51.]])
tensor1 = torch.tensor([[2.+0.j, -5.+0.j, 4.+0.j],
[-9.+0.j, 0.+0.j, 6.+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]])
torch.mm(input=tensor1, mat2=tensor2)
# tensor([[18.+0.j, 0.+0.j, -53.+0.j, 48.+0.j],
# [-69.+0.j, -72.+0.j, -15.+0.j, -51.+0.j]])
tensor1 = torch.tensor([[0.]])
tensor2 = torch.tensor([[]])
torch.mm(input=tensor1, mat2=tensor2)
# tensor([], size=(1, 0))
bmm() can do matrix multiplication with two of the 3D tensor of one or more elements and the 3D tensor of zero or more elements, getting the 3D tensor of zero or more elements:
*Memos:
-
bmm()can be used withtorchor a tensor. - The 1st argument(
input) withtorchor using a tensor(Required-Type:tensorofint,floatorcomplex). *It must be a 3D tesnor. - The 2nd argument with
torchor the 1st argument with a tensor ismat2(Required-Type:tensorofint,floatorcomplex). *It must be a 3D tesnor. - There is
outargument withtorch(Optional-Default:None-Type:tensor): *Memos:-
out=must be used. - [My post](https://dev.to/hyperkai/set-out-argument-pytorch-4hj explains
outargument.
-
import torch
tensor1 = torch.tensor([[[2, -5]], [[-9, 0]]])
tensor2 = torch.tensor([[[3, 6], [-8, 0]],
[[-7, 3], [-4, 5]]])
torch.bmm(input=tensor1, mat2=tensor2)
tensor1.bmm(mat2=tensor2)
# tensor([[[46, 12]],
# [[63, -27]]])
tensor1 = torch.tensor([[[2., -5.]], [[-9., 0.]]])
tensor2 = torch.tensor([[[3., 6.], [-8., 0.]],
[[-7., 3.], [-4., 5.]]])
torch.bmm(input=tensor1, mat2=tensor2)
# tensor([[[46., 12.]],
# [[63., -27.]]])
tensor1 = torch.tensor([[[2.+0.j, -5.+0.j]], [[-9.+0.j, 0.+0.j]]])
tensor2 = torch.tensor([[[3.+0.j, 6.+0.j], [-8.+0.j, 0.+0.j]],
[[-7.+0.j, 3.+0.j], [-4.+0.j, 5.+0.j]]])
torch.bmm(input=tensor1, mat2=tensor2)
# tensor([[[46.+0.j, 12.+0.j]],
# [[63.+0.j, -27.+0.j]]])
tensor1 = torch.tensor([[[0.]]])
tensor2 = torch.tensor([[[]]])
torch.bmm(input=tensor1, mat2=tensor2)
# tensor([], size=(1, 1, 0))
Top comments (0)