1 入門html
3 共享層函數
函數式模型有一個很好用的應用實例是:編寫擁有多個輸入和輸出的模型。函數式模型使得在複雜網絡中操做巨大的數據流變的簡單。編碼
咱們實現下面這樣的模型spa
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)
這裏有 兩個知識點code
一、embedding層的使用。這裏有個背景知識:咱們輸入的是100整數,每一個整數都是0-1000的。表明的含義是:咱們有個1000詞的詞典,輸入的是100詞的標題orm
而後通過embedding層,進行編碼,輸出512的向量htm
二、 LSTM(32)返回值是一個model,它能夠向layer同樣直接被調用blog
而後咱們插入一個輔助層,它可使得即便在模型的主損失值很大的時候 ,LSTM和Embedding層也能夠獲得平滑的訓練get
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)