1

How do I define a multi input layer using Keras Functional API? Below is an example of the neural network I want to build. There are three input nodes. I want each node to be a 1 dimensional numpy array of different lengths.

Here's what I have so far. Basically I want to define an input layer with multiple input tensors.

from keras.layers import Input, Dense, Dropout, concatenate
from keras.models import Model

x1 = Input(shape =(10,))
x2 = Input(shape =(12,))
x3 = Input(shape =(15,))

input_layer = concatenate([x1,x2,x3])

hidden_layer = Dense(units=4, activation='relu')(input_layer)
prediction = Dense(1, activation='linear')(hidden_layer)

model = Model(inputs=input_layer,outputs=prediction)

model.summary()

Neural Network

The code gives the error.

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("x1_1:0", shape=(?, 10), dtype=float32) at layer "x1". The following previous layers were accessed without issue: []

Later when I fit the model I will pass in a list of 1D numpy arrays with the corresponding lengths.

1
  • 1
    The funcional API guide describes exactly how to make multiple input models keras.io/getting-started/functional-api-guide your problem is that you have to give the inputs (x1, x2, x3), not the layer after that. Commented Jan 3, 2019 at 21:16

2 Answers 2

3

The inputs must be your Input() layers:

model = Model(inputs=[x1, x2, x3],outputs=prediction)
Sign up to request clarification or add additional context in comments.

Comments

2

Change

model = Model(inputs=input_layer,outputs=prediction)

to

model = Model(inputs=[x1, x2, x3],outputs=prediction)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.