【问题标题】:Error on LSTM input and target shapesLSTM 输入和目标形状的错误
【发布时间】:2018-07-23 18:14:14
【问题描述】:

我正在使用 Keras 的 LSTM 解决时间序列预测问题。我已经在 TimeseriesGenerator 上完成了数据集的整形和批处理,但我不确定我是否得到了我想要的结果,所以我试图让它保持简单的调试。

我的数据由 1 个具有 100 个时间步长和 1 个特征的样本组成,我正在使用最后 30 个值进行测试。我无法弄清楚训练 x/y 数据集的形状有什么问题。

train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=30, shuffle=False)
train_x = np.reshape(train_x, (1, train_x.shape[0], train_x.shape[1]))

model = Sequential()
model.add(LSTM(units = 16, input_shape=(train_x.shape[1],train_x.shape[2])))
model.add(Dense(units = 1, activation = 'linear'))
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
history = model.fit(train_x, train_y, validation_split=0.1, batch_size = 16, epochs = 300, shuffle=False,verbose=1)  

ValueError:输入数组的样本数应与目标数组相同。找到 1 个输入样本和 69 个目标样本。

【问题讨论】:

    标签: python keras lstm


    【解决方案1】:

    您对特征的重塑创建了一个形状数组 (1, xshape[0], xshape[1])。 这意味着您只有 1 个样本,您的 y 有 69 个样本。两者的大小必须匹配。如果你有 1 个输入 X,你必须通过 1 个 Y。

    【讨论】:

    • 是的,我了解我是如何塑造我的数据的,但我不希望每个样本有 1 个时间步长的 69 个样本,因为它违背了使用 LSTM 的目的。我只想要 69 个时间步长的 1 个样本。我应该如何重塑 x 和 y ?
    • 我不知道你的数据是什么样子的。好像你有 69 个 y 值?
    【解决方案2】:

    所以你的数据看起来像下面的代码 8 个时间步和 1 个样本

    arr=array([[[1],[2],[3],[4],[5],[6],[7],[8]]])
    

    但是你有 100 个时间步,而不是上面例子中的 8 个时间步 因此,如果我想重塑上面的数组以使其成为 4 个样本,并且每个样本是 2 个时间步长,则代码将是:

    reshaped_arr=arr.reshape(4,2,1)
    

    使用上面的例子作为指导

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      相关资源
      最近更新 更多