【问题标题】:How to set up Keras LSTM for time series forecasting?如何设置 Keras LSTM 进行时间序列预测?
【发布时间】:2017-06-27 22:20:46
【问题描述】:

我有一个包含 600 个连续点 (x(t), y(t)) 的训练批次,其中 x(t) 是 25 维向量,y(t) 是我的目标 (1 dim)。我想训练一个 LSTM 来预测在给定一些额外的 x(t) [t> 600] 的情况下该系列将如何继续。我尝试了以下模型:

    model = Sequential() 
    model.add(LSTM(128, input_shape = (600,25), batch_size = 1, activation= 'tanh', return_sequences = True)) 
    model.add(Dense(1, activation='linear'))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(trainX, trainY, epochs=20 ,verbose=2) prediction

    prediction = model.predict(testX, batch_size = 1)

拟合效果很好,但我在预测步骤中不断收到以下错误:

    Error when checking : expected lstm_46_input to have shape (1, 600, 25) but got array with shape (1, 10, 25)

我错过了什么?

这是我的形状:

    trainX.shape = (1,600,25)
    trainY.shape = (1,600,1)
    testX.shape = (1,10,25)

【问题讨论】:

    标签: keras lstm


    【解决方案1】:

    根据 Keras 文档,LSTM(或任何 RNN)层的输入应该是形状 (batch_size, timesteps, input_dim),您的输入形状是

    trainX.shape = (1,600,25)

    因此,这意味着对于训练,您只传递一个具有 600 个时间步长和每个时间步长 25 个特征的数据。但我感觉你实际上有 600 个训练数据,每个数据有 25 个时间步每个时间步 1 个特征。我猜你的输入形状(trainX)应该是600 x 25 x 1。训练目标(trainY)应该是600 x 1 如果我的假设是正确的,那么你的测试数据应该是10 x 25 x 1。第一个 LSTM 层应该写成

        model.add(LSTM(128, input_shape = (25,1), batch_size = 1, activation= 'tanh', return_sequences = False)) 
    

    【讨论】:

    • 如果 y 形状正确,他可能需要TimeDistributed(Dense(1)),因为他每个时间步都有一个输出。
    • 对于 2D 输入,输入为 (batch_size, input_size),input_shape = (25,1) 表示 batch_size=25,而不是 timesteps。
    【解决方案2】:

    如果您的训练数据实际上是 (1,600,25),这意味着您将 LSTM 反馈展开 600 次。第一个输入对第 600 个输入有影响。如果这是您想要的,您可以使用 Keras 函数“pad_sequences”将附加零添加到测试矩阵,使其具有形状 (1,600,25)。网络应该预测零,您需要在 testY 中添加 590 个零。

    如果您只想说 10 个之前的时间步来影响您当前的 Y 预测,那么您需要将您的 trainX 变成形状 (590,10,25)。输入行将类似于:

    model.add(LSTM(n_hid, stateful=True, return_sequences=False, batch_input_shape=(1,nTS,x_train.shape[2])))
    

    以您想要的形式获取它的处理可能是这样的:

    def formatTS(XX, yy, window_length):
    x_train = np.zeros((XX.shape[0]-window_length,window_length,XX.shape[1]))
    for i in range(x_train.shape[0]):
        x_train[i] = XX[i:i+window_length,:]
    y_train = yy[window_length:]
    return x_train, y_train
    

    那么你的测试就可以正常工作了,因为它已经是 (1,10,25) 的形状了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2017-10-03
      • 2018-09-04
      • 2021-11-09
      • 2021-04-21
      • 2018-12-28
      相关资源
      最近更新 更多