keras-team/keras · error · ValueError

Incompatible input shapes: axis values

Error message

Incompatible input shapes: axis values {shape1[axes[0]]} (at axis {axes[0]}) != {shape2[axes[1]]} (at axis {axes[1]}). Full input shapes: {shape1}, {shape2}

What it means

Compatibility check in Dot.build: after resolving self.axes (int expanded to both inputs, negatives normalized) it compares the contracted dimension sizes shape1[axes[0]] and shape2[axes[1]]; unequal sizes mean the inner-product axes cannot be multiplied, so the build fails.

Solutions

  1. Align the two inputs' contracted dimensions — e.g. give both Dense branches the same output units before Dot
  2. Change the axes so they select matching dims, such as axes=-1 to contract last axes
  3. Insert a projection layer (Dense of the right size) on one branch to match the other's axis size
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/merging/dot.py:287 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/8e1f20df573d68f9. Report an issue: GitHub.

Appendix: source

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

            or len(input_shape) != 2
        ):
            raise ValueError(
                f"A `Dot` layer should be called on a list of 2 inputs. "
                f"Received: input_shape={input_shape}"
            )
        shape1 = input_shape[0]
        shape2 = input_shape[1]
        if shape1 is None or shape2 is None:
            return
        if isinstance(self.axes, int):
            if self.axes < 0:
                axes = [self.axes % len(shape1), self.axes % len(shape2)]
            else:
                axes = [self.axes] * 2
        else:
            axes = self.axes
        if shape1[axes[0]] != shape2[axes[1]]:
            raise ValueError(
                f"Incompatible input shapes: "
                f"axis values {shape1[axes[0]]} (at axis {axes[0]}) != "
                f"{shape2[axes[1]]} (at axis {axes[1]}). "
                f"Full input shapes: {shape1}, {shape2}"
            )

    def _merge_function(self, inputs):
        if len(inputs) != 2:
            raise ValueError(
                f"A `Dot` layer should be called on exactly 2 inputs. "
                f"Received: inputs={inputs}"
            )
        x1 = inputs[0]
        x2 = inputs[1]

        if isinstance(self.axes, int):
            if self.axes < 0:
                axes = [

View on GitHub (pinned to 7a34a03db6)