【483】Keras 中 LSTM 與 BiLSTM 語法


參考:Keras-遞歸層Recurrent官方說明

參考:Keras-Bidirectional包裝器官方說明

LSTM(units=32, input_shape=(10, 64))

  • units=32:輸出神經元個數
  • input_shape=(10, 64):輸入數據形狀,10 代表時間序列的長度,64 代表每個時間序列數據的維度

LSTM(units=32, input_dim=64, input_length=10)

  • units=32:輸出神經元個數
  • input_dim=64:每個時間序列數據的維度
  • input_length=10:時間序列的長度

☀☀☀<< 舉例 >>☀☀☀

# as the first layer in a Sequential model
model = Sequential()
model.add(LSTM(32, input_shape=(10, 64)))
# now model.output_shape == (None, 10, 32)
# note: `None` is the batch dimension.

# the following is identical:
model = Sequential()
model.add(LSTM(32, input_dim=64, input_length=10))

# for subsequent layers, not need to specify the input size:
model.add(LSTM(16))

  

  • return_sequences:布爾值,默認False,控制返回類型。若為True則返回整個序列,否則僅返回輸出序列的最后一個輸出

keras.layers.wrappers.Bidirectional(layer, merge_mode='concat', weights=None)

  雙向RNN包裝器 

  參數

  • layer:Recurrent對象
  • merge_mode:前向和后向RNN輸出的結合方式,為sum,mul,concat,ave和None之一,若設為None,則返回值不結合,而是以列表的形式返回

☀☀☀<< 舉例 >>☀☀☀

model = Sequential()
model.add(Bidirectional(LSTM(10, return_sequences=True), input_shape=(5, 10)))
model.add(Bidirectional(LSTM(10)))
model.add(Dense(5))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM