pythontensorflowmachine-learningkeras

Incomprehensible shape error with one of the inputs of my non-sequential keras model


I wrote the following keras model

input_A = Input(shape=[5], name="wide_input")
hidden_layer_1 = Dense(10, activation="relu", name='h_wide_layer')(input_A)
input_B = Input(shape=[6], name="deep_input")
hidden_layer_2 = Dense(30, activation="relu", name='h_deep_layer_1')(input_B)
hidden_layer_3 = Dense(30, activation="relu", name='h_deep_layer_2')(hidden_layer_2)
concat = Concatenate()([hidden_layer_1, hidden_layer_3])
output = Dense(1, name="output")(concat)
complex_model_2_1 = keras.Model(inputs=[input_A, input_B], outputs=[output])

#Compile the second complexe model
complex_model_2_1.compile(loss="mean_squared_error", optimizer="sgd")

#Train
X_train_A, X_train_B = X_train[:, :5], X_train[:, 2:]
X_valid_A, X_valid_B = X_valid[:, :5], X_valid[:, 2:]
X_test_A, X_test_B = X_test_full[:, :5], X_test_full[:, 2:]
X_new_A, X_new_B = X_new[:, :5], X_new[:, 2:]
print(f"Shape of X_train_A: {X_train_A.shape}")
print(f"Shape of X_train_B: {X_train_B.shape}")
history = complex_model_2_1.fit({"wide_input": X_train_A, "deep_input": X_train_B},
                                y_train, epochs=20, validation_data=((X_valid_A, X_valid_B), y_valid))

X_train has 8 features so X_train_A and X_train_B have actually 5 and 6 features respectively. But, I don't understand why I get the bellow incompatible shape error for hidden_layer_2:

ValueError Traceback (most recent call last)
<ipython-input-42-86b1419058f7> in <cell line: 11>()
      9 print(f"Shape of X_train_A: {X_train_A.shape}")
     10 print(f"Shape of X_train_B: {X_train_B.shape}")
---> 11 history = complex_model_2_1.fit({"wide_input": X_train_A, "deep_input": X_train_B},
     12                                 y_train, epochs=20, validation_data=((X_valid_A, X_valid_B), y_valid))
     13 

1 frames
/usr/local/lib/python3.10/dist-packages/keras/src/layers/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    225                     None,
    226                 }:
--> 227                     raise ValueError(
    228                         f'Input {input_index} of layer "{layer_name}" is '
    229                         f"incompatible with the layer: expected axis {axis} "

ValueError: Exception encountered when calling Functional.call().

Input 0 of layer "h_deep_layer_1" is incompatible with the layer: expected axis -1 of input shape to have value 6, but received input with shape (None, 5)

Arguments received by Functional.call():
  • inputs={'wide_input': 'tf.Tensor(shape=(None, 5), dtype=float32)', 'deep_input': 'tf.Tensor(shape=(None, 6), dtype=float32)'}
  • training=True
  • mask={'wide_input': 'None', 'deep_input': 'None'}

How to fix it?

PS: Gemini in Google colab fails to explain the issue and proposes to me X_train_B = X_train[:, 5:] which is incorrect (having a shape of (_, 3)


Solution

  • to fix your problem you need to change the fit function. Your fit function should pass inputs as list instead of dictionary:

    history = complex_model_2_1.fit([X_train_A, X_train_B],
                                    y_train, epochs=20, validation_data=((X_valid_A, X_valid_B), y_valid))
    

    Explanation
    you either pass inputs as dictionary or as list. To pass inputs as lists you initiate keras model like this:

    complex_model_2_1 = keras.Model(inputs=[input_A,input_B], outputs=[output])
    

    If you want to use inputs as dictionary you should pass dict argument as inputs to the model. But in this case during training and inference all datasets must be passed as lists.

    complex_model_2_1 = keras.Model(inputs={'wide_input':input_A,                                        
                                            'deep_input':input_B}, outputs=[output])