Python 使用Keras构建LSTM单元

Python 使用Keras构建LSTM单元,python,keras,recurrent-neural-network,lstm,Python,Keras,Recurrent Neural Network,Lstm,我正在尝试为文本生成构建一个RNN。我一直在建我的LSTM手机。数据的形状如下-X是dim(908092700)的输入稀疏矩阵,Y是维度(90809,27)的输出矩阵。下面是我定义LSTM单元的代码- model = Sequential() model.add(LSTM(128, input_shape=(X.shape[0], X.shape[1]))) model.add(Dropout(0.2)) model.add(Dense(Y.shape[1], activation='softm

我正在尝试为文本生成构建一个RNN。我一直在建我的LSTM手机。数据的形状如下-X是dim(908092700)的输入稀疏矩阵,Y是维度(90809,27)的输出矩阵。下面是我定义LSTM单元的代码-

model = Sequential()
model.add(LSTM(128, input_shape=(X.shape[0], X.shape[1])))
model.add(Dropout(0.2))
model.add(Dense(Y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
我的理解是,输入_形状应该是输入矩阵的维数,密集层应该是每次观测的输出大小,在这种情况下为27。但是,我得到了以下错误-

Exception: Error when checking model input: expected lstm_input_3 to have 3 dimensions, but got array with shape (90809, 2700)
Exception: Error when checking model input: expected lstm_input_7 to have shape (None, 90809, 2700) but got array with shape (90809, 2700, 1)
我不知道出了什么问题。有人能帮我弄清楚为什么lstm_输入需要3维空间吗

我也尝试了以下方法-

X= np.reshape(np.asarray(dataX), (n_patterns, n_vocab*seq_length,1))
Y=np.reshape(np.asarray(dataY), (n_patterns, n_vocab,1))
这给了我以下的错误-

Exception: Error when checking model input: expected lstm_input_3 to have 3 dimensions, but got array with shape (90809, 2700)
Exception: Error when checking model input: expected lstm_input_7 to have shape (None, 90809, 2700) but got array with shape (90809, 2700, 1)

任何帮助都将不胜感激。谢谢

您应该了解
input\u shape
batch\u input\u shape
input\u dim
之间的区别

对于
input\u-shape
,我们不需要定义
batch\u-size
。这就是LSTM层的外观

model.add(LSTM(128, input_shape=(X.shape[1], 1)))