如何在keras中堆叠多个lstm? [英] How to stack multiple lstm in keras?
问题描述
我正在使用深度学习库keras,并尝试在没有运气的情况下堆叠多个LSTM. 下面是我的代码
I am using deep learning library keras and trying to stack multiple LSTM with no luck. Below is my code
model = Sequential()
model.add(LSTM(100,input_shape =(time_steps,vector_size)))
model.add(LSTM(100))
上面的代码在第三行Exception: Input 0 is incompatible with layer lstm_28: expected ndim=3, found ndim=2
The above code returns error in the third line Exception: Input 0 is incompatible with layer lstm_28: expected ndim=3, found ndim=2
输入X是形状的张量(100,250,50).我在tensorflow后端上运行keras
The input X is a tensor of shape (100,250,50). I am running keras on tensorflow backend
推荐答案
您需要在第一层添加return_sequences=True
,以便其输出张量具有ndim=3
(即批处理大小,时间步长,隐藏状态).
You need to add return_sequences=True
to the first layer so that its output tensor has ndim=3
(i.e. batch size, timesteps, hidden state).
请参见以下示例:
# 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'))
来自: https://keras.io/getting-started/sequential-model -guide/(搜索"stacked lstm")
From: https://keras.io/getting-started/sequential-model-guide/ (search for "stacked lstm")
这篇关于如何在keras中堆叠多个lstm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!