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'模型和其路徑中的一些附加層。 –