keras-team/keras · error · ValueError
Invalid einsum equation '{equation}'. Equations must be in t
Error message
Invalid einsum equation '{equation}'. Equations must be in the form [X],[Y]->[Z], ...[X],[Y]->...[Z], or [X]...,[Y]->[Z].... What it means
EinsumDense parses its equation with a regex covering 'ab,bc->ac', ellipsis forms like '...a,ab->...b', and split forms like 'aab,bc->acd' (where repeated left-side letters define extra output dims). Any equation matching none of these patterns raises this ValueError at build or compute_output_shape time.
Source
Thrown at keras/src/layers/core/einsum_dense.py:1759
# This is the case where ellipses are present on the left.
split_string = re.match(
"0([a-zA-Z]+),([a-zA-Z]+)->0([a-zA-Z]+)", dot_replaced_string
)
if split_string:
return _analyze_split_string(
split_string, bias_axes, input_shape, output_shape, left_elided=True
)
# This is the case where ellipses are present on the right.
split_string = re.match(
"([a-zA-Z]{2,})0,([a-zA-Z]+)->([a-zA-Z]+)0", dot_replaced_string
)
if split_string:
return _analyze_split_string(
split_string, bias_axes, input_shape, output_shape
)
raise ValueError(
f"Invalid einsum equation '{equation}'. Equations must be in the form "
"[X],[Y]->[Z], ...[X],[Y]->...[Z], or [X]...,[Y]->[Z]...."
)
def _analyze_split_string(
split_string, bias_axes, input_shape, output_shape, left_elided=False
):
"""Computes kernel and bias shapes from a parsed einsum equation.
This function takes the components of an einsum equation, validates them,
and calculates the required shapes for the kernel and bias weights.
Args:
split_string: A regex match object containing the input, weight, and
output specifications.
bias_axes: A string indicating which output axes to apply a bias to.
input_shape: The shape of the input tensor.View on GitHub (pinned to 7a34a03db6)
Solutions
- Restrict the equation to exactly two operands joined by ',' with a '->' output, e.g. 'ab,bc->ac'
- Place ellipsis only at the start of operands as in '...a,ab->...b'
- For split equations, ensure the output is a strict superset of the shared letters of the two inputs
- Print-check the equation string for stray spaces or wrong arrow characters before constructing the layer
Example fix
# before
layer = keras.layers.EinsumDense('a,b,c->d', ...)
# after
layer = keras.layers.EinsumDense('ab,bc->ac', ...)
# ellipsis form
layer = keras.layers.EinsumDense('...a,ab->...b', ...) Defensive patterns
Strategy: validation
Validate before calling
import re
EINSUM_RE = re.compile(r'^[a-z.]*,[a-z.]*->[a-z.]*$')
def valid_two_operand(eq):
eq = eq.replace(' ', '')
if not EINSUM_RE.match(eq):
return False
left, _ = eq.split('->')
return len(left.split(',')) == 2 Type guard
def is_valid_einsum_equation(eq):
eq = eq.replace(' ', '')
return '->' in eq and eq.count(',') == 1 and valid_two_operand(eq) Try / catch
try:
layer = keras.layers.EinsumDense(eq, output_shape=shape)
layer.compute_output_shape(input_shape)
except ValueError as e:
if 'Invalid einsum equation' in str(e):
raise ValueError('Bad equation: ' + str(e)) from e
raise Prevention
- Validate the equation string against a regex before constructing the layer
- Keep equations to the two documented forms; test with numpy.einsum first
- Never build equations by string concatenation without checking the result
When it happens
Trigger: Passing an equation with three operands ('a,b,c->d'), malformed separators (missing '->', spaces inside subscripts), a wrong arrow, or an unparseable split equation to keras.layers.EinsumDense, then calling build() or compute_output_shape().
Common situations: Copy-pasting numpy.einsum equations with three operands; typos like 'ab,bc=>ac' or 'ab bc->ac'; using an ellipsis in an unsupported position.
Related errors
- Dimension '{dim}' was specified in the output '{output_spec}
- Weight dimension '{dim}' did not have a match in either the
- Bias dimension '{char}' was requested, but is not part of th
- If using `weights="imagenet"` as true, `classes` should be 1
- The number of repeats in `EfficientNet` must be > 0. Receive
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/f00cd16a74f3f0a3.
Report an issue: GitHub.