<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 如何開發多層感知器模型進行時間序列預測 > 原文: [https://machinelearningmastery.com/how-to-develop-multilayer-perceptron-models-for-time-series-forecasting/](https://machinelearningmastery.com/how-to-develop-multilayer-perceptron-models-for-time-series-forecasting/) 多層感知器(簡稱 MLP)可應用于時間序列預測。 使用 MLP 進行時間序列預測的一個挑戰是準備數據。具體而言,必須將滯后觀察平坦化為特征向量。 在本教程中,您將了解如何針對一系列標準時間序列預測問題開發一套 MLP 模型。 本教程的目的是為每種類型的時間序列問題提供每個模型的獨立示例,作為模板,您可以根據特定的時間序列預測問題進行復制和調整。 在本教程中,您將了解如何針對一系列標準時間序列預測問題開發一套多層感知器模型。 完成本教程后,您將了解: * 如何開發單變量時間序列預測的 MLP 模型。 * 如何開發多元時間序列預測的 MLP 模型。 * 如何開發 MLP 模型進行多步時間序列預測。 讓我們開始吧。 ![How to Develop Multilayer Perceptron Models for Time Series Forecasting](https://img.kancloud.cn/c5/49/c549319a38fb62bda43b0d61f6a0b6e2_640x461.jpg) 如何開發用于時間序列預測的多層感知器模型 照片由[土地管理局](https://www.flickr.com/photos/mypubliclands/16358796247/),保留一些權利。 ## 教程概述 本教程分為四個部分;他們是: 1. 單變量 MLP 模型 2. 多變量 MLP 模型 3. 多步 MLP 模型 4. 多變量多步 MLP 模型 ## 單變量 MLP 模型 多層感知器(簡稱 MLP)可用于模擬單變量時間序列預測問題。 單變量時間序列是由具有時間順序的單個觀察序列組成的數據集,并且需要模型來從過去的一系列觀察中學習以預測序列中的下一個值。 本節分為兩部分;他們是: 1. 數據準備 2. MLP 模型 ### 數據準備 在對單變量系列進行建模之前,必須準備好它。 MLP 模型將學習將過去觀察序列作為輸入映射到輸出觀察的函數。因此,必須將觀察序列轉換為模型可以從中學習的多個示例。 考慮給定的單變量序列: ```py [10, 20, 30, 40, 50, 60, 70, 80, 90] ``` 我們可以將序列劃分為多個稱為樣本的輸入/輸出模式,其中三個時間步長用作輸入,一個時間步長用作正在學習的一步預測的輸出。 ```py X, y 10, 20, 30 40 20, 30, 40 50 30, 40, 50 60 ... ``` 下面的 _split_sequence()_ 函數實現了這種行為,并將給定的單變量序列分成多個樣本,其中每個樣本具有指定的時間步長,輸出是單個時間步長。 ```py # split a univariate sequence into samples def split_sequence(sequence, n_steps): X, y = list(), list() for i in range(len(sequence)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the sequence if end_ix > len(sequence)-1: break # gather input and output parts of the pattern seq_x, seq_y = sequence[i:end_ix], sequence[end_ix] X.append(seq_x) y.append(seq_y) return array(X), array(y) ``` 我們可以在上面的小型人為數據集上演示這個功能。 下面列出了完整的示例。 ```py # univariate data preparation from numpy import array # split a univariate sequence into samples def split_sequence(sequence, n_steps): X, y = list(), list() for i in range(len(sequence)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the sequence if end_ix > len(sequence)-1: break # gather input and output parts of the pattern seq_x, seq_y = sequence[i:end_ix], sequence[end_ix] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90] # choose a number of time steps n_steps = 3 # split into samples X, y = split_sequence(raw_seq, n_steps) # summarize the data for i in range(len(X)): print(X[i], y[i]) ``` 運行該示例將單變量系列分成六個樣本,其中每個樣本具有三個輸入時間步長和一個輸出時間步長。 ```py [10 20 30] 40 [20 30 40] 50 [30 40 50] 60 [40 50 60] 70 [50 60 70] 80 [60 70 80] 90 ``` 現在我們已經知道如何準備用于建模的單變量系列,讓我們看看開發一個可以學習輸入到輸出的映射的 MLP 模型。 ### MLP 模型 簡單的 MLP 模型具有單個隱藏的節點層,以及用于進行預測的輸出層。 我們可以如下定義用于單變量時間序列預測的 MLP。 ```py # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_steps)) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') ``` 定義中重要的是輸入的形狀;這就是模型所期望的每個樣本在時間步數方面的輸入。 輸入的時間步數是我們在準備數據集時選擇的數字,作為 _split_sequence()_ 函數的參數。 每個樣本的輸入維度在第一個隱藏層定義的 _input_dim_ 參數中指定。從技術上講,模型將每個時間步驟視為單獨的特征而不是單獨的時間步驟。 我們幾乎總是有多個樣本,因此,模型將期望訓練數據的輸入組件具有尺寸或形狀: ```py [samples, features] ``` 我們在上一節中的 _split_sequence()_ 函數輸出 _X_ ,形狀 _[樣本,特征]_ 準備用于建模。 使用隨機梯度下降的有效 [Adam 版本擬合該模型,并使用均方誤差或' _mse_ ',損失函數進行優化。](https://machinelearningmastery.com/adam-optimization-algorithm-for-deep-learning/) 定義模型后,我們可以將其放在訓練數據集上。 ```py # fit model model.fit(X, y, epochs=2000, verbose=0) ``` 在模型擬合后,我們可以使用它來進行預測。 我們可以通過提供輸入來預測序列中的下一個值: ```py [70, 80, 90] ``` 并期望模型預測如下: ```py [100] ``` 模型期望輸入形狀為 _[樣本,特征]_ 為二維,因此,我們必須在進行預測之前重新整形單個輸入樣本,例如,對于 1 個樣本,形狀為[1,3]和 3 個時間步驟用作輸入功能。 ```py # demonstrate prediction x_input = array([70, 80, 90]) x_input = x_input.reshape((1, n_steps)) yhat = model.predict(x_input, verbose=0) ``` 我們可以將所有這些結合在一起并演示如何為單變量時間序列預測開發 MLP 并進行單一預測。 ```py # univariate mlp example from numpy import array from keras.models import Sequential from keras.layers import Dense # split a univariate sequence into samples def split_sequence(sequence, n_steps): X, y = list(), list() for i in range(len(sequence)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the sequence if end_ix > len(sequence)-1: break # gather input and output parts of the pattern seq_x, seq_y = sequence[i:end_ix], sequence[end_ix] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90] # choose a number of time steps n_steps = 3 # split into samples X, y = split_sequence(raw_seq, n_steps) # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_steps)) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=2000, verbose=0) # demonstrate prediction x_input = array([70, 80, 90]) x_input = x_input.reshape((1, n_steps)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行該示例準備數據,擬合模型并進行預測。 鑒于算法的隨機性,您的結果可能會有所不同;嘗試運行幾次這個例子。 我們可以看到模型預測序列中的下一個值。 ```py [[100.0109]] ``` ## 多變量 MLP 模型 多變量時間序列數據是指每個時間步長有多個觀察值的數據。 對于多變量時間序列數據,我們可能需要兩種主要模型;他們是: 1. 多輸入系列。 2. 多個并聯系列。 讓我們依次看看每一個。 ### 多輸入系列 問題可能有兩個或更多并行輸入時間序列和輸出時間序列,這取決于輸入時間序列。 輸入時間序列是平行的,因為每個系列在同一時間步驟都有觀察。 我們可以通過兩個并行輸入時間序列的簡單示例來演示這一點,其中輸出序列是輸入序列的簡單添加。 ```py # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) ``` 我們可以將這三個數據數組重新整形為單個數據集,其中每一行都是一個時間步,每列是一個單獨的時間序列。這是將并行時間序列存儲在 CSV 文件中的標準方法。 ```py # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) ``` 下面列出了完整的示例。 ```py # multivariate data preparation from numpy import array from numpy import hstack # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) print(dataset) ``` 運行該示例將打印數據集,每個時間步長為一行,兩個輸入和一個輸出并行時間序列分別為一列。 ```py [[ 10 15 25] [ 20 25 45] [ 30 35 65] [ 40 45 85] [ 50 55 105] [ 60 65 125] [ 70 75 145] [ 80 85 165] [ 90 95 185]] ``` 與單變量時間序列一樣,我們必須將這些數據組織成具有輸入和輸出樣本的樣本。 我們需要將數據分成樣本,保持兩個輸入序列的觀察順序。 如果我們選擇三個輸入時間步長,那么第一個樣本將如下所示: 輸入: ```py 10, 15 20, 25 30, 35 ``` 輸出: ```py 65 ``` 也就是說,每個并行系列的前三個時間步長被提供作為模型的輸入,并且模型將其與第三時間步驟的輸出系列中的值相關聯,在這種情況下為 65。 我們可以看到,在將時間序列轉換為輸入/輸出樣本以訓練模型時,我們將不得不從輸出時間序列中丟棄一些值,其中我們在先前時間步驟中沒有輸入時間序列中的值。反過來,選擇輸入時間步數的大小將對使用多少訓練數據產生重要影響。 我們可以定義一個名為 _split_sequences()_ 的函數,該函數將采用數據集,因為我們已經為時間步長和行定義了并行序列和返回輸入/輸出樣本的列。 ```py # split a multivariate sequence into samples def split_sequences(sequences, n_steps): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the dataset if end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1, -1] X.append(seq_x) y.append(seq_y) return array(X), array(y) ``` 我們可以使用每個輸入時間序列的三個時間步長作為輸入在我們的數據集上測試此函數。 下面列出了完整的示例。 ```py # multivariate data preparation from numpy import array from numpy import hstack # split a multivariate sequence into samples def split_sequences(sequences, n_steps): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the dataset if end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1, -1] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps = 3 # convert into input/output X, y = split_sequences(dataset, n_steps) print(X.shape, y.shape) # summarize the data for i in range(len(X)): print(X[i], y[i]) ``` 首先運行該示例將打印 X 和 y 組件的形狀。 我們可以看到 X 組件具有三維結構。 第一個維度是樣本數,在本例中為 7.第二個維度是每個樣本的時間步數,在這種情況下為 3,即為函數指定的值。最后,最后一個維度指定并行時間序列的數量或變量的數量,在這種情況下,兩個并行序列為 2。 然后我們可以看到每個樣本的輸入和輸出都被打印出來,顯示了兩個輸入序列中每個樣本的三個時間步長以及每個樣本的相關輸出。 ```py (7, 3, 2) (7,) [[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 [[50 55] [60 65] [70 75]] 145 [[60 65] [70 75] [80 85]] 165 [[70 75] [80 85] [90 95]] 185 ``` 在我們可以在這些數據上擬合 MLP 之前,我們必須平整輸入樣本的形狀。 MLP 要求每個樣本的輸入部分的形狀是向量。使用多變量輸入,我們將有多個向量,每個時間步長一個。 我們可以展平每個輸入樣本的時間結構,以便: ```py [[10 15] [20 25] [30 35]] ``` 變為: ```py [10, 15, 20, 25, 30, 35] ``` 首先,我們可以計算每個輸入向量的長度,作為時間步數乘以要素數或時間序列數。然后我們可以使用此向量大小來重塑輸入。 ```py # flatten input n_input = X.shape[1] * X.shape[2] X = X.reshape((X.shape[0], n_input)) ``` 我們現在可以為多變量輸入定義 MLP 模型,其中向量長度用于輸入維度參數。 ```py # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_input)) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') ``` 在進行預測時,模型需要兩個輸入時間序列的三個時間步長。 我們可以預測輸出系列中的下一個值,證明輸入值: ```py 80, 85 90, 95 100, 105 ``` 具有 3 個時間步長和 2 個變量的 1 個樣本的形狀將是[1,3,2]。我們必須再次將其重塑為 1 個樣本,其中包含 6 個元素的向量或[1,6] 我們希望序列中的下一個值為 100 + 105 或 205。 ```py # demonstrate prediction x_input = array([[80, 85], [90, 95], [100, 105]]) x_input = x_input.reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) ``` 下面列出了完整的示例。 ```py # multivariate mlp example from numpy import array from numpy import hstack from keras.models import Sequential from keras.layers import Dense # split a multivariate sequence into samples def split_sequences(sequences, n_steps): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the dataset if end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1, -1] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps = 3 # convert into input/output X, y = split_sequences(dataset, n_steps) # flatten input n_input = X.shape[1] * X.shape[2] X = X.reshape((X.shape[0], n_input)) # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_input)) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=2000, verbose=0) # demonstrate prediction x_input = array([[80, 85], [90, 95], [100, 105]]) x_input = x_input.reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行該示例準備數據,擬合模型并進行預測。 ```py [[205.04436]] ``` 還有另一種更精細的方法來模擬問題。 每個輸入序列可以由單獨的 MLP 處理,并且可以在對輸出序列進行預測之前組合這些子模型中的每一個的輸出。 我們可以將其稱為多頭輸入 MLP 模型。根據正在建模的問題的具體情況,它可以提供更大的靈活性或更好的表現。 可以使用 [Keras 功能 API](https://machinelearningmastery.com/keras-functional-api-deep-learning/) 在 Keras 中定義此類型的模型。 首先,我們可以將第一個輸入模型定義為 MLP,其輸入層需要具有 _n_steps_ 特征的向量。 ```py # first input model visible1 = Input(shape=(n_steps,)) dense1 = Dense(100, activation='relu')(visible1) ``` 我們可以以相同的方式定義第二個輸入子模型。 ```py # second input model visible2 = Input(shape=(n_steps,)) dense2 = Dense(100, activation='relu')(visible2) ``` 既然已經定義了兩個輸入子模型,我們可以將每個模型的輸出合并為一個長向量,可以在對輸出序列進行預測之前對其進行解釋。 ```py # merge input models merge = concatenate([dense1, dense2]) output = Dense(1)(merge) ``` 然后我們可以將輸入和輸出聯系在一起。 ```py model = Model(inputs=[visible1, visible2], outputs=output) ``` 下圖提供了該模型外觀的示意圖,包括每層輸入和輸出的形狀。 ![Plot of Multi-Headed MLP for Multivariate Time Series Forecasting](https://img.kancloud.cn/ed/d9/edd99983f2315be23bf7c83d80914203_715x405.jpg) 多元時間序列預測的多頭 MLP 圖 此模型要求輸入作為兩個元素的列表提供,其中列表中的每個元素包含一個子模型的數據。 為了實現這一點,我們可以將 3D 輸入數據分成兩個獨立的輸入數據陣列:即從一個形狀為[7,3,2]的陣列到兩個形狀為[7,3]的 2D 陣列 ```py # separate input data X1 = X[:, :, 0] X2 = X[:, :, 1] ``` 然后可以提供這些數據以適合模型。 ```py # fit model model.fit([X1, X2], y, epochs=2000, verbose=0) ``` 類似地,我們必須在進行單個一步預測時將單個樣本的數據準備為兩個單獨的二維數組。 ```py x_input = array([[80, 85], [90, 95], [100, 105]]) x1 = x_input[:, 0].reshape((1, n_steps)) x2 = x_input[:, 1].reshape((1, n_steps)) ``` 我們可以將所有這些結合在一起;下面列出了完整的示例。 ```py # multivariate mlp example from numpy import array from numpy import hstack from keras.models import Model from keras.layers import Input from keras.layers import Dense from keras.layers.merge import concatenate # split a multivariate sequence into samples def split_sequences(sequences, n_steps): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the dataset if end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1, -1] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps = 3 # convert into input/output X, y = split_sequences(dataset, n_steps) # separate input data X1 = X[:, :, 0] X2 = X[:, :, 1] # first input model visible1 = Input(shape=(n_steps,)) dense1 = Dense(100, activation='relu')(visible1) # second input model visible2 = Input(shape=(n_steps,)) dense2 = Dense(100, activation='relu')(visible2) # merge input models merge = concatenate([dense1, dense2]) output = Dense(1)(merge) model = Model(inputs=[visible1, visible2], outputs=output) model.compile(optimizer='adam', loss='mse') # fit model model.fit([X1, X2], y, epochs=2000, verbose=0) # demonstrate prediction x_input = array([[80, 85], [90, 95], [100, 105]]) x1 = x_input[:, 0].reshape((1, n_steps)) x2 = x_input[:, 1].reshape((1, n_steps)) yhat = model.predict([x1, x2], verbose=0) print(yhat) ``` 運行該示例準備數據,擬合模型并進行預測。 ```py [[206.05022]] ``` ### 多個并聯系列 另一個時間序列問題是存在多個并行時間序列并且必須為每個時間序列預測值的情況。 例如,給定上一節的數據: ```py [[ 10 15 25] [ 20 25 45] [ 30 35 65] [ 40 45 85] [ 50 55 105] [ 60 65 125] [ 70 75 145] [ 80 85 165] [ 90 95 185]] ``` 我們可能想要預測下一個時間步的三個時間序列中的每一個的值。 這可以稱為多變量預測。 同樣,必須將數據分成輸入/輸出樣本以訓練模型。 該數據集的第一個示例是: 輸入: ```py 10, 15, 25 20, 25, 45 30, 35, 65 ``` 輸出: ```py 40, 45, 85 ``` 下面的 _split_sequences()_ 函數將分割多個并行時間序列,其中時間步長為行,每列一個系列為所需的輸入/輸出形狀。 ```py # split a multivariate sequence into samples def split_sequences(sequences, n_steps): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the dataset if end_ix > len(sequences)-1: break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix, :] X.append(seq_x) y.append(seq_y) return array(X), array(y) ``` 我們可以在人為的問題上證明這一點;下面列出了完整的示例。 ```py # multivariate output data prep from numpy import array from numpy import hstack # split a multivariate sequence into samples def split_sequences(sequences, n_steps): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the dataset if end_ix > len(sequences)-1: break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix, :] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps = 3 # convert into input/output X, y = split_sequences(dataset, n_steps) print(X.shape, y.shape) # summarize the data for i in range(len(X)): print(X[i], y[i]) ``` 首先運行該實例打印制備的 _X_ 和 _y_ 組分的形狀。 _X_ 的形狀是三維的,包括樣品的數量(6),每個樣品選擇的時間步數(3),以及平行時間序列或特征的數量(3)。 _y_ 的形狀是二維的,正如我們對樣本數(6)和每個樣本預測的時間變量數(3)所預期的那樣。 然后,打印每個樣本,顯示每個樣本的輸入和輸出分量。 ```py (6, 3, 3) (6, 3) [[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] [[ 50 55 105] [ 60 65 125] [ 70 75 145]] [ 80 85 165] [[ 60 65 125] [ 70 75 145] [ 80 85 165]] [ 90 95 185] ``` 我們現在準備在這些數據上安裝 MLP 模型。 與之前的多變量輸入情況一樣,我們必須將輸入數據樣本的三維結構展平為[_ 樣本,特征 _]的二維結構,其中滯后觀察被模型視為特征。 ```py # flatten input n_input = X.shape[1] * X.shape[2] X = X.reshape((X.shape[0], n_input)) ``` 模型輸出將是一個向量,三個不同時間序列中的每一個都有一個元素。 ```py n_output = y.shape[1] ``` 我們現在可以定義我們的模型,使用輸入層的展平向量長度和進行預測時的向量長度作為向量長度。 ```py # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_input)) model.add(Dense(n_output)) model.compile(optimizer='adam', loss='mse') ``` 我們可以通過為每個系列提供三個時間步長的輸入來預測三個并行系列中的每一個的下一個值。 ```py 70, 75, 145 80, 85, 165 90, 95, 185 ``` 用于進行單個預測的輸入的形狀必須是 1 個樣本,3 個時間步長和 3 個特征,或者[1,3,3]。同樣,我們可以將其展平為[1,6]以滿足模型的期望。 我們希望向量輸出為: ```py [100, 105, 205] ``` ```py # demonstrate prediction x_input = array([[70,75,145], [80,85,165], [90,95,185]]) x_input = x_input.reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) ``` 我們可以將所有這些結合在一起并演示下面的多變量輸出時間序列預測的 MLP。 ```py # multivariate output mlp example from numpy import array from numpy import hstack from keras.models import Sequential from keras.layers import Dense # split a multivariate sequence into samples def split_sequences(sequences, n_steps): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the dataset if end_ix > len(sequences)-1: break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix, :] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps = 3 # convert into input/output X, y = split_sequences(dataset, n_steps) # flatten input n_input = X.shape[1] * X.shape[2] X = X.reshape((X.shape[0], n_input)) n_output = y.shape[1] # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_input)) model.add(Dense(n_output)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=2000, verbose=0) # demonstrate prediction x_input = array([[70,75,145], [80,85,165], [90,95,185]]) x_input = x_input.reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行該示例準備數據,擬合模型并進行預測。 ```py [[100.95039 107.541306 206.81033 ]] ``` 與多輸入系列一樣,還有另一種更精細的方法來模擬問題。 每個輸出系列都可以由單獨的輸出 MLP 模型處理。 我們可以將其稱為多輸出 MLP 模型。根據正在建模的問題的具體情況,它可以提供更大的靈活性或更好的表現。 可以使用 [Keras 功能 API](https://machinelearningmastery.com/keras-functional-api-deep-learning/) 在 Keras 中定義此類型的模型。 首先,我們可以將輸入模型定義為 MLP,其輸入層需要平坦的特征向量。 ```py # define model visible = Input(shape=(n_input,)) dense = Dense(100, activation='relu')(visible) ``` 然后,我們可以為我們希望預測的三個系列中的每一個定義一個輸出層,其中每個輸出子模型將預測單個時間步長。 ```py # define output 1 output1 = Dense(1)(dense) # define output 2 output2 = Dense(1)(dense) # define output 2 output3 = Dense(1)(dense) ``` 然后,我們可以將輸入和輸出層組合到一個模型中。 ```py # tie together model = Model(inputs=visible, outputs=[output1, output2, output3]) model.compile(optimizer='adam', loss='mse') ``` 為了使模型架構清晰,下面的示意圖清楚地顯示了模型的三個獨立輸出層以及每個層的輸入和輸出形狀。 ![Plot of Multi-Output MLP for Multivariate Time Series Forecasting](https://img.kancloud.cn/4b/cf/4bcf945552e40ed9bdc6f8a0673fa7f8_1039x295.jpg) 多元時間序列預測的多輸出 MLP 圖 在訓練模型時,每個樣本需要三個獨立的輸出陣列。 我們可以通過將具有形狀[7,3]的輸出訓練數據轉換為具有形狀[7,1]的三個陣列來實現這一點。 ```py # separate output y1 = y[:, 0].reshape((y.shape[0], 1)) y2 = y[:, 1].reshape((y.shape[0], 1)) y3 = y[:, 2].reshape((y.shape[0], 1)) ``` 可以在訓練期間將這些陣列提供給模型。 ```py # fit model model.fit(X, [y1,y2,y3], epochs=2000, verbose=0) ``` 將所有這些結合在一起,下面列出了完整的示例。 ```py # multivariate output mlp example from numpy import array from numpy import hstack from keras.models import Model from keras.layers import Input from keras.layers import Dense # split a multivariate sequence into samples def split_sequences(sequences, n_steps): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps # check if we are beyond the dataset if end_ix > len(sequences)-1: break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix, :] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps = 3 # convert into input/output X, y = split_sequences(dataset, n_steps) # flatten input n_input = X.shape[1] * X.shape[2] X = X.reshape((X.shape[0], n_input)) # separate output y1 = y[:, 0].reshape((y.shape[0], 1)) y2 = y[:, 1].reshape((y.shape[0], 1)) y3 = y[:, 2].reshape((y.shape[0], 1)) # define model visible = Input(shape=(n_input,)) dense = Dense(100, activation='relu')(visible) # define output 1 output1 = Dense(1)(dense) # define output 2 output2 = Dense(1)(dense) # define output 2 output3 = Dense(1)(dense) # tie together model = Model(inputs=visible, outputs=[output1, output2, output3]) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, [y1,y2,y3], epochs=2000, verbose=0) # demonstrate prediction x_input = array([[70,75,145], [80,85,165], [90,95,185]]) x_input = x_input.reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行該示例準備數據,擬合模型并進行預測。 ```py [array([[100.86121]], dtype=float32), array([[105.14738]], dtype=float32), array([[205.97507]], dtype=float32)] ``` ## 多步 MLP 模型 實際上,MLP 模型在預測表示不同輸出變量的向量輸出(如前例中所示)或表示一個變量的多個時間步長的向量輸出方面幾乎沒有差別。 然而,訓練數據的編制方式存在細微而重要的差異。在本節中,我們將演示使用向量模型開發多步預測模型的情況。 在我們查看模型的細節之前,讓我們首先看一下多步預測的數據準備。 ### 數據準備 與一步預測一樣,用于多步時間序列預測的時間序列必須分為帶有輸入和輸出組件的樣本。 輸入和輸出組件都將包含多個時間步長,并且可以具有或不具有相同數量的步驟。 例如,給定單變量時間序列: ```py [10, 20, 30, 40, 50, 60, 70, 80, 90] ``` 我們可以使用最后三個時間步作為輸入并預測接下來的兩個時間步。 第一個樣本如下: 輸入: ```py [10, 20, 30] ``` 輸出: ```py [40, 50] ``` 下面的 _split_sequence()_ 函數實現了這種行為,并將給定的單變量時間序列分割為具有指定數量的輸入和輸出時間步長的樣本。 ```py # split a univariate sequence into samples def split_sequence(sequence, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequence)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out # check if we are beyond the sequence if out_end_ix > len(sequence): break # gather input and output parts of the pattern seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:out_end_ix] X.append(seq_x) y.append(seq_y) return array(X), array(y) ``` 我們可以在小型設計數據集上演示此功能。 下面列出了完整的示例。 ```py # multi-step data preparation from numpy import array # split a univariate sequence into samples def split_sequence(sequence, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequence)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out # check if we are beyond the sequence if out_end_ix > len(sequence): break # gather input and output parts of the pattern seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:out_end_ix] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90] # choose a number of time steps n_steps_in, n_steps_out = 3, 2 # split into samples X, y = split_sequence(raw_seq, n_steps_in, n_steps_out) # summarize the data for i in range(len(X)): print(X[i], y[i]) ``` 運行該示例將單變量系列拆分為輸入和輸出時間步驟,并打印每個系列的輸入和輸出組件。 ```py [10 20 30] [40 50] [20 30 40] [50 60] [30 40 50] [60 70] [40 50 60] [70 80] [50 60 70] [80 90] ``` 既然我們知道如何為多步預測準備數據,那么讓我們看一下可以學習這種映射的 MLP 模型。 ### 向量輸出模型 MLP 可以直接輸出向量,可以解釋為多步預測。 在前一節中看到這種方法是每個輸出時間序列的一個時間步驟被預測為向量。 通過 _n_steps_in_ 和 _n_steps_out_ 變量中指定的輸入和輸出步數,我們可以定義一個多步驟時間序列預測模型。 ```py # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_steps_in)) model.add(Dense(n_steps_out)) model.compile(optimizer='adam', loss='mse') ``` 該模型可以對單個樣本進行預測。我們可以通過提供輸入來預測數據集末尾之后的下兩個步驟: ```py [70, 80, 90] ``` 我們希望預測的輸出為: ```py [100, 110] ``` 正如模型所預期的那樣,進行預測時輸入數據的單個樣本的形狀對于輸入和單個特征的 1 個樣本和 3 個時間步長(特征)必須是[1,3]。 ```py # demonstrate prediction x_input = array([70, 80, 90]) x_input = x_input.reshape((1, n_steps_in)) yhat = model.predict(x_input, verbose=0) ``` 將所有這些結合在一起,下面列出了具有單變量時間序列的多步驟預測的 MLP。 ```py # univariate multi-step vector-output mlp example from numpy import array from keras.models import Sequential from keras.layers import Dense # split a univariate sequence into samples def split_sequence(sequence, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequence)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out # check if we are beyond the sequence if out_end_ix > len(sequence): break # gather input and output parts of the pattern seq_x, seq_y = sequence[i:end_ix], sequence[end_ix:out_end_ix] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence raw_seq = [10, 20, 30, 40, 50, 60, 70, 80, 90] # choose a number of time steps n_steps_in, n_steps_out = 3, 2 # split into samples X, y = split_sequence(raw_seq, n_steps_in, n_steps_out) # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_steps_in)) model.add(Dense(n_steps_out)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=2000, verbose=0) # demonstrate prediction x_input = array([70, 80, 90]) x_input = x_input.reshape((1, n_steps_in)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行示例預測并打印序列中的后兩個時間步驟。 ```py [[102.572365 113.88405 ]] ``` ## 多變量多步 MLP 模型 在前面的部分中,我們研究了單變量,多變量和多步驟時間序列預測。 可以混合和匹配到目前為止針對不同問題呈現的不同類型的 MLP 模型。這也適用于涉及多變量和多步預測的時間序列預測問題,但它可能更具挑戰性,特別是在準備數據和定義模型的輸入和輸出的形狀時。 在本節中,我們將以多變量多步驟時間序列預測的數據準備和建模的簡短示例作為模板來緩解這一挑戰,具體來說: 1. 多輸入多步輸出。 2. 多個并行輸入和多步輸出。 也許最大的絆腳石是準備數據,所以這是我們關注的重點。 ### 多輸入多步輸出 存在多變量時間序列預測問題,其中輸出序列是分開的但取決于輸入時間序列,并且輸出序列需要多個時間步長。 例如,考慮前一部分的多變量時間序列: ```py [[ 10 15 25] [ 20 25 45] [ 30 35 65] [ 40 45 85] [ 50 55 105] [ 60 65 125] [ 70 75 145] [ 80 85 165] [ 90 95 185]] ``` 我們可以使用兩個輸入時間序列中的每一個的三個先前時間步驟來預測輸出時間序列的兩個時間步長。 輸入: ```py 10, 15 20, 25 30, 35 ``` 輸出: ```py 65 85 ``` 下面的 _split_sequences()_ 函數實現了這種行為。 ```py # split a multivariate sequence into samples def split_sequences(sequences, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out-1 # check if we are beyond the dataset if out_end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1:out_end_ix, -1] X.append(seq_x) y.append(seq_y) return array(X), array(y) ``` 我們可以在我們設計的數據集上證明這一點。下面列出了完整的示例。 ```py # multivariate multi-step data preparation from numpy import array from numpy import hstack # split a multivariate sequence into samples def split_sequences(sequences, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out-1 # check if we are beyond the dataset if out_end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1:out_end_ix, -1] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps_in, n_steps_out = 3, 2 # convert into input/output X, y = split_sequences(dataset, n_steps_in, n_steps_out) print(X.shape, y.shape) # summarize the data for i in range(len(X)): print(X[i], y[i]) ``` 首先運行該示例打印準備好的訓練數據的形狀。 我們可以看到樣本的輸入部分的形狀是三維的,由六個樣本組成,具有三個時間步長和兩個輸入時間序列的兩個變量。 樣本的輸出部分對于六個樣本是二維的,并且每個樣本的兩個時間步長是預測的。 然后打印制備的樣品以確認數據是按照我們指定的方式制備的。 ```py (6, 3, 2) (6, 2) [[10 15] [20 25] [30 35]] [65 85] [[20 25] [30 35] [40 45]] [ 85 105] [[30 35] [40 45] [50 55]] [105 125] [[40 45] [50 55] [60 65]] [125 145] [[50 55] [60 65] [70 75]] [145 165] [[60 65] [70 75] [80 85]] [165 185] ``` 我們現在可以使用向量輸出開發用于多步預測的 MLP 模型。 下面列出了完整的示例。 ```py # multivariate multi-step mlp example from numpy import array from numpy import hstack from keras.models import Sequential from keras.layers import Dense # split a multivariate sequence into samples def split_sequences(sequences, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out-1 # check if we are beyond the dataset if out_end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1:out_end_ix, -1] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps_in, n_steps_out = 3, 2 # convert into input/output X, y = split_sequences(dataset, n_steps_in, n_steps_out) # flatten input n_input = X.shape[1] * X.shape[2] X = X.reshape((X.shape[0], n_input)) # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_input)) model.add(Dense(n_steps_out)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=2000, verbose=0) # demonstrate prediction x_input = array([[70, 75], [80, 85], [90, 95]]) x_input = x_input.reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行該示例適合模型并預測輸出序列的下兩個時間步驟超出數據集。 我們希望接下來的兩個步驟是[185,205]。 這是一個具有挑戰性的問題框架,數據非常少,模型的任意配置版本也很接近。 ```py [[186.53822 208.41725]] ``` ### 多個并行輸入和多步輸出 并行時間序列的問題可能需要預測每個時間序列的多個時間步長。 例如,考慮前一部分的多變量時間序列: ```py [[ 10 15 25] [ 20 25 45] [ 30 35 65] [ 40 45 85] [ 50 55 105] [ 60 65 125] [ 70 75 145] [ 80 85 165] [ 90 95 185]] ``` 我們可以使用三個時間序列中的每一個的最后三個步驟作為模型的輸入,并預測三個時間序列中的每一個的下一個時間步長作為輸出。 訓練數據集中的第一個樣本如下。 輸入: ```py 10, 15, 25 20, 25, 45 30, 35, 65 ``` 輸出: ```py 40, 45, 85 50, 55, 105 ``` 下面的 _split_sequences()_ 函數實現了這種行為。 ```py # split a multivariate sequence into samples def split_sequences(sequences, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out # check if we are beyond the dataset if out_end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix:out_end_ix, :] X.append(seq_x) y.append(seq_y) return array(X), array(y) ``` 我們可以在小型設計數據集上演示此功能。 下面列出了完整的示例。 ```py # multivariate multi-step data preparation from numpy import array from numpy import hstack # split a multivariate sequence into samples def split_sequences(sequences, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out # check if we are beyond the dataset if out_end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix:out_end_ix, :] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps_in, n_steps_out = 3, 2 # convert into input/output X, y = split_sequences(dataset, n_steps_in, n_steps_out) print(X.shape, y.shape) # summarize the data for i in range(len(X)): print(X[i], y[i]) ``` 首先運行該示例打印準備好的訓練數據集的形狀。 我們可以看到數據集的輸入( _X_ )和輸出( _Y_ )元素分別對于樣本數,時間步長和變量或并行時間序列是三維的。 。 然后將每個系列的輸入和輸出元素并排打印,以便我們可以確認數據是按照我們的預期準備的。 ```py (5, 3, 3) (5, 2, 3) [[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]] [[ 40 45 85] [ 50 55 105] [ 60 65 125]] [[ 70 75 145] [ 80 85 165]] [[ 50 55 105] [ 60 65 125] [ 70 75 145]] [[ 80 85 165] [ 90 95 185]] ``` 我們現在可以開發 MLP 模型來進行多變量多步預測。 除了展平輸入數據的形狀之外,正如我們在先前的例子中所做的那樣,我們還必須平整輸出數據的三維結構。這是因為 MLP 模型只能采用向量輸入和輸出。 ```py # flatten input n_input = X.shape[1] * X.shape[2] X = X.reshape((X.shape[0], n_input)) # flatten output n_output = y.shape[1] * y.shape[2] y = y.reshape((y.shape[0], n_output)) ``` 下面列出了完整的示例。 ```py # multivariate multi-step mlp example from numpy import array from numpy import hstack from keras.models import Sequential from keras.layers import Dense # split a multivariate sequence into samples def split_sequences(sequences, n_steps_in, n_steps_out): X, y = list(), list() for i in range(len(sequences)): # find the end of this pattern end_ix = i + n_steps_in out_end_ix = end_ix + n_steps_out # check if we are beyond the dataset if out_end_ix > len(sequences): break # gather input and output parts of the pattern seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix:out_end_ix, :] X.append(seq_x) y.append(seq_y) return array(X), array(y) # define input sequence in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95]) out_seq = array([in_seq1[i]+in_seq2[i] for i in range(len(in_seq1))]) # convert to [rows, columns] structure in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) out_seq = out_seq.reshape((len(out_seq), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2, out_seq)) # choose a number of time steps n_steps_in, n_steps_out = 3, 2 # convert into input/output X, y = split_sequences(dataset, n_steps_in, n_steps_out) # flatten input n_input = X.shape[1] * X.shape[2] X = X.reshape((X.shape[0], n_input)) # flatten output n_output = y.shape[1] * y.shape[2] y = y.reshape((y.shape[0], n_output)) # define model model = Sequential() model.add(Dense(100, activation='relu', input_dim=n_input)) model.add(Dense(n_output)) model.compile(optimizer='adam', loss='mse') # fit model model.fit(X, y, epochs=2000, verbose=0) # demonstrate prediction x_input = array([[60, 65, 125], [70, 75, 145], [80, 85, 165]]) x_input = x_input.reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行該示例適合模型并預測超出數據集末尾的下兩個時間步的三個時間步中的每一個的值。 我們希望這些系列和時間步驟的值如下: ```py 90, 95, 185 100, 105, 205 ``` 我們可以看到模型預測合理地接近預期值。 ```py [[ 91.28376 96.567 188.37575 100.54482 107.9219 208.108 ] ``` ## 摘要 在本教程中,您了解了如何針對一系列標準時間序列預測問題開發一套多層感知器或 MLP 模型。 具體來說,你學到了: * 如何開發單變量時間序列預測的 MLP 模型。 * 如何開發多元時間序列預測的 MLP 模型。 * 如何開發 MLP 模型進行多步時間序列預測。 你有任何問題嗎? 在下面的評論中提出您的問題,我會盡力回答。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看