DEV Community

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

Posted on • Edited 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-Default:None-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-Default:None-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-Default:None-Type:tensor): *Memos:
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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay