keras-team/keras · error · ValueError
Cannot do batch_dot on inputs with rank < 2. Received inputs
Error message
Cannot do batch_dot on inputs with rank < 2. Received inputs with shapes {x_shape} and {y_shape}. What it means
The batch_dot operation (used by Keras's Dot merge layer) requires both operands to have rank >= 2 (a batch dim plus at least one feature dim). This error fires in Dot._merge_function -> batch_dot when either input is a 1-D vector.
Source
Thrown at keras/src/layers/merging/dot.py:60
integer. The sizes of `x.shape[axes[0]]` and `y.shape[axes[1]]`
should be equal.
Note that axis `0` (the batch axis) cannot be included.
Returns:
A tensor with shape equal to the concatenation of `x`'s shape
(less the dimension that was summed over) and `y`'s shape (less the
batch dimension and the dimension that was summed over). If the final
rank is 1, we reshape it to `(batch_size, 1)`.
"""
x_shape = x.shape
y_shape = y.shape
x_ndim = len(x_shape)
y_ndim = len(y_shape)
if x_ndim < 2 or y_ndim < 2:
raise ValueError(
f"Cannot do batch_dot on inputs "
f"with rank < 2. "
f"Received inputs with shapes "
f"{x_shape} and {y_shape}."
)
x_batch_size = x_shape[0]
y_batch_size = y_shape[0]
if x_batch_size is not None and y_batch_size is not None:
if x_batch_size != y_batch_size:
raise ValueError(
f"Cannot do batch_dot on inputs "
f"with different batch sizes. "
f"Received inputs with shapes "
f"{x_shape} and {y_shape}."
)
if isinstance(axes, int):View on GitHub (pinned to 7a34a03db6)
Solutions
- Expand vectors to rank 2 before the Dot: x = keras.ops.expand_dims(x, -1)
- Use keras.ops.sum(x * y, axis=-1) or a Dense(1) for vector inner products instead of Dot
- Check upstream layers (squeeze, reduce) that might drop a dimension
Example fix
# before out = layers.Dot(axes=-1)([x, y]) # x shape (None,) -> ValueError # after x = layers.Reshape((1,))(x) # or expand_dims to rank 2 out = layers.Dot(axes=-1)([x, y])
Defensive patterns
Strategy: validation
Validate before calling
assert len(x.shape) >= 2 and len(y.shape) >= 2, 'batch_dot needs rank >= 2'
Type guard
def batch_dot_ok(x, y) -> bool:
return len(x.shape) >= 2 and len(y.shape) >= 2 Prevention
- Check ndim / len(shape) before Dot layers
- Reshape or expand_dims vectors to rank 2 first
- Use element-wise multiply plus sum for vector inner products
When it happens
Trigger: Calling layers.Dot(axes=1)([x, y]) where x or y is shape (None,) or (batch_size,); using batched products on plain vectors.
Common situations: Dotting raw embedding outputs without a time/channel axis; mixing up Dot with inner products on flattened vectors; inputs unexpectedly squeezed upstream.
Related errors
- A `Concatenate` layer requires inputs with matching shapes e
- Cannot do batch_dot on inputs with rank < 2. Received inputs
- Cannot do batch_dot on inputs with different batch sizes. Re
- Multiple target dimensions are not supported. Expected: None
- Cannot perform batch_dot over axis 0. If your inputs are not
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/3a90328f0eb4f913.
Report an issue: GitHub.