モデル構造LSTM製時系列予測の6種類[ターン] - Kerasが実装

メモリネットワークの長さはLSTM(長期短期記憶ネットワーク)、サイクル後のニューラルネットワークが改善である、RNNは、上述した一連の予測問題長距離依存性、時間を扱うことができない問題を解決することができ、幅広い用途があります。

今日は、分割LSTMいくつかの時系列モデルの構造は、実装上の問題に対応する方法を見ている入力・出力モードに基づいて発行します。


1.単変量

単変量手段:

入力複数の時間ステップ、
出力は時間の問題です。

数例:

训练集:
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.マルチ入力

複数の入力手段:

入力シーケンスには、複数の
出力シーケンスを発行します。

数例:

训练集:
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この例では、2つの並列シーケンスを

単変量と比較:

コードの構造は、同じモデルだけでn_features = X.shape[2]はなく、1。


3.複数の並列

複数の並列手段:

入力された複数の配列、
問題は出力の複数のシーケンスです。

数例:

训练集:
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_features3つの並列入力系列があるので、本実施= 3での

単変量と比較:

模型结构的定义中,多了一个 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://www.jianshu.com/p/88090a7895db

发布了28 篇原创文章 · 获赞 2 · 访问量 1万+

おすすめ

転載: blog.csdn.net/highlevels/article/details/90667108