python - 具有keras和多个序列的时间序列预测

标签 python deep-learning keras

我理解在一个序列上keras中的stateful LSTM prediction example。这个例子有一个5万次观测的序列。
我的问题:
如果你想训练5万次观测的多个序列呢?假设一个以不同的值开始/结束并且有稍微不同的行为?
如何修改示例以增加预测时间步长?
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)

最佳答案

看看菲利普·雷米的这篇文章。它解释了如何在keras中使用有状态lstms。

关于python - 具有keras和多个序列的时间序列预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39758190/

相关文章:

java - 使用 deeplearning4j 加载 nlp 模型的问题

deep-learning - 预计来自 model.fit() 的 Keras 验证损失

python - 将 csv 列连接到字符串并打印特定值

python - 如何使用 Python/Azure Databricks 将成员添加到 Azure AD 组中?

deep-learning - Caffe - 如何使用 pycaffe 更改 caffe 权重的数据类型?

machine-learning - 为什么在 Caffe 的示例中要交换 RGB channel ?

machine-learning - 全局池操作的池大小是多少?

python - 构建图像分类的初始架构。喀拉斯。值错误

python - 协程外的Aiohttp ClientSession

python - 排除名称中带有空格的整个目录