{"record":{"id":"f00cd16a74f3f0a3","repo":"keras-team/keras","slug":"invalid-einsum-equation-equation-equations-mu","errorCode":null,"errorMessage":"Invalid einsum equation '{equation}'. Equations must be in the form [X],[Y]->[Z], ...[X],[Y]->...[Z], or [X]...,[Y]->[Z]....","messagePattern":"Invalid einsum equation '(.+?)'\\. Equations must be in the form \\[X\\],\\[Y\\]->\\[Z\\], \\.\\.\\.\\[X\\],\\[Y\\]->\\.\\.\\.\\[Z\\], or \\[X\\]\\.\\.\\.,\\[Y\\]->\\[Z\\]\\.\\.\\.\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/einsum_dense.py","lineNumber":1759,"sourceCode":"    # This is the case where ellipses are present on the left.\n    split_string = re.match(\n        \"0([a-zA-Z]+),([a-zA-Z]+)->0([a-zA-Z]+)\", dot_replaced_string\n    )\n    if split_string:\n        return _analyze_split_string(\n            split_string, bias_axes, input_shape, output_shape, left_elided=True\n        )\n\n    # This is the case where ellipses are present on the right.\n    split_string = re.match(\n        \"([a-zA-Z]{2,})0,([a-zA-Z]+)->([a-zA-Z]+)0\", dot_replaced_string\n    )\n    if split_string:\n        return _analyze_split_string(\n            split_string, bias_axes, input_shape, output_shape\n        )\n\n    raise ValueError(\n        f\"Invalid einsum equation '{equation}'. Equations must be in the form \"\n        \"[X],[Y]->[Z], ...[X],[Y]->...[Z], or [X]...,[Y]->[Z]....\"\n    )\n\n\ndef _analyze_split_string(\n    split_string, bias_axes, input_shape, output_shape, left_elided=False\n):\n    \"\"\"Computes kernel and bias shapes from a parsed einsum equation.\n\n    This function takes the components of an einsum equation, validates them,\n    and calculates the required shapes for the kernel and bias weights.\n\n    Args:\n        split_string: A regex match object containing the input, weight, and\n            output specifications.\n        bias_axes: A string indicating which output axes to apply a bias to.\n        input_shape: The shape of the input tensor.","sourceCodeStart":1741,"sourceCodeEnd":1777,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/einsum_dense.py#L1741-L1777","documentation":"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.","triggerScenarios":"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().","commonSituations":"Copy-pasting numpy.einsum equations with three operands; typos like 'ab,bc=>ac' or 'ab bc->ac'; using an ellipsis in an unsupported position.","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"],"exampleFix":"# before\nlayer = keras.layers.EinsumDense('a,b,c->d', ...)\n# after\nlayer = keras.layers.EinsumDense('ab,bc->ac', ...)\n\n# ellipsis form\nlayer = keras.layers.EinsumDense('...a,ab->...b', ...)","handlingStrategy":"validation","validationCode":"import re\nEINSUM_RE = re.compile(r'^[a-z.]*,[a-z.]*->[a-z.]*$')\ndef valid_two_operand(eq):\n    eq = eq.replace(' ', '')\n    if not EINSUM_RE.match(eq):\n        return False\n    left, _ = eq.split('->')\n    return len(left.split(',')) == 2","typeGuard":"def is_valid_einsum_equation(eq):\n    eq = eq.replace(' ', '')\n    return '->' in eq and eq.count(',') == 1 and valid_two_operand(eq)","tryCatchPattern":"try:\n    layer = keras.layers.EinsumDense(eq, output_shape=shape)\n    layer.compute_output_shape(input_shape)\nexcept ValueError as e:\n    if 'Invalid einsum equation' in str(e):\n        raise ValueError('Bad equation: ' + str(e)) from e\n    raise","preventionTips":["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"],"tags":["keras","einsum-dense","einsum-equation","validation"],"backgroundTag":"einsum-equation-syntax","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}