keras-team/keras · error · ValueError
Inputs have incompatible shapes. Received shapes {shape1} an
Error message
Inputs have incompatible shapes. Received shapes {shape1} and {shape2} What it means
Merge layers (Add, Multiply, Average, Maximum, etc.) broadcast their inputs and require non-batch dimensions to be either equal or 1. This error fires during build/compute_output_shape when two inputs disagree on a dimension and neither is 1, so broadcasting cannot resolve them.
Source
Thrown at keras/src/layers/merging/base_merge.py:93
"""
if None in [shape1, shape2]:
return None
elif len(shape1) < len(shape2):
return self._compute_elemwise_op_output_shape(shape2, shape1)
elif not shape2:
return shape1
output_shape = list(shape1[: -len(shape2)])
for i, j in zip(shape1[-len(shape2) :], shape2):
if i is None or j is None:
output_shape.append(None)
elif i == 1:
output_shape.append(j)
elif j == 1:
output_shape.append(i)
else:
if i != j:
raise ValueError(
"Inputs have incompatible shapes. "
f"Received shapes {shape1} and {shape2}"
)
output_shape.append(i)
return tuple(output_shape)
def build(self, input_shape):
# Used purely for shape validation.
if not isinstance(input_shape[0], (tuple, list)):
raise ValueError(
"A merge layer should be called on a list of inputs. "
f"Received: input_shape={input_shape} (not a list of shapes)"
)
if len(input_shape) < 1:
raise ValueError(
"A merge layer should be called "
"on a list of at least 1 input. "
f"Received {len(input_shape)} inputs. "View on GitHub (pinned to 7a34a03db6)
Solutions
- Fix the upstream layers so both branches produce the same non-batch shape
- Insert a Dense/Conv projection or Reshape on one branch to align dimensions before the merge
- Use 1 for a dimension to exploit broadcasting intentionally
- If shapes are dynamic (None), confirm the runtime shapes actually match
Example fix
# before out = layers.Add()([enc, dec]) # (None,64) + (None,128) -> ValueError # after dec = layers.Dense(64)(dec) out = layers.Add()([enc, dec])
Defensive patterns
Strategy: validation
Validate before calling
def broadcastable(s1, s2):
return len(s1) == len(s2) and all(a == b or a == 1 or b == 1 or a is None or b is None for a, b in zip(s1, s2))
assert broadcastable(tuple(x.shape), tuple(y.shape)) Type guard
def shapes_broadcastable(s1, s2) -> bool:
return len(s1) == len(s2) and all(a == b or a == 1 or b == 1 or a is None or b is None for a, b in zip(s1, s2)) Prevention
- Assert all branch shapes before merge layers
- Add projections or reshapes when fusing branches
- Use model.summary() to verify shapes during development
When it happens
Trigger: Calling Add()([x, y]) where x has shape (None, 32) and y has shape (None, 16); Multiply on feature maps with different channel counts; shape inference on inputs with conflicting dims.
Common situations: Feeding embeddings of different dimensions into an Add; forgetting a projection/reshape layer before merging encoder and decoder branches; off-by-one pooling that changes a spatial dim on one branch only.
Related errors
- Cannot merge tensors with different batch sizes. Received te
- 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/30dfa63592a6fda8.
Report an issue: GitHub.