使用Keras Functional API的多个输入 [英] Multiple inputs with Keras Functional API
问题描述
Keras似乎缺少有关功能API的文档,但我可能会弄错一切.
It seems that Keras lacks documentation regarding functional API but I might be getting it all wrong.
我有多个独立的输入,并且我想预测每个输入的输出.到目前为止,这是我的代码:
I have multiple independent inputs and I want to predict an output for each input. Here's my code so far:
hour = Input(shape=(1,1), name='hour')
hour_output = LSTM(1, name='hour_output')(hour)
port = Input(shape=(1,1), name='port')
port_output = LSTM(1, name='port_output')(port)
model = Model(inputs=[hour, port], outputs = [hour_output, port_output])
model.compile(loss="mean_squared_error", optimizer="adam", metrics=['accuracy'])
model.fit(trainX, trainY, epochs=10 batch_size=1, verbose=2, shuffle=False)
我为此遇到的错误:
ValueError:没有为"hour_output"提供数据.需要每个键中的数据:['hour_output','port_output']
ValueError: No data provided for "hour_output". Need data for each key in: ['hour_output', 'port_output']
我也很难获得正确的输入,因此我最终使用了示例结构为{'hour': array([[[0]], [[1]], [[3]]]) }
的字典.我也不喜欢(使用字典).
I also had a hard time getting the right input for this, so I ended up using dictionary with example structure:{'hour': array([[[0]], [[1]], [[3]]]) }
. I don't like that (using dict) either.
请注意,我有更多输入要使用,这很有意义,但是现在我只是在尝试使模型正常工作.
Note that I have more inputs to use and for this to make sense, but now I am just trying to make the model work.
推荐答案
在您的model.fit中,您需要提供一个长度为2的输入列表,因为您在模型中定义了两个输入.
In your model.fit you need to provide a list of inputs with length = 2, since you define two inputs in your Model.
在train_hour
和train_port
中拆分您的训练数据,然后像这样调用fit
:
Split your training data in train_hour
and train_port
and call fit
like:
model.fit([train_X_hour, train_X_port], [train_Y_hour, train_Y_port] epochs=10, batch_size=1, verbose=2, shuffle=False)
这篇关于使用Keras Functional API的多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!