How to Multiply Matrices in PyTorch with torch.matmul
Matrix multiplication is the calculation a neural network spends most of its time on: every layer multiplies its input by a matrix of weights. PyTorch does it with torch.matmul, or the shorter @. This guide multiplies two small matrices both ways in PyTorch 2.15.0, and compares them with *.
Syntax
torch.matmul(a, b)
a @ b
| Part | What it means |
|---|---|
a, b | The two tensors to multiply. For two tables (matrices), the number of columns of a must equal the number of rows of b |
@ | Python's matrix multiplication operator. On tensors it gives the same result as torch.matmul |
PyTorch's reference calls it the "Matrix product of two tensors", and says: "If both arguments are 2-dimensional, the matrix-matrix product is returned."
Each value of the result is one row of a multiplied by one column of b, value by value, then added up. For two-by-two matrices, the top-left value is a's first row times b's first column.
The Example
A script multiplies two matrices of two rows and two columns with @ and with torch.matmul, then with * to compare.
Prerequisites
- A uv project with PyTorch and NumPy.
uv add torch numpyadds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.
Step 1: Write the Script
import torch
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
print(a @ b)
print(torch.matmul(a, b))
print(a * b)

a and b are two-by-two tables of whole numbers. The script prints the matrix product twice, once per way of writing it, and the result of * last.
Step 2: Run It
uv run python matmul.py
tensor([[19, 22],
[43, 50]])
tensor([[19, 22],
[43, 50]])
tensor([[ 5, 12],
[21, 32]])

@ and torch.matmul gave the same matrix. Its top-left 19 is a's first row times b's first column: 1 × 5 + 2 × 7. The 22 next to it is the same row times b's second column: 1 × 6 + 2 × 8.
* is a different calculation: it multiplies the values in the same places, 1 × 5, 2 × 6, 3 × 7 and 4 × 8, which gives 5, 12, 21, 32. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Shapes that don't fit, such as two-by-three times two-by-three | RuntimeError: mm: mat1 and mat2 shapes cannot be multiplied (2x3 and 2x3) | Make the columns of the first match the rows of the second, for example by transposing the second |
| Whole numbers times decimals | RuntimeError: expected m1 and m2 to have the same dtype, but got: long long != float | Convert one first, such as a.to(torch.float32) |
* where you meant a matrix product | a * b multiplied value by value: 5, 12, 21, 32 | a @ b |
FAQ
Does the order matter?
Yes. b @ a printed tensor([[23, 34], [31, 46]]), not the 19, 22, 43, 50 of a @ b.
Does it work on the GPU?
Yes, with both tensors on the GPU. Made on the M1 Pro's GPU, a @ b gave the same 19, 22, 43, 50, with device='mps:0'. With only a on the GPU, it failed with RuntimeError: Tensor for argument #2 'mat2' is on CPU, but expected it to be on GPU (while checking arguments for mm), which this fix solves.
Conclusion
a @ b and torch.matmul(a, b) both gave the matrix product 19, 22, 43, 50, each value a row of a times a column of b, while a * b multiplied value by value.
Additional Resources
- torch.matmul — PyTorch's reference, including vectors and batches of matrices.
- Tensors tutorial — PyTorch's introduction to tensors and their arithmetic.
- PEP 465 — the Python proposal that added the
@operator.
