具有不同序列长度的多对多序列预测

时间:2017-03-30 12:17:35

标签: python tensorflow keras lstm recurrent-neural-network

我的问题是在Keras的LSTM图层的前一个时间步长(t_0, t_1, ... t_{n_post-1})预测一系列值(t_{-n_pre}, t_{-n_pre+1} ... t_{-1})

Keras很好地支持以下两种情况:

  • n_post == 1(多对一预测)
  • n_post == n_pre(很多人 预测序列长度相等)

但不是n_post < n_pre

的版本

为了说明我的需要,我使用正弦波构建了一个简单的玩具示例。

多对一模型预测

使用以下模型:

model = Sequential()  
model.add(LSTM(input_dim=1, output_dim=hidden_neurons, return_sequences=False))  
model.add(Dense(1))
model.add(Activation('linear'))   
model.compile(loss='mean_squared_error', optimizer='rmsprop') 

预测看起来像这样: Sine: Many to one LSTM

使用n_pre == n_post

进行多对多模型预测

网络学习使用n_pre == n_post的正弦波非常适合这样的模型:

model = Sequential()  
model.add(LSTM(input_dim=1, output_dim=hidden_neurons, return_sequences=True))  
model.add(TimeDistributed(Dense(1)))
model.add(Activation('linear'))   
model.compile(loss='mean_squared_error', optimizer='rmsprop')  

Sine Many to Many model forecast using n_post==n_pre

使用n_post&lt;多对多模型进行预测n_pre

但现在,假设我的数据如下所示: dataX或输入:(nb_samples, nb_timesteps, nb_features) -> (1000, 50, 1) dataY或输出:(nb_samples, nb_timesteps, nb_features) -> (1000, 10, 1)

经过一些研究后,我找到了一种如何在Keras中处理这些输入尺寸的方法,使用这样的模型:

model = Sequential()  
model.add(LSTM(input_dim=1, output_dim=hidden_neurons, return_sequences=False))  
model.add(RepeatVector(10))
model.add(TimeDistributed(Dense(1)))
model.add(Activation('linear'))   
model.compile(loss='mean_squared_error', optimizer='rmsprop') 

但预测真的很糟糕: Many to many with n_post < n_pre

现在我的问题是:

  • 如何使用n_post < n_pre构建一个不丢失信息的模型,因为它有return_sequences=False
  • 使用n_post == n_pre然后裁剪输出(训练后)对我来说不起作用,因为它仍会尝试适应很多时间步,而只有前几个可以用神经网络预测(其他人没有很好的相关性,会扭曲结果)

1 个答案:

答案 0 :(得分:4)

在Keras Github页面上询问了这个问题之后,我得到了一个答案,我在这里发布了完整的答案。

解决方案是在使用RepeatVector将输出整形到所需的输出步数之后,使用第二个LSTM层。

model = Sequential()  
model.add(LSTM(input_dim=1, output_dim=hidden_neurons, return_sequences=False))  
model.add(RepeatVector(10))
model.add(LSTM(output_dim=hidden_neurons, return_sequences=True))  
model.add(TimeDistributed(Dense(1)))
model.add(Activation('linear'))   
model.compile(loss='mean_squared_error', optimizer='rmsprop')  

预测现在看起来更好,看起来像这样: enter image description here