mortusdetus
mortusdetus

Reputation: 97

Multiple inputs with Keras Functional 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)

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.

Upvotes: 1

Views: 3960

Answers (1)

Michele Tonutti
Michele Tonutti

Reputation: 4348

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)

Upvotes: 1

Related Questions