使用tensorflow连接(合并)层keras

时间:2017-05-18 08:14:43

标签: python tensorflow keras

我想制作一个如下模型。

input data    input data
    |             | 
 convnet1      convet2
    |             |
maxpooling    maxpooling
    |             |
    - Dense layer -
           |
      Dense layer

所以,我写了下面的代码。

model1 = Sequential()
model1.add(Conv2D(32, (3, 3), activation='relu', input_shape=(bands, frames, 1)))
print(model1.output_shape)
model1.add(MaxPooling2D(pool_size=(2, 2)))
model1.add(Flatten())

model2 = Sequential()
model2.add(Conv2D(32, (9, 9), activation='relu', input_shape=(bands, frames, 1)))
print(model2.output_shape)
model2.add(MaxPooling2D(pool_size=(4, 4)))
model2.add(Flatten())

modelall = Sequential()
modelall.add(concatenate([model1, model2], axis=1))
modelall.add(Dense(100, activation='relu'))

modelall.add(Dropout(0.5))
modelall.add(Dense(10, activation='softmax')) #number of class = 10
print(modelall.output_shape)

modelall.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

modelall.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=training_epochs)
score = modelall.evaluate(X_test, y_test, batch_size=batch_size)

然而,我收到了一个错误。

AttributeError: 'Sequential' object has no attribute 'get_shape'

整个错误回溯如下。

  Traceback (most recent call last):
  File "D:/keras/cnn-keras.py", line 54, in <module>
    model.add(concatenate([modelf, modelt], axis=1))
  File "C:\Users\Anaconda3\lib\site-packages\keras\layers\merge.py", line 508, in concatenate
    return Concatenate(axis=axis, **kwargs)(inputs)
  File "C:\Users\Anaconda3\lib\site-packages\keras\engine\topology.py", line 542, in __call__
    input_shapes.append(K.int_shape(x_elem))
  File "C:\Users\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 411, in int_shape
    shape = x.get_shape()
AttributeError: 'Sequential' object has no attribute 'get_shape'

张量流引起的错误是什么?有关如何修复的想法吗?

1 个答案:

答案 0 :(得分:1)

您不能使用顺序模型来创建分支,这不起作用。

您必须使用功能API:

from keras.models import Model    
from keras.layers import *

将每个分支作为顺序模型可以,但fork必须位于Model中。

#in the functional API you create layers and call them passing tensors to get their output:

conc = Concatenate()([model1.output, model2.output])

    #notice you concatenate outputs, which are tensors.     
    #you cannot concatenate models


out = Dense(100, activation='relu')(conc)
out = Dropout(0.5)(out)
out = Dense(10, activation='softmax')(out)

modelall = Model([model1.input, model2.input], out)

这里没有必要,但通常在功能API中创建Input层:

inp = Input((shape of the input))
out = SomeLayer(blbalbalba)(inp)
....
model = Model(inp,out)