keras-team/keras · error · ValueError

Expected the two input tensors to have identical shapes. Rec

Error message

Expected the two input tensors to have identical shapes. Received input_shape={input_shape}

What it means

HashedCrossing requires its two input tensors to have identical shapes (the last dimension must match). Mismatched shapes make the crossing ill-defined, so compute_output_shape rejects them.

Source

Thrown at keras/src/layers/preprocessing/hashed_crossing.py:121

        self.num_bins = num_bins
        self.output_mode = output_mode
        self.sparse = sparse
        self._allow_non_tensor_positional_args = True
        self._convert_input_args = False
        self.supports_jit = False

    def compute_output_shape(self, input_shape):
        if (
            not len(input_shape) == 2
            or not isinstance(input_shape[0], tuple)
            or not isinstance(input_shape[1], tuple)
        ):
            raise ValueError(
                "Expected as input a list/tuple of 2 tensors. "
                f"Received input_shape={input_shape}"
            )
        if input_shape[0][-1] != input_shape[1][-1]:
            raise ValueError(
                "Expected the two input tensors to have identical shapes. "
                f"Received input_shape={input_shape}"
            )

        if not input_shape:
            if self.output_mode == "int":
                return ()
            return (self.num_bins,)
        if self.output_mode == "int":
            return tuple(input_shape[0])

        if self.output_mode == "one_hot" and input_shape[0][-1] != 1:
            return tuple(input_shape[0]) + (self.num_bins,)

        return tuple(input_shape[0])[:-1] + (self.num_bins,)

    def call(self, inputs):
        from keras.src.backend import tensorflow as tf_backend

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Make both inputs the same shape, typically (batch, 1)
  2. Reshape one input upstream: tf.reshape(x, [-1, 1]) or a keras Reshape((1,)) layer
  3. Check Input(shape=...) declarations in Functional models for equality

Example fix

// before
a = keras.Input(shape=(1,)); b = keras.Input(shape=(2,))
out = HashedCrossing(100)([a, b])
// after
a = keras.Input(shape=(1,)); b = keras.Input(shape=(1,))
out = HashedCrossing(100)([a, b])
Defensive patterns

Strategy: validation

Validate before calling

assert input_shape[0][-1] == input_shape[1][-1], "inputs must have identical last dim"

Type guard

def same_last_dim(s):
    return s[0][-1] == s[1][-1]

Try / catch

catch ValueError from build/compute_output_shape and reshape both inputs to a common shape before retrying

Prevention

When it happens

Trigger: input_shape[0][-1] != input_shape[1][-1], e.g. Input(shape=(1,)) and Input(shape=(2,)) both wired into the crossing layer.

Common situations: Two Input layers with different shapes (e.g. (1,) and (2,)); one input reshaped upstream and the other not.

Related errors


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