函數式模型有一個很好用的應用實例是:編寫擁有多個輸入和輸出的模型。函數式模型使得在復雜網絡中操作巨大的數據流變的簡單。
我們實現下面這樣的模型
from keras.layers import Input, Embedding, LSTM, Dense from keras.models import Model # Headline input: meant to receive sequences of 100 integers, between 1 and 10000. # Note that we can name any layer by passing it a "name" argument. main_input = Input(shape=(100,), dtype='int32', name='main_input') # This embedding layer will encode the input sequence # into a sequence of dense 512-dimensional vectors. x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input) # A LSTM will transform the vector sequence into a single vector, # containing information about the entire sequence lstm_out = LSTM(32)(x)
這里有 兩個知識點
1、embedding層的使用。這里有個背景知識:我們輸入的是100整數,每個整數都是0-1000的。代表的含義是:我們有個1000詞的詞典,輸入的是100詞的標題
然后經過embedding層,進行編碼,輸出512的向量
2、 LSTM(32)返回值是一個model,它可以向layer一樣直接被調用
然后我們插入一個輔助層,它可以使得即使在模型的主損失值很大的時候 ,LSTM和Embedding層也可以得到平滑的訓練
auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)
我們加入一個平滑的輸入 ,把它和LSTM的輸出連接在一起
auxiliary_input = Input(shape=(5,), name='aux_input') x = keras.layers.concatenate([lstm_out, auxiliary_input]) # We stack a deep densely-connected network on top x = Dense(64, activation='relu')(x) x = Dense(64, activation='relu')(x) x = Dense(64, activation='relu')(x) # And finally we add the main logistic regression layer main_output = Dense(1, activation='sigmoid', name='main_output')(x)
這樣,我們的模型就有兩個輸入和兩個輸出
model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])
我們編譯我們的模型,並且給平滑損失一個0.2的權重。可以用列表或者字典定義不同輸出對應損失權重,如果對loss傳入一個數 ,則損失權重會被用於全部的輸出。
model.compile(optimizer='rmsprop', loss='binary_crossentropy', loss_weights=[1., 0.2])
然后fit數據進行訓練
model.fit([headline_data, additional_data], [labels, labels],
epochs=50, batch_size=32)
當然,也可以通過字典來 實現這個目的:
model.compile(optimizer='rmsprop', loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'}, loss_weights={'main_output': 1., 'aux_output': 0.2}) # And trained it via: model.fit({'main_input': headline_data, 'aux_input': additional_data}, {'main_output': labels, 'aux_output': labels}, epochs=50, batch_size=32)