keras-team/keras · error · ValueError
Input shape and output shape do not match at shared dimensio
Error message
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]]}. What it means
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.
Source
Thrown at keras/src/layers/core/einsum_dense.py:1837
}
# Because we've constructed the full output shape already, we don't need
# to do negative indexing.
output_dim_map = {
dim: (i + elided) for i, dim in enumerate(output_spec)
}
else:
input_dim_map = {dim: i for i, dim in enumerate(input_spec)}
output_dim_map = {dim: i for i, dim in enumerate(output_spec)}
for dim in input_spec:
input_shape_at_dim = input_shape[input_dim_map[dim]]
if dim in output_dim_map:
output_shape_at_dim = output_shape[output_dim_map[dim]]
if (
output_shape_at_dim is not None
and output_shape_at_dim != input_shape_at_dim
):
raise ValueError(
"Input shape and output shape do not match at shared "
f"dimension '{dim}'. Input shape is {input_shape_at_dim}, "
"and output shape "
f"is {output_shape[output_dim_map[dim]]}."
)
for dim in output_spec:
if dim not in input_spec and dim not in weight_spec:
raise ValueError(
f"Dimension '{dim}' was specified in the output "
f"'{output_spec}' but has no corresponding dim in the input "
f"spec '{input_spec}' or weight spec '{output_spec}'"
)
weight_shape = []
input_axes, output_axes = [], []
for i, dim in enumerate(weight_spec):
if dim in output_dim_map:View on GitHub (pinned to 7a34a03db6)
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
Example fix
# before: input last dim 64, but shared 'd' declared as 128
layer = EinsumDense('aab,bd->ad', output_shape=(128, 8), bias_axes=None)
# after
layer = EinsumDense('aab,bd->ad', output_shape=(64, 8), bias_axes=None) Defensive patterns
Strategy: validation
Validate before calling
def shared_dims_match(eq, input_shape, output_shape):
lhs, out = eq.split('->')
in_spec, _ = lhs.split(',')
in_spec = in_spec.lstrip('.')
for i, ch in enumerate(in_spec):
if ch in out:
out_i = out.lstrip('.').index(ch)
if output_shape[out_i] is not None and output_shape[out_i] != input_shape[i]:
return False
return True Type guard
def shapes_compatible(eq, input_shape, output_shape):
return shared_dims_match(eq, input_shape, output_shape) Try / catch
try:
layer.build(input_shape)
except ValueError as e:
if 'do not match at shared dimension' not in str(e):
raise
# resize output_shape or the input pipeline
pass Prevention
- Derive output_shape programmatically from the input shape for shared letters
- Add shape assertions in data-pipeline tests before model construction
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Dimension '{dim}' was specified in the output '{output_spec}
- Architecture configuration does not match {weights_name} var
- Model name "{name}" does not match weights variant "{weights
- DenseNet does not support the `channels_first` image data fo
- The last dimension of `query_shape` and `value_shape` must b
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/a1cca8bf71776d74.
Report an issue: GitHub.