Isa*_*Isa 7 python lstm keras tensorflow 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')
Run Code Online (Sandbox Code Playgroud)
使用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')
Run Code Online (Sandbox Code Playgroud)
使用n_post <n_pre进行多对多模型预测
但现在,假设我的数据如下所示:dataX或input:(nb_samples, nb_timesteps, nb_features) -> (1000, 50, 1)
dataY或output:(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')
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:
n_post < n_pre
不丢失信息的模型,因为它有一个return_sequences=False
?n_post == n_pre
然后裁剪输出(训练后)对我来说不起作用,因为它仍然会尝试适应很多时间步,而只有前几个可以用神经网络预测(其他的不是很好地相关并会扭曲结果)在 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')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2229 次 |
最近记录: |