cooldood3490
cooldood3490

Reputation: 2498

Keras Functional API Multi Input Layer

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.

Upvotes: 1

Views: 1472

Answers (2)

Luke DeLuccia
Luke DeLuccia

Reputation: 541

Change

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

to

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

Upvotes: 2

Primusa
Primusa

Reputation: 13498

The inputs must be your Input() layers:

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

Upvotes: 3

Related Questions