【问题标题】:Concatenate additional features after LSTM layer for Time Series Forecasting在 LSTM 层之后连接其他特征以进行时间序列预测
【发布时间】:2019-03-05 21:41:21
【问题描述】:

我有以下数据集:

                Feature 1   Feature 2 ...  Feature to Predict
2015-01-01         1000          8                 12
2015-01-02         1200          2                 22
2015-01-03         4000          4                 51
2015-01-04         2000          8                 33
2015-01-05         1000          5                 14

我想使用之前的 n 时间戳来预测时间 t + 1 的最后一个特征(“要预测的特征”)。为此,我使用了一个多变量 LSTM,它使用从 t-nt 的数据进行训练。

事实上,我也有可能在我想要预测的时间 't+1' 内获得其他特征(特征 1、特征 2 ...)。

我想做的是在LSTM 层之后和Dense 层之前添加这些附加功能,并将它们用于我的“预测特征”的预测中。

现在我的代码,没有附加功能,但只有 't-n' 到 't' 功能,看起来像这样:

mdl = Sequential()
# create and fit the LSTM network
mdl.addLSTM(neuronsl1,activation = 'tanh' ,return_sequences=True, input_shape=(lags,n_features))
mdl.add(Dropout(0.2))
mdl.addLSTM(neuronsl2,activation = 'tanh' , input_shape=(lags,n_features))
mdl.add(Dropout(0.2))

--------->>> At this point i would like to add the additional features at time 't + 1'

mdl.add(Dense(neuronsl3))
mdl.add(Dense(neuronsl4))
mdl.add(Dense(1))

关于如何做到这一点的任何建议?

【问题讨论】:

    标签: python tensorflow keras time-series lstm


    【解决方案1】:

    我想我误解了你的问题。你提到你想要:

    获取我想要预测的时间 't+1' 的其他特征(特征 1、特征 2 ...)。

    您的数据集中有“特征 1”和“特征 2”。但是在标题中您提到了“连接附加功能”。因此,如果您不仅要在时间步 t+1 预测“要预测的特征”,还要预测“特征 1”、“特征 2”等,那么:

    • 只需将最后一个 Dense 层中的单元数设置为您想要预测的特征数即可达到您想要的效果。另外,如果您只想获得时间步长t+1,那么您需要在最后一个 LSTM 层中设置return_sequences=False(当然这是默认情况)。那是因为the Dense layer is applied on the last axis 而不是一次全部数据。

    但是,如果您想将最后一个 LSTM 层的输出与其他特征(您需要将其作为模型的输入提供)连接,您需要使用 Keras functional API 并使用 concatenate 函数(或等效Concatenate层):

    mdl_input1 = Input(shape=(lags,n_features))
    
    x = LSTM(neuronsl1, activation='tanh', return_sequences=True)(mdl_input1)
    x = Dropout(0.2)(x)
    x = LSTM(neuronsl2, activation='tanh')(x)
    x = Dropout(0.2)(x)
    
    mdl_input2 = Input(shape=(shape_of_features))
    
    concat = concatenate([x, mdl_input2])
    
    x = Dense(neuronsl3)(x)
    x = Dense(neuronsl4)(x)
    output = Dense(1)(x)
    
    model = Model([mdl_input1, mdl_input2], output)
    
    # compile the model ...
    
    model.fit([input_array_for_lstm, input_array_additional_features], y_train, ...)
    

    【讨论】:

    • return_sequences=True 只是一个错误,因为我复制并粘贴了第一行。我想你并没有完全理解我想要做什么。我要预测的特征只是时间 t+1 的一个('要预测的特征'),我想做的是在时间 t+ 向我的模型添加其他特征('特征 1、2 ...) 1 在做出预测之前
    • @MarcoMiglionico 我明白了。我正在编辑我的答案。
    • @MarcoMiglionico 更新了我的答案。
    • 假设我的附加输入有6个特征,mdl_input2 = Input(shape=(shape_of_features))的形状应该是什么?那么我应该在 shape_of_features 上插入什么
    • @MarcoMiglionico 把(6,)放在那里,即shape=(6,)
    猜你喜欢
    • 1970-01-01
    • 2017-10-03
    • 2021-01-14
    • 1970-01-01
    • 2018-09-22
    • 2019-12-26
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多