【发布时间】: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