使用LSTM和keras进行时间序列预测的分类变量

时间:2018-03-26 18:09:11

标签: python deep-learning keras lstm categorical-data

我有一个LSTM模型(keras),它接收206变量的4值作为输入,并预测这些变量3的未来past_time_steps = 6 future_time_steps = 4 inputs = Input(shape=(20,past_time_steps)) m = LSTM(hid, return_sequences=True)(inputs) m = Dropout(0.5)(m) m = LSTM(hid)(m) m = Dropout(0.5)(m) outputA = Dense(future_time_steps, activation='linear', W_constraint=nonneg())(m) outputB = Dense(future_time_steps, activation='linear', W_constraint=nonneg())(m) outputC = Dense(future_time_steps, activation='linear', W_constraint=nonneg())(m) m = Model(inputs=[inputs], outputs=[outputA, outputB, outputC]) m.compile(optimizer='adam', loss='mae') m.fit(x,[y1,y2, y2]) 值。换句话说,我有6个时间序列,我试图用过去的20个值来预测它们的未来值。基本代码是:

(500,20,6)

因此,输入是一个形状为500的numpy矩阵,其中0,1,2,3,4,5表示样本数(例如训练时间序列)。

现在,我有了新的数据,所以对于每个时间序列,我都有一个分类变量(可以有6个值:(500,21,6))。如何将此信息添加到模型中?我可以添加另一个使用此变量的图层吗?我应该在时间序列的开头/结尾填充此变量,以便我有一个形状为import random x = list(range(1,101)) indices = list(range(len(x))) change = set(random.sample(indices, 10)) #randomly select 10 indices to change x = [j+random.choice([-1,1]) if i in change else j for i,j in enumerate(x)] 的输入矩阵吗?

2 个答案:

答案 0 :(得分:0)

One_hot_encoded分类变量,并以与其他时间数据相同的方式对其进行预处理。您的时间步不受此新数据的影响。受影响的只是变量数。

答案 1 :(得分:0)

该主题可能会让您感兴趣:Adding Features To Time Series Model LSTM

您基本上有3种可能的方式:

让我们以来自两个不同城市的天气数据为例:巴黎和旧金山。您想根据历史数据预测下一个温度。但同时,您预计天气会因城市而异。您可以:

  • 在开始或结束时(丑陋!)将辅助功能与时间序列数据组合在一起。
  • 将辅助要素与RNN层的输出连接在一起。由于RNN层看不到此辅助信息,因此属于RNN后期调整。
  • 或者仅使用学习到的条件表示形式(例如巴黎或旧金山)来初始化RNN状态。

我写了一个库,以辅助输入为条件。它抽象了所有复杂性,并被设计为尽可能易于使用:

https://github.com/philipperemy/cond_rnn/

实现是在tensorflow(> = 1.13.1)和Keras中实现的。

希望有帮助!

相关问题