@DanielAdiwardana的答案的詳細說明。我們需要為除最后一層之外的所有LSTM層添加return_sequences = True。
將此標志設置為True可讓Keras知道LSTM輸出應包含所有歷史生成的輸出以及時間戳(3D)。 因此,下一個LSTM層可以進一步處理數據。
如果此標志為假,則LSTM僅返回最后一個輸出(2D)。 這樣的輸出對于另一個LSTM層來說還不夠好。
# expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(LSTM(32, return_sequences=True,
input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 32
model.add(LSTM(32, return_sequences=True)) # returns a sequence of vectors of dimension 32
model.add(LSTM(32)) # return a single vector of dimension 32
model.add(Dense(10, activation='softmax'))
在側面注意:::添加了最后一個密集層,以獲取用戶所需格式的輸出。 這里的Dense(10)表示將使用softmax激活生成10個不同的類輸出。
如果您將LSTM用于時間序列,則應具有Dense(1)。 因此只給出一個數字輸出。