{"record":{"id":"a1cca8bf71776d74","repo":"keras-team/keras","slug":"input-shape-and-output-shape-do-not-match-at-share","errorCode":null,"errorMessage":"Input shape and output shape do not match at shared dimension '{dim}'. Input shape is {input_shape_at_dim}, and output shape is {output_shape[output_dim_map[dim]]}.","messagePattern":"Input shape and output shape do not match at shared dimension '(.+?)'\\. Input shape is (.+?), and output shape is (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/core/einsum_dense.py","lineNumber":1837,"sourceCode":"        }\n        # Because we've constructed the full output shape already, we don't need\n        # to do negative indexing.\n        output_dim_map = {\n            dim: (i + elided) for i, dim in enumerate(output_spec)\n        }\n    else:\n        input_dim_map = {dim: i for i, dim in enumerate(input_spec)}\n        output_dim_map = {dim: i for i, dim in enumerate(output_spec)}\n\n    for dim in input_spec:\n        input_shape_at_dim = input_shape[input_dim_map[dim]]\n        if dim in output_dim_map:\n            output_shape_at_dim = output_shape[output_dim_map[dim]]\n            if (\n                output_shape_at_dim is not None\n                and output_shape_at_dim != input_shape_at_dim\n            ):\n                raise ValueError(\n                    \"Input shape and output shape do not match at shared \"\n                    f\"dimension '{dim}'. Input shape is {input_shape_at_dim}, \"\n                    \"and output shape \"\n                    f\"is {output_shape[output_dim_map[dim]]}.\"\n                )\n\n    for dim in output_spec:\n        if dim not in input_spec and dim not in weight_spec:\n            raise ValueError(\n                f\"Dimension '{dim}' was specified in the output \"\n                f\"'{output_spec}' but has no corresponding dim in the input \"\n                f\"spec '{input_spec}' or weight spec '{output_spec}'\"\n            )\n\n    weight_shape = []\n    input_axes, output_axes = [], []\n    for i, dim in enumerate(weight_spec):\n        if dim in output_dim_map:","sourceCodeStart":1819,"sourceCodeEnd":1855,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/core/einsum_dense.py#L1819-L1855","documentation":"In a split-style einsum equation, a letter appearing on both the input side and the output side denotes a shared dimension; Keras requires the input's size at that dimension to equal the declared output size. If they differ, _analyze_split_string raises this error naming the mismatched dimension.","triggerScenarios":"Constructing EinsumDense with a split equation such as 'aab,bc->acd' where the layer's output_shape declares a shared letter (e.g. 'd') with a size different from the input tensor's size at that axis, then calling build(input_shape).","commonSituations":"Reusing an equation written for a different input width; hardcoding output_shape that no longer matches a resized embedding or feature dimension; changing vocabulary size or hidden dim without updating output_shape.","solutions":["Align the output_shape entry for the shared letter with the input tensor's actual dim size","Drop the shared letter from the output spec if it should be computed from the input instead of declared","Re-derive the equation from scratch for the new tensor shapes"],"exampleFix":"# before: input last dim 64, but shared 'd' declared as 128\nlayer = EinsumDense('aab,bd->ad', output_shape=(128, 8), bias_axes=None)\n# after\nlayer = EinsumDense('aab,bd->ad', output_shape=(64, 8), bias_axes=None)","handlingStrategy":"validation","validationCode":"def shared_dims_match(eq, input_shape, output_shape):\n    lhs, out = eq.split('->')\n    in_spec, _ = lhs.split(',')\n    in_spec = in_spec.lstrip('.')\n    for i, ch in enumerate(in_spec):\n        if ch in out:\n            out_i = out.lstrip('.').index(ch)\n            if output_shape[out_i] is not None and output_shape[out_i] != input_shape[i]:\n                return False\n    return True","typeGuard":"def shapes_compatible(eq, input_shape, output_shape):\n    return shared_dims_match(eq, input_shape, output_shape)","tryCatchPattern":"try:\n    layer.build(input_shape)\nexcept ValueError as e:\n    if 'do not match at shared dimension' not in str(e):\n        raise\n    # resize output_shape or the input pipeline\n    pass","preventionTips":["Derive output_shape programmatically from the input shape for shared letters","Add shape assertions in data-pipeline tests before model construction"],"tags":["keras","einsum-dense","shape-mismatch","split-equation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}