Keras
matrix multiplication
implementation guide
deep learning
machine learning

How to implement a matrix multiplication in Keras?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In modern Keras, matrix multiplication is usually implemented with TensorFlow operations such as tf.matmul, or with higher-level layers such as Dot when the operation is really an inner product over a known axis. The right choice depends on the tensor ranks involved and whether you want a reusable layer abstraction or just one multiplication inside a model graph.

Using tf.matmul directly

Because Keras runs on top of TensorFlow, the most direct solution is often tf.matmul.

python
1import tensorflow as tf
2
3left = tf.keras.Input(shape=(4,))
4right = tf.keras.Input(shape=(4, 3))
5
6x = tf.expand_dims(left, axis=1)
7out = tf.matmul(x, right)
8out = tf.squeeze(out, axis=1)
9
10model = tf.keras.Model(inputs=[left, right], outputs=out)
11model.summary()

This approach gives explicit control over shapes and batch behavior.

Using layers.Dot for simpler cases

If the operation is really a dot product or inner product, Dot is cleaner.

python
1import tensorflow as tf
2
3a = tf.keras.Input(shape=(4,))
4b = tf.keras.Input(shape=(4,))
5
6dot = tf.keras.layers.Dot(axes=1)([a, b])
7model = tf.keras.Model(inputs=[a, b], outputs=dot)

This is ideal for similarity scoring, embeddings, and pairwise interaction models.

A reusable custom layer

If the matrix multiplication is part of a recurring architecture pattern, wrap it in a custom layer.

python
1import tensorflow as tf
2
3class MatrixMultiply(tf.keras.layers.Layer):
4    def call(self, inputs):
5        left, right = inputs
6        return tf.matmul(left, right)
7
8left = tf.keras.Input(shape=(2, 3))
9right = tf.keras.Input(shape=(3, 4))
10out = MatrixMultiply()([left, right])
11
12model = tf.keras.Model(inputs=[left, right], outputs=out)
13model.summary()

This makes the graph easier to reuse and document.

Shape reasoning matters most

The hardest part of matrix multiplication in Keras is usually not the API call. It is getting tensor shapes to line up.

That is why debugging this kind of model code usually starts with shape inspection rather than with changing layer classes at random.

For tf.matmul, the inner dimensions must agree. If left has shape (..., m, n), then right must have shape (..., n, p).

That means debugging is often mostly about printing and verifying shapes.

python
sample_left = tf.random.normal((1, 2, 3))
sample_right = tf.random.normal((1, 3, 4))
print(tf.matmul(sample_left, sample_right).shape)

Batch matrix multiplication

tf.matmul naturally supports batched multiplication when the leading dimensions are batch dimensions.

python
1left = tf.random.normal((8, 2, 3))
2right = tf.random.normal((8, 3, 4))
3out = tf.matmul(left, right)
4print(out.shape)

This is useful in attention mechanisms, learned projections, and custom sequence operations.

When a Dense layer is enough

Sometimes people ask for “matrix multiplication in Keras” when what they really need is a learned linear transformation. In that case, a Dense layer may already be the right abstraction.

This is a good example of why tensor shape intent matters more than mechanically choosing the lowest-level operation available.

python
layer = tf.keras.layers.Dense(16)
output = layer(tf.keras.Input(shape=(8,)))

Internally, that layer performs matrix multiplication plus bias and optional activation. So the right answer depends on whether you need explicit tensor-tensor multiplication or just a standard trainable projection.

Common Pitfalls

A common mistake is focusing on the multiplication function while ignoring shape compatibility.

Another mistake is using Dot when the operation is a full matrix multiplication rather than a reduction over one axis.

A third mistake is writing a custom layer when a built-in layer such as Dense or Dot already expresses the intent more clearly.

Summary

  • Use tf.matmul for explicit matrix multiplication in Keras/TensorFlow code.
  • Use Dot when the operation is really a dot product over a chosen axis.
  • Wrap the logic in a custom layer when the multiplication pattern is reused.
  • Debug matrix multiplication by checking tensor shapes first.
  • If the goal is a standard learnable linear projection, a Dense layer may already be the correct abstraction.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.