用keras和多序列进行时间序列预测

时间:2016-09-28 21:45:33

标签: python deep-learning keras

我理解Keras中stateful LSTM prediction example的单个序列。该示例具有一个50k观测序列。

我的问题:

  • 如果您想训练多个50k观测序列怎么办?假设一个以不同的值开始/结束并且行为略有不同?
  • 如何修改示例以增加预测时间步骤?
  • LSTM对于那种事情是否有任何好处?

具有3个均值回复时间序列并预测20步的完全可复制示例。

# 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)

1 个答案:

答案 0 :(得分:-2)

由Philippe Remy查看此blog post。它解释了如何在keras中使用有状态LSTM。