keras-team/keras · error · ValueError

Cannot perform batch_dot over axis 0. If your inputs are…

Error message

Cannot perform batch_dot over axis 0. If your inputs are not batched, add a dummy batch dimension to your inputs using keras.ops.expand_dims(x, 0)

What it means

Sanity check after negative-axis conversion in batch_dot: axis 0 is reserved as the batch dimension, so contracting over it is forbidden; the error usually means the user passed unbatched vectors to an API that expects a batch dimension.

Solutions

  1. Add a dummy batch dimension to each input: keras.ops.expand_dims(x, 0) — exactly as the message suggests
  2. Shift the axes so neither entry is 0, e.g. axes=(1, 1) instead of axes=(0, 0)
  3. For plain dot products of vectors, use keras.ops.dot or keras.ops.matmul on reshaped tensors instead of batch_dot
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/merging/dot.py:105 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/384363e0fb5aeb01. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/layers/merging/dot.py:105

    if any(isinstance(a, (list, tuple)) for a in axes):
        raise ValueError(
            f"Multiple target dimensions are not supported. "
            f"Expected: None, int, (int, int), "
            f"Provided: {axes} "
        )

    # if tuple, convert to list.
    axes = list(axes)

    # convert negative indices.
    if axes[0] < 0:
        axes[0] += x_ndim
    if axes[1] < 0:
        axes[1] += y_ndim

    # sanity checks
    if 0 in axes:
        raise ValueError(
            "Cannot perform batch_dot over axis 0. "
            "If your inputs are not batched, "
            "add a dummy batch dimension to your "
            "inputs using keras.ops.expand_dims(x, 0)"
        )
    a0, a1 = axes
    d1 = x_shape[a0]
    d2 = y_shape[a1]

    if d1 is not None and d2 is not None and d1 != d2:
        raise ValueError(
            f"Cannot do batch_dot on inputs with shapes "
            f"{x_shape} and {y_shape} with axes={axes}. "
            f"x.shape[{axes[0]}] != y.shape[{axes[1]}] ({d1} != {d2})."
        )

    # backup ndims. Need them later.
    orig_x_ndim = x_ndim

View on GitHub (pinned to 7a34a03db6)