我理解Keras中stateful LSTM prediction example的单个序列。该示例具有一个50k观测序列。
我的问题:
具有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)