Generalized matrix operations
Compute matrix products or other contractions without manually reshaping and transposing tensors.
TensorFlow · Function
tf.einsum performs tensor contractions and outer products from a compact Einstein-summation equation. Repeated labels can be summed, while explicit output labels determine the result's axes and order.
tf.einsum interprets an Einstein-summation equation to define element-wise tensor computations. Labels identify axes, repeated labels are contracted when omitted from the output, and an explicit -> specifies the output-axis order.
tf.einsum(equation, *inputs, **kwargs)| Name | Type | Description |
|---|---|---|
equation | str | A string describing the contraction, using NumPy einsum notation. |
*inputs | Tensor | One or more tensors to contract; their shapes must agree with the equation. |
**kwargs | keyword arguments | Optional keyword arguments, including optimize and name. optimize selects a contraction-path strategy and defaults to greedy. |
The contracted Tensor, with shape determined by the equation.
ij,jk->ik, contracting the shared middle axis.import tensorflow as tf
m0 = tf.constant([[1., 2., 3.], [4., 5., 6.]])
m1 = tf.constant([[1., 2.], [3., 4.], [5., 6.]])
result = tf.einsum("ij,jk->ik", m0, m1)
print(result.shape) # (2, 2)Multiplies two matrices by contracting their shared j axis.
Compute matrix products or other contractions without manually reshaping and transposing tensors.
Reduce selected dimensions by leaving repeated labels out of the explicit output.
Combine independent labeled axes to construct an outer product.
Express higher-dimensional operations while retaining batch dimensions through ellipsis notation.
ij,jk->ik represent matrix multiplication?The equation describes input and output axis labels in NumPy einsum format. In ij,jk->ik, the shared j axis is contracted, while i and k remain in the result.
Follow-up: What happens if ->ik is omitted? Repeated labels that are not listed in the output are summed, so the shared j dimension is still contracted.
Repeated labels are summed when they do not appear in the output specification. An explicit output section controls which labels remain and their ordering.
Follow-up: How would you preserve a repeated label instead? Include that label in the explicit output subscripts, subject to the equation's validity rules.
tf.einsum expose?The function accepts an equation string, tensor inputs whose shapes agree with that equation, and optional keyword arguments. The documented optimization choices include greedy, optimal, branch-2, branch-all, and auto, with greedy as the default.
Follow-up: Why might the optimization option matter with several operands? It selects a contraction path, which can change the intermediate work and memory requirements.
tf.linalg.einsumtf.keras.ops.einsum