[英]Input Shape Error in Second-layer (but not first) of Keras LSTM
I am trying to build an LSTM model, working off the documentation example at https://keras.io/layers/recurrent/我正在尝试构建一个 LSTM 模型,在https://keras.io/layers/recurrent/ 处理文档示例
from keras.models import Sequential
from keras.layers import LSTM
The following three lines of code (plus comment) are taken directly from the documentation link above:以下三行代码(加上注释)直接取自上面的文档链接:
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))
ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2
ValueError:输入 0 与层 lstm_2 不兼容:预期 ndim=3,发现 ndim=2
I get that error above after executing the second model.add() statement, but before exposing the model to my data, or even compiling it.在执行第二个 model.add() 语句后,但在将模型暴露给我的数据之前,甚至编译它之前,我得到了上面的错误。
What am I doing wrong here?我在这里做错了什么? I'm using Keras 1.2.1.
我正在使用 Keras 1.2.1。
Just upgraded to current 1.2.2, still having same issue.刚刚升级到当前的 1.2.2,仍然有同样的问题。
Thanks to patyork for answering this on Github :感谢 patyork 在Github上回答这个问题:
the second LSTM layer is not getting a 3D input that it expects (with a shape of (batch_size, timesteps, features). This is because the first LSTM layer has (by fortune of default values) return_sequences=False, meaning it only output the last feature set at time t-1 which is of shape (batch_size, 32), or 2 dimensions that doesn't include time.
第二个 LSTM 层没有得到它期望的 3D 输入(形状为(batch_size、timesteps、features)。这是因为第一个 LSTM 层具有(默认值)return_sequences=False,这意味着它只输出t-1 时刻的最后一个特征集,其形状为 (batch_size, 32),或不包括时间的 2 个维度。
So to offer a code example of how to use a stacked LSTM to achieve many-to-one (return_sequences=False) sequence classification, just make sure to use return_sequences=True on the intermediate layers like this:因此,要提供如何使用堆叠 LSTM 实现多对一(return_sequences=False)序列分类的代码示例,只需确保在中间层使用 return_sequences=True ,如下所示:
model = Sequential()
model.add(LSTM(32, input_dim=64, input_length=10, return_sequences=True))
model.add(LSTM(24, return_sequences=True))
model.add(LSTM(16, return_sequences=True))
model.add(LSTM(1, return_sequences=False))
model.compile(optimizer = 'RMSprop', loss = 'categorical_crossentropy')
(no errors) (没有错误)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.