【问题标题】:Implementing LSTM in Keras在 Keras 中实现 LSTM
【发布时间】:2019-07-03 19:35:27
【问题描述】:

感谢您在 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: Error when checks input: expected lstm_11_input to have 3 dimensions, but got array with shape (100, 289)”。

谁能向我解释我做错了什么以及如何修复代码。它一定和输入的形状有关,但我不知道它是什么。

非常感谢您的帮助,

亚历山大。

【问题讨论】:

    标签: python keras neural-network deep-learning lstm


    【解决方案1】:

    我找到了答案。这是正确的代码:

    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.shape1))

    这些更改解决了问题。找到了导致答案的线索here

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2018-07-21
      • 2019-02-18
      • 2021-03-08
      相关资源
      最近更新 更多