DEV Community

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

Posted on

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

*My post explains matmul() and dot().

mv() can do matrix-vector multiplication with a 2D tensor and 1D tensor:

*Memos:

  • mv() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float or complex) with torch or using a tensor(tensor of int, float or complex) is input(Required). *It must be a 2D tensor.
  • The 2nd argument(tensor of int, float or complex) with torch or the 1st argument(tensor of int, float or complex) is vec(Required). *It must be a 1D tensor.
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])

Enter fullscreen mode Exit fullscreen mode

mm() can do matrix multiplication with two of 2D tensors:

*Memos:

  • mm() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float or complex) with torch or using a tensor(tensor of int, float or complex) is input(Required). *It must be a 2D tesnor.
  • The 2nd argument(tensor of int, float or complex) with torch or the 1st argument(tensor of int, float or complex) is mat2(Required). *It must be a 2D tesnor.
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)
# 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]])
Enter fullscreen mode Exit fullscreen mode

bmm() can do matrix multiplication with two of 3D tensors:

*Memos:

  • bmm() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float or complex) with torch or using a tensor(tensor of int, float or complex) is input(Required). *It must be a 3D tesnor.
  • The 2nd argument(tensor of int, float or complex) with torch or the 1st argument(tensor of int, float or complex) is mat2(Required). *It must be a 3D tesnor.
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]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)