Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.einsum

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.

How it works

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.

Syntax

tf.einsum(equation, *inputs, **kwargs)

Parameters

NameTypeDescription
equationstrA string describing the contraction, using NumPy einsum notation.
*inputsTensorOne or more tensors to contract; their shapes must agree with the equation.
**kwargskeyword argumentsOptional keyword arguments, including optimize and name. optimize selects a contraction-path strategy and defaults to greedy.

Returns

The contracted Tensor, with shape determined by the equation.

Key points

  • It supports both contractions over labeled indices and outer products.
  • The equation provides a compact description of the element-wise tensor computation.
  • Matrix multiplication can be written as ij,jk->ik, contracting the shared middle axis.
  • With implicit output labels, repeated indices disappear through summation.
  • The equation uses the same string notation as NumPy's einsum.
  • Each operand must be a Tensor and its shape must match the labels in the equation.

Common mistakes

  • Named axes do not broadcast: dimensions associated with matching labels must be compatible, so equal-length axes are required.
  • Every input dimension assigned the same label must have the same size; otherwise the equation is invalid.

Example: Matrix multiplication with einsum

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.

When to use it

Generalized matrix operations

Compute matrix products or other contractions without manually reshaping and transposing tensors.

Tensor reductions

Reduce selected dimensions by leaving repeated labels out of the explicit output.

Outer products

Combine independent labeled axes to construct an outer product.

Batch-aware tensor algebra

Express higher-dimensional operations while retaining batch dimensions through ellipsis notation.

Interview questions

QHow does the equation 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.

QWhat is the difference between implicit and explicit output subscripts?

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.

QWhat optional controls does 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.einsum
  • tf.keras.ops.einsum

Source: Unique. Checked 2026-10-15.