mv(), mm() and bmm() in PyTorch
Tags: pytorch, mv, mm, bmm
Published: 2024-05-10 22:43:04

*[My post](https://dev.to/hyperkai/matmul-and-dot-in-pytorch-4e8p) explains [matmul()](https://pytorch.org/docs/stable/generated/torch.matmul.html) and [dot()](https://pytorch.org/docs/stable/generated/torch.dot.html).

[mv()](https://pytorch.org/docs/stable/generated/torch.mv.html) 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](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(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](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
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()](https://pytorch.org/docs/stable/generated/torch.mm.html) 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 with `torch` or using a tensor is `input`(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](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
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()](https://pytorch.org/docs/stable/generated/torch.bmm.html) 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 with `torch` or using a tensor is `input`(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](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
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))
```