【问题标题】:Many to many sequence prediction with different sequence length具有不同序列长度的多对多序列预测
【发布时间】:2017-08-24 08:47:23
【问题描述】:

我的问题是使用 Keras 的 LSTM 层在给定先前时间步 (t_{-n_pre}, t_{-n_pre+1} ... t_{-1}) 的情况下预测一系列值 (t_0, t_1, ... t_{n_post-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') 

预测如下所示:

使用 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')  

使用 n_post

进行多对多模型预测

但是现在,假设我的数据如下所示: 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') 

但是预测真的很糟糕:

现在我的问题是:

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

【问题讨论】:

  • 您要预测提前多少步?
  • 在这个特定的例子中,10。但它可能是这样的:1

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


【解决方案1】:

在 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')  

预测现在看起来更好,看起来像这样:

【讨论】:

猜你喜欢
  • 2018-04-03
  • 2020-11-04
  • 2020-05-30
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2023-03-14
相关资源
最近更新 更多