DEV Community

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

Posted on • Updated on

mv(), mm() and bmm() in PyTorch

Buy Me a Coffee

*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) with torch or using a tensor(Required-Type:tensor of int, float or complex). *It must be a 2D tensor.
  • The 2nd argument with torch or the 1st argument with a tensor is vec(Required-Type:tensor of int, float or complex). *It must be a 1D tensor.
  • There is out argument with torch (Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
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.])
Enter fullscreen mode Exit fullscreen mode

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 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 2D tesnor.
  • The 2nd argument with torch or the 1st argument with a tensor is mat2(Required-Type:tensor of int, float or complex). *It must be a 2D tesnor.
  • There is out argument with torch (Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
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))
Enter fullscreen mode Exit fullscreen mode

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 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 3D tesnor.
  • The 2nd argument with torch or the 1st argument with a tensor is mat2(Required-Type:tensor of int, float or complex). *It must be a 3D tesnor.
  • There is out argument with torch (Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
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))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)