ValueError Shape must be rank 2 but is rank 3 for 'MatMul'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This error means a matrix multiplication op expected a rank-2 tensor, but one of the inputs had three dimensions instead. In practice, that usually happens when a batch dimension or sequence dimension is still present and the code is calling a plain matrix multiply path that expects a single matrix rather than a stack of matrices.
Read the Shapes First
A rank-2 tensor has shape like (rows, cols). A rank-3 tensor has shape like (batch, rows, cols) or (batch, time, features).
For example:
- rank 2:
(32, 128) - rank 3:
(16, 32, 128)
If an operation named MatMul expects rank 2, then a shape such as (16, 32, 128) is not acceptable until you either reshape it or use a batched matrix multiplication API.
The Two Usual Fixes
The right fix depends on what you meant mathematically.
If you wanted one big 2D matrix, reshape the tensor first.
If you actually wanted batched matrix multiplication, keep the batch dimension and use an API that supports it.
In that second case, TensorFlow performs matrix multiplication for each batch element separately.
Common Real-World Sources of the Error
One frequent source is flattening image or sequence data incorrectly before a dense layer. Suppose you have a tensor shaped (batch, height, width). If you want a dense layer that consumes one feature vector per example, flatten the last dimensions first.
Another source is mixing low-level ops with Keras layers. Keras Dense can handle extra leading dimensions in many cases because it applies the weight matrix on the last axis, but a raw rank-2 MatMul path may not.
Debugging Shape Problems Quickly
Print tensor shapes before the failing line. That is often faster than guessing.
In graph-heavy code, explicit assertions help too:
If those assertions fail, you know the mismatch is happening before the multiply, not inside it.
Choose the Fix That Matches the Math
Do not reshape blindly just to silence the error. Reshaping changes how values are grouped, so it is only correct if collapsing dimensions matches your intended computation.
A useful rule is:
- use
reshapewhen you want to convert structured features into one flat vector per example - use
tf.linalg.matmulwhen you want matrix multiplication across batches - use
einsumonly when the multiplication pattern is more complex than the usual APIs express cleanly
Common Pitfalls
The most common mistake is flattening the wrong dimensions. For example, collapsing the batch axis into the feature axis changes the meaning of the data and usually breaks training.
Another mistake is assuming every TensorFlow multiplication op supports batched inputs in the same way. Some higher-level APIs do, while lower-level MatMul paths may still expect rank 2.
It is also easy to fix the error with a reshape that makes the code run but makes the model mathematically wrong. Always check what the intended dimensions represent.
Summary
- The error means a rank-2 matrix multiply received a rank-3 tensor.
- First inspect the tensor shapes instead of guessing.
- Use
reshapeif you need one 2D matrix per example batch. - Use
tf.linalg.matmulfor batched matrix multiplication. - Match the fix to the intended mathematics, not just to the error message.

