Understanding NumPy's einsum
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
numpy.einsum is a compact way to describe tensor operations using index labels instead of a long chain of reshapes and matrix helpers. It looks unfamiliar at first because the syntax comes from Einstein summation notation, but the underlying rule is simple: name axes, decide which ones are multiplied together, and decide which ones remain in the output. Once that clicks, many array operations become easier to express.
Read the Subscript String as Axis Names
Consider this example:
Read "ij,j->i" step by step:
- the first input has axes
iandj - the second input has axis
j - repeated labels are multiplied and summed over
- only
iremains in the output
That is matrix-vector multiplication. einsum is not magic here. It is just describing the same operation in axis language.
Learn It Through Familiar Operations
Many common NumPy operations have direct einsum versions.
Dot product:
Transpose:
Matrix multiplication:
These examples are worth memorizing because they build intuition. Once you can translate these, more advanced contractions feel much less abstract.
Repeated Labels Mean Reduction
The fastest way to understand einsum is to track which labels disappear. Any label that is repeated across inputs and not included in the output is summed away.
For example, row and column sums can be written directly:
The first keeps rows, the second keeps columns, and the third keeps no axes at all, so it returns the total sum. Thinking in terms of labels that survive is often easier than thinking in terms of a memorized formula.
Batched Tensor Operations Are Where einsum Shines
einsum becomes especially valuable when the operation involves batch dimensions or more than two axes. Instead of reshaping by hand, you can write the contraction directly.
Here b is the batch axis, so each item in the batch is multiplied independently. This is a good example of why einsum is popular in scientific computing and machine learning code: it exposes the intended axis behavior directly.
Use optimize=True for Larger Contractions
For multi-operand expressions, contraction order can matter. NumPy can choose a better path when you ask it to optimize.
This does not mean einsum is always the fastest tool, but it does mean you should not judge performance from the syntax alone. For simple operations, matmul or sum may still be clearer.
Use einsum When It Clarifies the Operation
The best use of einsum is not showing off compactness. The best use is making a tensor relationship explicit. If a reader already understands A @ B, use @. If a contraction involves several axes and awkward reshapes, einsum may be the clearest representation.
A good engineering rule is to choose the most direct expression of the operation. einsum is powerful, but clarity still matters more than cleverness.
Common Pitfalls
- Treating the subscript string as magic instead of mapping each label to a real axis.
- Forgetting that repeated labels are usually summed away unless they appear in the output.
- Using
einsumfor very simple operations where@orsumwould be clearer. - Mixing up label order and producing the right numbers in the wrong shape.
- Ignoring
optimize=Truewhen contracting several operands and then blamingeinsumfor poor performance.
Summary
- '
einsumnames axes explicitly and describes which ones survive into the output.' - Repeated labels typically indicate multiplication followed by summation.
- Familiar operations such as dot product, transpose, and matrix multiplication can all be written with
einsum. - The function is especially helpful for batched and higher-rank tensor contractions.
- Use it when it makes the operation clearer, not just shorter.

