TensorFlow
ValueError
MatMul
Python
Deep Learning

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.

python
1import tensorflow as tf
2
3x = tf.random.normal((16, 8, 4))
4w = tf.random.normal((32, 10))
5
6x2 = tf.reshape(x, (16, 32))
7y = tf.matmul(x2, w)
8
9print(y.shape)

If you actually wanted batched matrix multiplication, keep the batch dimension and use an API that supports it.

python
1import tensorflow as tf
2
3x = tf.random.normal((16, 8, 4))
4w = tf.random.normal((16, 4, 6))
5
6y = tf.linalg.matmul(x, w)
7print(y.shape)

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.

python
1import tensorflow as tf
2
3x = tf.random.normal((5, 28, 28))
4x_flat = tf.reshape(x, (5, 28 * 28))
5layer = tf.keras.layers.Dense(64)
6
7y = layer(x_flat)
8print(y.shape)

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.

python
print(x.shape)
print(w.shape)

In graph-heavy code, explicit assertions help too:

python
tf.debugging.assert_rank(x, 2)
tf.debugging.assert_rank(w, 2)

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 reshape when you want to convert structured features into one flat vector per example
  • use tf.linalg.matmul when you want matrix multiplication across batches
  • use einsum only 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 reshape if you need one 2D matrix per example batch.
  • Use tf.linalg.matmul for batched matrix multiplication.
  • Match the fix to the intended mathematics, not just to the error message.

Course illustration
Course illustration

All Rights Reserved.