python - 在 Keras 中实现 LSTM

标签 python keras neural-network deep-learning lstm

非常感谢您帮助我在 Keras 中创建 LSTM 模型。我的输入是一个二维 numpy 数组,其中的行是长度相等的数字时间序列。 我编写了以下几行代码来创建一个 LSTM 模型,该模型在没有嵌入层的情况下对原始数据进行操作:

lstm_clf = Sequential()
lstm_clf.add(LSTM(100, input_shape=(X_train[0], X_train[1]))
lstm_clf.add(Dense(7, activation='softmax'))
lstm_clf.compile(loss='sparse_categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])

当我到达拟合阶段时,出现以下错误:“ValueError:检查输入时出错:预期 lstm_11_input 有 3 个维度,但得到的数组形状为 (100, 289)”。

有人可以向我解释一下我做错了什么以及如何修复代码吗?它一定与输入形状有关,但我不知道它是什么。

非常感谢您的帮助,

亚历山大。

最佳答案

我找到了答案。正确的代码如下:

lstm_clf = Sequential()

lstm_clf.add(LSTM(100, input_shape=(1, max_seq_len))) lstm_clf.add(密集(7,激活='softmax')) lstm_clf.compile(loss='sparse_categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])

我的数据也必须重新调整,如下所示:

X_seqs_train = reshape(X_seqs_train, (X_seqs_train.shape[0], 1, X_seqs_train.shape[1]))

X_seqs_test = reshape(X_seqs_test, (X_seqs_test.shape[0], 1, X_seqs_test.shape 1 ))

这些更改解决了问题。找到了一条线索here .

关于python - 在 Keras 中实现 LSTM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611396/

相关文章:

python - fit_generator() 在 Spyder 中不起作用

python - 我在哪里调用 Keras 中的 BatchNormalization 函数?

neural-network - 有没有办法调用 Spark ML MultilayerPerceptronClassificationModel 的 'predict' 方法?

python - 通过仅跳过空白行来读取 excel 文件 (pd.read_excel())

python - 类型错误 : Failed to convert object of type Sparsetensor to Tensor

python - 不确定是什么导致 Selenium 超时

python - 未实现错误: Layer ModuleWrapper has arguments in `__init__` and therefore must override `get_config`

python - Keras——嵌入层的输入形状

python - Python可以调用DLL中的Delphi函数吗?

python - 字典数据未正确附加到另一个字典