3

我试图合并keras中的2个顺序模型。下面是代码:在Keras中合并2个顺序模型

model1 = Sequential(layers=[ 
    # input layers and convolutional layers 
    Conv1D(128, kernel_size=12, strides=4, padding='valid', activation='relu', input_shape=input_shape), 
    MaxPooling1D(pool_size=6), 
    Conv1D(256, kernel_size=12, strides=4, padding='valid', activation='relu'), 
    MaxPooling1D(pool_size=6), 
    Dropout(.5), 

]) 

model2 = Sequential(layers=[ 
    # input layers and convolutional layers 
    Conv1D(128, kernel_size=20, strides=5, padding='valid', activation='relu', input_shape=input_shape), 
    MaxPooling1D(pool_size=5), 
    Conv1D(256, kernel_size=20, strides=5, padding='valid', activation='relu'), 
    MaxPooling1D(pool_size=5), 
    Dropout(.5), 

]) 

model = merge([model1, model2], mode = 'sum') 
Flatten(), 
Dense(256, activation='relu'), 
Dropout(.5), 
Dense(128, activation='relu'), 
Dropout(.35), 
# output layer 
Dense(5, activation='softmax') 
return model 

以下是错误日志:

File "/nics/d/home/dsawant/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 392, in is_keras_tensor raise ValueError('Unexpectedly found an instance of type ' + str(type(x)) + ' . ' ValueError: Unexpectedly found an instance of type <class 'keras.models.Sequential'> . Expected a symbolic tensor instance.

一些更多的日志:

ValueError: Layer merge_1 was called with an input that isn't a symbolic tensor. Received type: class 'keras.models.Sequential'. Full input: [keras.models.Sequential object at 0x2b32d518a780, keras.models.Sequential object at 0x2b32d521ee80]. All inputs to the layer should be tensors.

我如何可以合并使用不同的窗口大小这2个连续的模型和像'最大','总和'等功能应用于他们?

+0

您需要合并两个模型的输出层,我不认为您可以合并keras中的编译模型。你应该看看[keras的功能API](https://keras.io/getting-started/functional-api-guide/) – gionni

+0

检查:https://stackoverflow.com/questions/44872982/how-do-i -train-multiple-neural-nets-in-keras –

+0

明白了。我认为我们可以。这值得一试。感谢您的链接 –

回答

5

使用功能API为您带来所有可能性。

使用功能性API时,您需要跟踪输入和输出,而不是仅定义图层。

您定义了一个图层,然后用输入张量调用该图层以获得输出张量。模型和图层可以用完全相同的方式调用。

对于合并层,我更喜欢使用更直观的其他合并层,例如Add()Multiply()Concatenate()

from keras.layers import * 

mergedOut = Add()([model1.output,model2.output]) 
    #Add() -> creates a merge layer that sums the inputs 
    #The second parentheses "calls" the layer with the output tensors of the two models 
    #it will demand that both model1 and model2 have the same output shape 

这同样的想法适用于所有以下层。我们不断更新输出张给它的每一层并获得一个新的输出(如果我们有兴趣在创建新的分支,我们会使用不同的变种每个感兴趣的输出来跟踪他们的):

mergedOut = Flatten()(mergedOut)  
mergedOut = Dense(256, activation='relu')(mergedOut) 
mergedOut = Dropout(.5)(mergedOut) 
mergedOut = Dense(128, activation='relu')(mergedOut) 
mergedOut = Dropout(.35)(mergedOut) 

# output layer 
mergedOut = Dense(5, activation='softmax')(mergedOut) 

现在我们创建了“路径”,现在是创建Model的时候了。创建模型只是像对在其输入张量它开始和它的尽头:

from keras.models import Model 

newModel = Model([model1.input,model2.input], mergedOut) 
    #use lists if you want more than one input or output  

注意,因为这种模式有两个输入端,你在列表中有两个不同的X_training瓦尔训练它:

newModel.fit([X_train_1, X_train_2], Y_train, ....)  

现在,假设你想只有一个输入,并且都MODEL1和MODEL2将采取相同的输入。

功能API允许很容易地创建一个输入张量并将其进料模型(我们称之为模型,好像他们是层):

commonInput = Input(input_shape) 

out1 = model1(commonInput)  
out2 = model2(commonInput)  

mergedOut = Add()([out1,out2]) 

在这种情况下,模型会认为这输入:

oneInputModel = Model(commonInput,mergedOut) 
+0

所以当我们想合并2个模型时,我们不能将这2个模型声明为Sequential()?我们必须使用功能性API。当我调用Concatenate()时,我不断收到我在上面提到的错误。这是正确的,在这种情况下,我们不能使用Sequential()? –

+0

您可以保留顺序模型,没有问题,但最终模型不能顺序执行,这是不可行的。错误消息是关于:“调用图层时不传递张量”。你很可能会传递模型。请注意我的答案中的'model1.output'和'model2.output'张量。 ----'model1'是一个模型,而'model1.output'是一个张量。 –

+0

最终模型是一个功能性'模型',其中包含两个'Sequential'模型和其路径中的一些附加层。 –