Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple inputs to Keras Sequential model

I am trying to merge output from two models and give them as input to the third model using keras sequential model. Model1 :

inputs1 = Input(shape=(750,))
x = Dense(500, activation='relu')(inputs1)
x = Dense(100, activation='relu')(x)

Model1 :

inputs2 = Input(shape=(750,))
y = Dense(500, activation='relu')(inputs2)
y = Dense(100, activation='relu')(y)

Model3 :

merged = Concatenate([x, y])
final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(100, activation='relu'))
final_model.add(Dense(3, activation='softmax'))

Till here, my understanding is that, output from two models as x and y are merged and given as input to the third model. But when I fit this all like,

module3.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
module3.fit([in1, in2], np_res_array)

in1 and in2 are two numpy ndarray of dimention 10000*750 which contains my training data and np_res_array is the corresponding target.
This gives me error as 'list' object has no attribute 'shape' As far as know, this is how we give multiple inputs to a model, but what is this error? How do I resolve it?

like image 527
Piyush Doke Avatar asked Oct 26 '18 22:10

Piyush Doke


1 Answers

You can't do this using Sequential API. That's because of two reasons:

  1. Sequential models, as their name suggests, are a sequence of layers where each layer is connected directly to its previous layer and therefore they cannot have branches (e.g. merge layers, multiple input/output layers, skip connections, etc.).

  2. The add() method of Sequential API accepts a Layer instance as its argument and not a Tensor instance. In your example merged is a Tensor (i.e. concatenation layer's output).

Further, the correct way of using Concatenate layer is like this:

merged = Concatenate()([x, y])

However, you can also use concatenate (note the lowercase "c"), its equivalent functional interface, like this:

merged = concatenate([x, y])

Finally, to be able to construct that third model you also need to use the functional API.

like image 97
today Avatar answered Nov 04 '22 07:11

today