Keras LSTM第二层(但不是第一层)中的输入形状误差 [英] Input Shape Error in Second-layer (but not first) of Keras LSTM

查看:315
本文介绍了Keras LSTM第二层(但不是第一层)中的输入形状误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建LSTM模型,以处理 https://keras.io上的文档示例. /layers/recurrent/

I am trying to build an LSTM model, working off the documentation example at 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:输入0与lstm_2层不兼容:预期 ndim = 3,找到的ndim = 2

ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2

在执行第二个model.add()语句之后,但在将模型暴露给我的数据甚至编译它之前,我得到了上面的错误.

I get that error above after executing the second model.add() statement, but before exposing the model to my data, or even compiling it.

我在这里做错了什么?我正在使用Keras 1.2.1.

What am I doing wrong here? I'm using Keras 1.2.1.

只是升级到当前的1.2.2,仍然存在相同的问题.

Just upgraded to current 1.2.2, still having same issue.

推荐答案

感谢patyork在 Github :

Thanks to patyork for answering this on Github:

第二个LSTM层没有获得预期的3D输入(形状为(batch_size,时间步长,特征).这是因为第一个LSTM层具有(按默认值计算)return_sequences = False,这意味着仅在时间t-1处输出形状为(batch_size,32)或2个不包含时间的尺寸的最后一个特征集.

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实现多对一(return_sequences = False)序列分类,只需确保在这样的中间层上使用return_sequences = True即可:

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:

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')

(没有错误)

这篇关于Keras LSTM第二层(但不是第一层)中的输入形状误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆