matmul() and dot() in PyTorch
Tags: pytorch, matmul, dot, function
Published: 2024-05-09 13:46:01

*[My post](https://dev.to/hyperkai/mv-mm-and-bmm-in-pytorch-57nh) explains [mv()](https://pytorch.org/docs/stable/generated/torch.mv.html), [mm()](https://pytorch.org/docs/stable/generated/torch.mm.html) and [bmm()](https://pytorch.org/docs/stable/generated/torch.bmm.html).

[matmul()](https://pytorch.org/docs/stable/generated/torch.matmul.html) can do dot, matrix-vector or matrix multiplication with two of the 1D or more D tensors of zero or more elements, getting the 0D or more D tensor of one or more elements:

*Memos:
- `matmul()` 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 1D or more D tensor.
- The 2nd argument with `torch` or the 1st argument with a tensor is `other`(Required-Type:`tensor` of `int`, `float` or `complex`). *It must be a 1D or more D 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.
- The combination of a 1D tensor(`input`) and a 1D tensor(`other`) is done by dot multiplication.
- The combination of a 2D or more D tensor(`input`) and a 1D tensor(`other`) is done by matrix-vector multiplication.

```python
import torch

# Dot multiplication

tensor1 = torch.tensor([2, -5, 4])
tensor2 = torch.tensor([3, 6, -1])

torch.matmul(input=tensor1, other=tensor2)
tensor1.matmul(other=tensor2)
# tensor(-28)

# Matrix-vector multiplication

tensor1 = torch.tensor([[2, -5, 4], [-9, 0, 6]])
tensor2 = torch.tensor([3, 6, -1])

torch.matmul(tensor1, tensor2)
# tensor([-28, -33])

# Matrix multiplication

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.matmul(tensor1, tensor2)
# tensor([[18, 0, -53,  48],
#         [-69, -72, -15, -51]])

tensor1 = torch.tensor([2, -5, 4])
tensor2 = torch.tensor([[3, 6, -1, 9],
                        [-8, 0, 7, -2],
                        [-7, -3, -4, 5]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([18, 0, -53, 48])

tensor1 = torch.tensor([2, -5])
tensor2 = torch.tensor([[[3, 6, -1, 9],
                         [-8, 0, 7, -2]],
                        [[-7, -3, -4, 5],
                         [-9, 4, -6, 0]]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[46, 12, -37, 28],
#         [31, -26, 22, 10]])

tensor1 = torch.tensor([[2, -5], [4, 3]])
tensor2 = torch.tensor([[[3, 6, -1, 9],
                         [-8, 0, 7, -2]],
                        [[-7, -3, -4, 5],
                         [-9, 4, -6, 0]]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[[46, 12, -37, 28],
#          [-12, 24, 17, 30]],
#         [[31, -26, 22, 10],
#          [-55, 0, -34, 20]]])

tensor1 = torch.tensor([[2., -5.], [4., 3.]])
tensor2 = torch.tensor([[[3., 6., -1., 9.],
                         [-8., 0., 7., -2.]],
                        [[-7., -3., -4., 5.],
                         [-9., 4., -6., 0.]]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[[46., 12., -37., 28.],
#          [-12., 24., 17., 30.]],
#         [[31., -26., 22., 10.],
#          [-55., 0., -34., 20.]]])

tensor1 = torch.tensor([[2.+0.j, -5.+0.j], [4.+0.j, 3.+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],
                         [-9.+0.j, 4.+0.j, -6.+0.j, 0.+0.j]]])
torch.matmul(input=tensor1, other=tensor2)
# tensor([[[46.+0.j, 12.+0.j, -37.+0.j, 28.+0.j],
#          [-12.+0.j, 24.+0.j, 17.+0.j, 30.+0.j]],
#         [[31.+0.j, -26.+0.j, 22.+0.j, 10.+0.j],
#          [-55.+0.j, 0.+0.j, -34.+0.j, 20.+0.j]]])

tensor1 = torch.tensor([])
tensor2 = torch.tensor([])

torch.matmul(input=tensor1, other=tensor2)
# tensor(0.)
```

[dot()](https://pytorch.org/docs/stable/generated/torch.dot.html) can do dot multiplication with two of the 1D tensors of zero or more elements, getting the 0D tensor of one element:

*Memos:
- `dot()` 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 1D tesnor.
- The 2nd argument with `torch` or the 1st argument with a tensor is `tensor`(Required-Type:`tensor` of `int`, `float` or `complex`). *It must be a 1D 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])
tensor2 = torch.tensor([3, 6, -1])

torch.dot(input=tensor1, tensor=tensor2)
tensor1.dot(tensor=tensor2)
# tensor(-28)

tensor1 = torch.tensor([2., -5., 4.])
tensor2 = torch.tensor([3., 6., -1.])

torch.dot(input=tensor1, tensor=tensor2)
# tensor(-28.)

tensor1 = torch.tensor([2.+0.j, -5.+0.j, 4.+0.j])
tensor2 = torch.tensor([3.+0.j, 6.+0.j, -1.+0.j])

torch.dot(input=tensor1, tensor=tensor2)
# tensor(-28.+0.j)

tensor1 = torch.tensor([])
tensor2 = torch.tensor([])

torch.dot(input=tensor1, tensor=tensor2)
# tensor(0.)
```