使用多个输入训练Keras模型

时间:2019-01-23 15:29:12

标签: python machine-learning keras deep-learning

我想使用keras训练具有3种不同输入的模型。训练数据 x_train left_train right_train 的形状为(10000,83,12)。这是代码的一部分。

from keras.layers import Dense, Input, LSTM
...
x = Input(shape = (83,12), dtype = "float32")
left = Input(shape = (83,12), dtype = "float32")
right = Input(shape = (83,12), dtype = "float32")
...
model = Model(inputs = [x, left, right], outputs = output)
model.compile(optimizer = "adadelta", loss = "categorical_crossentropy", metrics = ["accuracy"])
model.fit([x_train, left_train, right_train], y_train, validation_data=(x_test, y_test), epochs=20, batch_size=128)
...

我在训练时遇到以下错误:

ValueError                       Traceback (most recent call last)
<ipython-input-17-261d36872e91> in <module>()
     51 
     52 
---> 53 model.fit([x_train, left, right], y_train, validation_data= 
(x_test, y_test), epochs=20, batch_size=128)
     54 
     55 scores = model.evaluate(x_test, y_test)
     ...
     ValueError: Error when checking model input: the list of 
Numpy arrays that you are passing to your model is not the size the 
model expected. Expected to see 3 array(s), but instead got the 
following list of 1 arrays: [array(...

在调用 fit 方法时,我确实传递了3个输入的列表。有什么问题吗?

1 个答案:

答案 0 :(得分:1)

validation_datamodel.evaluate也需要多输入。在您的情况下,您仅提供单个数组(x_test, y_test),仅提供x_test,这将类似于([x_test, left_test, right_test], y_test)。本质上,验证数据需要与训练数据具有相同数量的输入/输出。