Multiple inputs to Keras Sequential model

无人久伴 提交于 2019-12-18 08:55:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/53017177/multiple-inputs-to-keras-sequential-model

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