我写了以下keras模型
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))
#Evaluate the model
mse_test_complex_2_1 = complex_model_2_1.evaluate((X_test_A, X_test_B), y_test_full)
# Predict the first three houses price
y_pred_complex_2_1 = complex_model_2_1.predict((X_new_A, X_new_B))
X_train有8个特征,所以X_train_A和X_train_B实际上分别有5和6个特征。但是,我不明白为什么我会收到以下 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'}
有人可以帮我解决吗?
PS:Google colab 中的 Gemini 未能解释该问题,并向我建议 X_train_B = X_train[:, 5:] 这是不正确的(形状为 (_, 3)
要解决您的问题,您需要更改拟合函数。您的 fit 函数应该将输入作为列表而不是字典传递:
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))
解释
您可以将输入作为字典或列表传递。要将输入作为列表传递,您可以像这样启动 keras 模型:
complex_model_2_1 = keras.Model(inputs=[input_A,input_B], outputs=[output])
如果您想使用输入作为字典,您应该将 dict 参数作为模型的输入传递。但在这种情况下,在训练和推理过程中,所有数据集都必须作为列表传递。
complex_model_2_1 = keras.Model(inputs={'wide_input':input_A,
'deep_input':input_B}, outputs=[output])