【问题标题】:ValueError: Error when checking input: expected lstm_6_input to have shape (87482, 1) but got array with shape (87482, 3)ValueError:检查输入时出错:预期 lstm_6_input 具有形状 (87482, 1) 但得到的数组具有形状 (87482, 3)
【发布时间】:2019-11-20 06:28:07
【问题描述】:

X_train 数据维度为 87482, 3。

但我在使用以下代码运行时出错。错误是:

ValueError: 检查输入时出错:预期 lstm_6_input 的形状为 (87482, 1) 但得到的数组的形状为 (87482, 3)

我的代码是:

model = Sequential()
#model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
model.add(LSTM(units=3, input_shape=(X_train_rnn.shape[1],1),return_sequences=True))
model.add(LSTM(3, return_sequences=True))  # returns a sequence of vectors of dimension 32
model.add(LSTM(3))

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_train_rnn, y_train, epochs=2, batch_size=32)
# Final evaluation of the model
scores = model.evaluate(X_test_rnn, y_test, verbose=0)

【问题讨论】:

    标签: python neural-network lstm recurrent-neural-network


    【解决方案1】:

    input_shape=(X_train.shape[1], 1) 应该修复错误,但可能不是整个问题:每个序列有多长

    • LSTM 完整输入形状 (batch_shape) 是 (batch_size, timesteps, channels) - 或者,等效地,(samples, timesteps, features)
    • batch_size=32 在您的 .fit() 中,但如果 X_train 维度是 (87472, 3),您是否有 87472 每个长度为 3 (timesteps=3) 的样本(序列)?如果是这样,您将需要input_shape=(3, 1)(单变量数据),这就是您通过(X_train.shape[1], 1) 获得的
    • 如果不是你的timesteps=87462,你将需要input_shape=(87462, 3) - 但这是一个非常糟糕的主意,因为LSTMs 为timesteps > 0 奋斗

    我不知道X_train_rnn有什么形状,所以我用X_train写了答案。欢迎澄清。

    【讨论】:

      猜你喜欢
      • 2020-09-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2021-02-26
      • 2019-08-21
      • 2020-12-05
      • 2018-01-01
      相关资源
      最近更新 更多