Python 在LSTM层之后连接其他特征以进行时间序列预测
我有以下数据集:Python 在LSTM层之后连接其他特征以进行时间序列预测,python,tensorflow,keras,time-series,lstm,Python,Tensorflow,Keras,Time Series,Lstm,我有以下数据集: 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
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-n
到t
的数据进行训练
事实上,我也有可能获得我想要预测的时间't+1'的其他特征(特征1,特征2…)
我想做的是在LSTM
层之后和densite
层之前添加这些附加功能,并将它们用于我的“Feauture to Predict”预测
现在,我的代码没有附加功能,但只包含“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))
有什么建议吗?我想我误解了你的问题。您提到您想: 获取我想要预测的时间“t+1”的其他特征(特征1、特征2…) 数据集中有“功能1”和“功能2”。但在标题中,您提到了“连接其他功能”。因此,如果您想在时间步
t+1
不仅预测“要预测的功能”,还预测“功能1”、“功能2”等,那么:
- 只需将最后一个密集层中的单元数设置为您想要预测的特征数,即可实现您想要的效果。另外,如果您只想获得timestep
,则需要在最后一个LSTM层中设置t+1
(当然这是默认情况)。这是因为,并不是一下子就对整个数据进行分析return\u sequences=False
return\u sequences=True
只是一个错误,因为我复制并粘贴了第一行。我想你不太明白我到底想做什么。我想预测的特征在t+1时只有一个(‘要预测的特征’),我想做的是在进行预测之前,在我的模型中添加t+1时的其他特征(‘特征1,2…)prediction@MarcoMiglionico我明白了。我正在编辑我的答案。@MarcoMiglionico更新了我的答案。假设我的额外输入有6个功能,那么mdl\u input2=input(shape=(shape\u of\u features))的形状应该是什么?那么我应该在_features@MarcoMiglionico只要把(6,)
放在那里,即形状=(6,)
。
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, ...)