6 种用 LSTM 做时间序列预测的模型结构 - Keras 实现

 

6 种用 LSTM 做时间序列预测的模型结构 - Keras 实现

6 种用 LSTM 做时间序列预测的模型结构 - Keras 实现

XI YANG

XI YANG

学习

59 人赞同了该文章

LSTM(Long Short Term Memory Network)长短时记忆网络,是一种改进之后的循环神经网络,可以解决 RNN 无法处理长距离的依赖的问题,在时间序列预测问题上面也有广泛的应用。

 

 

今天我们根据问题的输入输出模式划分,来看一下几种时间序列问题所对应的 LSTM 模型结构如何实现。

 

 


1. Univariate

 

 

Univariate 是指:

input 为多个时间步, output 为一个时间的问题。

数例:

训练集:
X,          y
10, 20, 30      40
20, 30, 40      50
30, 40, 50      60
…


预测输入:
X,
70, 80, 90

模型的 Keras 代码:

# define model【Vanilla LSTM】

model = Sequential()
model.add( LSTM(50,  activation='relu',  input_shape = (n_steps, n_features)) )
model.add( Dense(1) )
model.compile(optimizer='adam', loss='mse')

n_steps = 3
n_features = 1

其中:

n_steps 为输入的 X 每次考虑几个时间步 n_features 为每个时间步的序列数

这个是最基本的模型结构,我们后面几种模型会和这个进行比较。


2. Multiple Input

 

 

Multiple Input 是指:

input 为多个序列, output 为一个序列的问题。

数例:

训练集:
X,       y
[[10 15]
 [20 25]
 [30 35]] 65
[[20 25]
 [30 35]
 [40 45]] 85
[[30 35]
 [40 45]
 [50 55]] 105
[[40 45]
 [50 55]
 [60 65]] 125
…


预测输入:
X,
80,  85
90,  95
100,     105

即数据样式为:

in_seq1: [10, 20, 30, 40, 50, 60, 70, 80, 90]
in_seq2: [15, 25, 35, 45, 55, 65, 75, 85, 95]

out_seq: [in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]

模型的 Keras 代码:

# define model【Vanilla LSTM】
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

n_steps = 3
# 此例中 n features = 2,因为输入有两个并行序列
n_features = X.shape[2]

其中:

n_steps 为输入的 X 每次考虑几个时间步 n_features 此例中 = 2,因为输入有两个并行序列

和 Univariate 相比:

模型的结构代码是一样的,只是在 n_features = X.shape[2],而不是 1.


3. Multiple Parallel

 

 

Multiple Parallel 是指:

input 为多个序列, output 也是多个序列的问题。

数例:

训练集:
X,          y
[[10 15 25]
 [20 25 45]
 [30 35 65]] [40 45 85]
[[20 25 45]
 [30 35 65]
 [40 45 85]] [ 50  55 105]
[[ 30  35  65]
 [ 40  45  85]
 [ 50  55 105]] [ 60  65 125]
[[ 40  45  85]
 [ 50  55 105]
 [ 60  65 125]] [ 70  75 145]
…


预测输入:
X,
70, 75, 145
80, 85, 165
90, 95, 185

模型的 Keras 代码:

# define model【Vanilla LSTM】
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps, n_features)))
model.add(Dense(n_features))
model.compile(optimizer='adam', loss='mse')

n_steps = 3
# 此例中 n features = 3,因为输入有3个并行序列
n_features = X.shape[2]

其中:

n_steps 为输入的 X 每次考虑几个时间步 n_features 此例中 = 3,因为输入有 3 个并行序列

和 Univariate 相比:

模型结构的定义中,多了一个 return_sequences=True,即返回的是序列, 输出为 Dense(n_features),而不是 1.


4. Multi-Step

 

 

Multi-Step 是指:

input 为多个时间步, output 也是多个时间步的问题。

数例:

训练集:
X,          y
[10 20 30] [40 50]
[20 30 40] [50 60]
[30 40 50] [60 70]
[40 50 60] [70 80]
…


预测输入:
X,
[70, 80, 90]

模型的 Keras 代码:

# define model【Vanilla LSTM】
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(100, activation='relu'))
model.add(Dense(n_steps_out))
model.compile(optimizer='adam', loss='mse')

n_steps_in, n_steps_out = 3, 2
n_features = 1

其中:

n_steps_in 为输入的 X 每次考虑几个时间步 n_steps_out 为输出的 y 每次考虑几个时间步 n_features 为输入有几个序列

和 Univariate 相比:

模型结构的定义中,多了一个 return_sequences=True,即返回的是序列, 而且 input_shape=(n_steps_in, n_features) 中有代表输入时间步数的 n_steps_in, 输出为 Dense(n_steps_out),代表输出的 y 每次考虑几个时间步.

当然这个问题还可以用 Encoder-Decoder 结构实现:

# define model【Encoder-Decoder Model】
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_steps_in, n_features)))
model.add(RepeatVector(n_steps_out))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(optimizer='adam', loss='mse')

5. Multivariate Multi-Step

 

 

Multivariate Multi-Step 是指:

input 为多个序列, output 为多个时间步的问题。

数例:

训练集:
X,          y
[[10 15]
 [20 25]
 [30 35]] [65 
          85]
[[20 25]
 [30 35]
 [40 45]] [ 85
           105]
[[30 35]
 [40 45]
 [50 55]] [105 
         125]
…


预测输入:
X,
[40 45]
 [50 55]
 [60 65]

模型的 Keras 代码:

# define model
model = Sequential()
model.add(LSTM(100, activation='relu', return_sequences=True, input_shape=(n_steps_in, n_features)))
model.add(LSTM(100, activation='relu'))
model.add(Dense(n_steps_out))
model.compile(optimizer='adam', loss='mse')

n_steps_in, n_steps_out = 3, 2
# 此例中 n features = 2,因为输入有2个并行序列  
n_features = X.shape[2]

其中:

n_steps_in 为输入的 X 每次考虑几个时间步 n_steps_out 为输出的 y 每次考虑几个时间步 n_features 为输入有几个序列,此例中 = 2,因为输入有 2 个并行序列

和 Univariate 相比:

模型结构的定义中,多了一个 return_sequences=True,即返回的是序列, 而且 input_shape=(n_steps_in, n_features) 中有代表输入时间步数的 n_steps_in, 输出为 Dense(n_steps_out),代表输出的 y 每次考虑几个时间步, 另外 n_features = X.shape[2],而不是 1, 相当于是 Multivariate 和 Multi-Step 的结构组合起来。


6. Multiple Parallel Input & Multi-Step Output

 

 

Multiple Parallel Input & Multi-Step Output 是指:

input 为多个序列, output 也是多个序列 & 多个时间步的问题。

数例:

训练集:
X,          y
[[10 15 25]
 [20 25 45]
 [30 35 65]] [[ 40  45  85]
          [ 50  55 105]]
[[20 25 45]
 [30 35 65]
 [40 45 85]] [[ 50  55 105]
          [ 60  65 125]]
[[ 30  35  65]
 [ 40  45  85]
 [ 50  55 105]] [[ 60  65 125]
             [ 70  75 145]]
…


预测输入:
X,
[[ 40  45  85]
 [ 50  55 105]
 [ 60  65 125]]

模型的 Keras 代码:

# define model【Encoder-Decoder model】
model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(n_steps_in, n_features)))
model.add(RepeatVector(n_steps_out))
model.add(LSTM(200, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(n_features)))
model.compile(optimizer='adam', loss='mse')

n_steps_in, n_steps_out = 3, 2
# 此例中 n features = 3,因为输入有3个并行序列   
n_features = X.shape[2]

其中:

n_steps_in 为输入的 X 每次考虑几个时间步 n_steps_out 为输出的 y 每次考虑几个时间步 n_features 为输入有几个序列

这里我们和 Multi-Step 的 Encoder-Decoder 相比:

二者的模型结构,只是在最后的输出层参数不同, TimeDistributed(Dense(n_features)) 而不是 Dense(1)


好啦,这几种时间序列的输入输出模式所对应的代码结构就是这样,如果您还有更有趣的,欢迎补充!

 

文章来源:https://zhuanlan.zhihu.com/p/59862381

  • 6
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LSTM(长短期记忆)是一种递归神经网络(RNN),可以用于处理序列数据,例如时间序列预测。在Keras中,可以使用LSTM层来构建多变量时间序列预测模型。 首先,需要将多个变量转换为单个输入向量。这可以通过将各个变量沿着时间轴堆叠在一起来实现。例如,如果有三个变量 $x_1, x_2, x_3$,每个变量都有 $n$ 个时间步长,则可以将它们组合成一个形状为 $(n, 3)$ 的单个输入张量。 然后,可以使用LSTM层来构建模型。例如,以下代码显示了如何构建具有一个LSTM层的模型: ```python from keras.models import Sequential from keras.layers import LSTM, Dense model = Sequential() model.add(LSTM(50, input_shape=(n_steps, n_features))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') ``` 在这里,`n_steps` 是时间序列的步长,`n_features` 是每个时间步长的变量数。在本例中,我们使用一个LSTM层,其中有50个神经元,并且输入形状为 `(n_steps, n_features)`。随后是一个具有一个神经元的密集层,并使用均方误差作为损失函数进行编译。 最后,可以使用训练数据来拟合模型,并使用测试数据进行预测。 ```python model.fit(X_train, y_train, epochs=100, batch_size=32) y_pred = model.predict(X_test) ``` 在这里,`X_train` 和 `y_train` 是训练数据,`X_test` 是测试数据。在训练期间,使用100个时期和一个批次大小为32来拟合模型。随后,可以使用 `predict` 方法来预测测试数据的输出。 以上是LSTM多变量输入回归预测模型的基本步骤。具体的实现可能因数据类型和模型结构而异。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值