Multiply Tensors with different ranks
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Multiplying tensors with different ranks is not one single operation. It can mean element-wise multiplication with broadcasting, an outer product, or a contracted product such as matrix multiplication or tensordot. The right answer depends on which axes should align and what shape you want as the result.
Start by Deciding What “Multiply” Means
Two tensors can interact in several valid ways. The most common interpretations are:
- element-wise multiplication with broadcasting
- outer product, which increases rank
- contraction over one or more axes, which reduces rank
If you skip this definition and ask only whether ranks are different, you still do not know which operation should happen.
For example, a vector of shape (3,) and a matrix of shape (2, 3) can be combined in at least three meaningful ways depending on the intended math.
Element-Wise Multiplication Uses Broadcasting
If the shapes are compatible under broadcasting rules, frameworks such as NumPy and TensorFlow can multiply element by element even when the ranks differ.
Output:
Here the vector of shape (3,) is broadcast across the rows of the matrix of shape (2, 3). The ranks differ, but the trailing dimensions are compatible.
Outer Product Creates a Higher-Rank Tensor
If you want every element of one tensor multiplied by every element of another, you want an outer product. This increases rank rather than trying to match existing axes.
Output:
Conceptually, this turns a rank-1 tensor and another rank-1 tensor into a rank-2 result. Generalized outer products can be expressed with einsum or by reshaping and broadcasting manually.
Contraction Multiplies and Sums Over Chosen Axes
Sometimes the intended operation is not element-wise at all. Matrix multiplication is really a contraction over one axis. The same idea extends to higher-rank tensors.
With NumPy, tensordot is often the clearest API:
This contracts the last axis of x with the first axis of y. The result shape is formed from the remaining axes.
That is why “different ranks” by itself is not a problem. What matters is whether the chosen contraction axes are compatible.
einsum Makes the Axis Logic Explicit
When broadcasting and contraction rules start to feel opaque, einsum can clarify the intent by naming axes directly.
This example performs the same broadcasted scaling as earlier, but with an explicit axis description. einsum is especially useful for higher-rank tensor code where silent broadcasting would be hard to read.
TensorFlow Uses the Same Core Ideas
TensorFlow follows similar patterns. Element-wise multiplication relies on broadcasting, while contraction can use tf.tensordot or tf.einsum.
If the shapes are not broadcast-compatible, TensorFlow raises a shape error instead of guessing what you meant.
Reshaping Is Often Part of the Solution
When the intended multiplication is clear but the raw shapes do not line up, reshape one tensor so the operation matches the intended axis structure.
For example, if you want a column vector instead of a row-wise broadcast, reshape explicitly:
That [:, None] turns the vector from shape (2,) into (2, 1), which changes how broadcasting works.
Common Pitfalls
The most common mistake is asking for “multiplication” without specifying whether the goal is element-wise multiplication, an outer product, or a contraction.
Another mistake is relying on broadcasting without checking which axes are being expanded. The code may run and still compute the wrong math.
Developers also overlook reshaping. A small explicit reshape often makes the intended operation both correct and readable.
Summary
- Tensors with different ranks can still be multiplied, but the operation must be defined clearly.
- Broadcasting handles element-wise multiplication when shapes are compatible.
- Outer products increase rank, while contractions such as
tensordotreduce selected axes. - '
einsumis often the clearest way to express complex tensor multiplication.' - When shapes do not line up, reshape deliberately instead of hoping broadcasting guesses your intent.
Related reading
- Multiset domination algorithm
- n-th or Arbitrary Combination of a Large Set
- n steps with 1, 2 or 3 steps taken. How many ways to get to the top?
- nᵗʰ ugly number
- Naive Bayes classifier bases decision only on a-priori probabilities
- Nan in summary histogram
- Natural Logarithm of Bessel Function, Overflow
- Nearest neighbor search with periodic boundary conditions

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.