具有keras和多个序列的时间序列预测 [英] Time series prediction with keras and multiple sequences
本文介绍了具有keras和多个序列的时间序列预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我了解Keras上的状态LSTM预测示例一个序列.该示例包含一个50k观测值的序列.
I understand the stateful LSTM prediction example in Keras on a single sequence. That example has one sequence of 50k observations.
我的问题:
- 如果您要训练多个50k观测序列,该怎么办?说一个以不同的值开始/结束并且行为略有不同的人吗?
- 如何修改示例以增加预测时间步长?
- LSTM对这种事情有什么好处吗?
完全可复制的示例,具有3个均值回复时间序列并预测20个步骤.
Fully replicable example with 3 mean-reverting time series and predicting 20 steps out.
# generate random data
import statsmodels.api as sm
import numpy as np
import pandas as pd
cfg_t_total = 25000
cfg_t_step = 20
cfg_batch_size = 100
np.random.seed(12345)
arparams = np.array([.75, -.25])
maparams = np.array([.65, .35])
ar = np.r_[1, -arparams] # add zero-lag and negate
ma = np.r_[1, maparams] # add zero-lag
y0 = sm.tsa.arma_generate_sample(ar, ma, cfg_t_total)
y1 = sm.tsa.arma_generate_sample(ar, ma, cfg_t_total)
y2 = sm.tsa.arma_generate_sample(ar, ma, cfg_t_total)
df=pd.DataFrame({'a':y0,'b':y1,'c':y2})
df.head(100).plot()
df.head(5)
# create training data format
X = df.unstack()
y = X.groupby(level=0).shift(-cfg_t_step)
idx_keep = ~(y.isnull())
X = X.ix[idx_keep]
y = y.ix[idx_keep]
from keras.models import Sequential
from keras.layers import Dense, LSTM
# LSTM taken from https://github.com/fchollet/keras/blob/master/examples/stateful_lstm.py
# how to do this...?!
print('Creating Model')
model = Sequential()
model.add(LSTM(50,
batch_input_shape=(cfg_batch_size, cfg_t_step, 1),
return_sequences=True,
stateful=True))
model.add(LSTM(50,
batch_input_shape=(cfg_batch_size, cfg_t_step, 1),
return_sequences=False,
stateful=True))
model.add(Dense(1))
model.compile(loss='mse', optimizer='rmsprop')
model.fit(X, y, batch_size=cfg_batch_size, verbose=2, validation_split=0.25, nb_epoch=1, shuffle=False)
推荐答案
查看此博客Philippe Remy的帖子.它说明了如何在keras中使用有状态LSTM.
Check out this blog post by Philippe Remy. It explains how to use stateful LSTMs in keras.
这篇关于具有keras和多个序列的时间序列预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文