简体   繁体   English

使用多个输入训练 Keras 模型

[英]Training Keras model with multiple inputs

I want to train a model with 3 different inputs using keras.我想使用 keras 训练一个具有 3 个不同输入的模型。 The training data - x_train , left_train , right_train is of shape (10000,83,12).训练数据 - x_trainleft_trainright_train的形状为 (10000,83,12)。 Here is part of the code.这是代码的一部分。

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)
...

I am getting the following error while training:我在训练时收到以下错误:

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(...

I do pass a list of 3 inputs when calling fit method.在调用fit方法时,我确实传递了 3 个输入的列表。 What's the problem?有什么问题?

The validation_data and model.evaluate needs to be multi-input as well. validation_datamodel.evaluate需要是多输入的。 In your case you only provide a single array (x_test, y_test) and just x_test where it would be something similar to ([x_test, left_test, right_test], y_test) .在您的情况下,您只提供一个数组(x_test, y_test)x_test ,它类似于([x_test, left_test, right_test], y_test) Essentially, the validation data needs to have same number of inputs / outputs as the training data.本质上,验证数据需要与训练数据具有相同数量的输入/输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM