Multiple inputs with Keras Functional API

陌路散爱 提交于 2020-05-16 03:17:28

问题


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)

Error I get for this:

ValueError: No data provided for "hour_output". Need data for each key in: ['hour_output', 'port_output']

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.


回答1:


In your model.fit you need to provide a list of inputs with length = 2, since you define two inputs in your Model.

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)


来源:https://stackoverflow.com/questions/44615420/multiple-inputs-with-keras-functional-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!