How to Multiply Matrices in TensorFlow with tf.matmul
Matrix multiplication is the maths inside every neural network layer: it multiplies values in pairs and adds the products up. tf.matmul, or the @ operator, does it in TensorFlow. This guide multiplies two small matrices in TensorFlow 2.22.0, on a shopping example small enough to check by hand.
Syntax
tf.matmul(<matrix a>, <matrix b>)
<matrix a> @ <matrix b>
| Part | What it means |
|---|---|
<matrix a> | A tensor of shape (m, n): m rows of n values |
<matrix b> | A tensor of shape (n, p): its number of rows must equal <matrix a>'s number of columns |
| Result | A tensor of shape (m, p). Each value is one row of a times one column of b, value by value, added up |
The tf.linalg.matmul API page says it "Multiplies matrix a by matrix b, producing a * b", and what the inputs must be: "tensors of rank >= 2 where the inner 2 dimensions specify valid matrix multiplication dimensions". About @: "In TensorFlow, it simply calls the tf.matmul() function".
The Example
Two shopping baskets and a price list. Basket 1 holds 3 apples, 1 loaf and 2 cartons of milk; basket 2 holds 2 loaves and 1 carton. An apple costs 0.50, a loaf 2.50 and a carton 1.20. Matrix multiplication gives each basket's total.
Prerequisites
- A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.
Step 1: Write the Script
import tensorflow as tf
# Two shopping baskets (rows): how many apples, loaves and cartons of milk
quantities = tf.constant([[3.0, 1.0, 2.0], [0.0, 2.0, 1.0]])
# The price of one apple, one loaf and one carton (a column)
prices = tf.constant([[0.5], [2.5], [1.2]])
print(tf.matmul(quantities, prices))
print(quantities @ prices)

quantities has the shape (2, 3) and prices (3, 1). The inner lengths match, 3 and 3, so the result will be (2, 1): one total per basket.
Step 2: Run It
uv run python shopping.py
tf.Tensor(
[[6.4]
[6.2]], shape=(2, 1), dtype=float32)
tf.Tensor(
[[6.4]
[6.2]], shape=(2, 1), dtype=float32)

Basket 1 costs 6.4: 3 × 0.50 + 1 × 2.50 + 2 × 1.20 = 1.50 + 2.50 + 2.40. Basket 2 costs 6.2: 0 × 0.50 + 2 × 2.50 + 1 × 1.20. tf.matmul and @ gave the same (2, 1) result. The command works unchanged in PowerShell.
The first run in a new environment is slow, while TensorFlow loads for the first time.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using * for matrix multiplication | quantities * prices stopped with InvalidArgumentError: … Incompatible shapes: [2,3] vs. [3,1] [Op:Mul]. * multiplies value by value | Use tf.matmul or @ |
| The prices as a row instead of a column | tf.matmul(quantities, tf.constant([[0.5, 2.5, 1.2]])) stopped with Matrix size-incompatible: In[0]: [2,3], In[1]: [1,3] [Op:MatMul]: 3 columns against 1 row | Give b one row per column of a: shape (3, 1) |
A plain list of prices, shape (3,) | ValueError: Argument `b` passed to `tf.linalg.matmul` must be at least rank 2. Received `b` with shape (3,) (rank 1). The message goes on to suggest tf.linalg.matvec | Use tf.linalg.matvec(quantities, prices), which returned [6.4 6.2] |
FAQ
Does the order of the matrices matter?
Yes. a @ b and b @ a are different calculations, and often only one of them has matching inner lengths. Here tf.matmul(prices, quantities) paired a (3, 1) with a (2, 3), inner lengths 1 and 2, and stopped with Matrix size-incompatible: In[0]: [3,1], In[1]: [2,3] [Op:MatMul].
What does this have to do with neural networks?
A Keras Dense layer does this multiplication. Its documentation says it "implements the operation: output = activation(dot(input, kernel) + bias)", where the kernel "is a weights matrix created by the layer". The inputs play the part of the quantities, and training finds the weights.
Conclusion
tf.matmul(quantities, prices) and quantities @ prices turned 2 baskets and 3 prices into 2 totals: a (2, 3) matrix times a (3, 1) gave a (2, 1). * multiplies value by value instead, and the inner lengths must always match.
Additional Resources
- tf.linalg.matmul — every argument, including transposing either matrix.
- tf.linalg.matvec — a matrix times a vector.
