<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 如何使用 TimeseriesGenerator 進行 Keras 中的時間序列預測 > 原文: [https://machinelearningmastery.com/how-to-use-the-timeseriesgenerator-for-time-series-forecasting-in-keras/](https://machinelearningmastery.com/how-to-use-the-timeseriesgenerator-for-time-series-forecasting-in-keras/) 必須將時間序列數據轉換為具有輸入和輸出組件的樣本結構,然后才能用于擬合監督學習模型。 如果您必須手動執行此轉換,這可能具有挑戰性。 Keras 深度學習庫提供了 TimeseriesGenerator,可以將單變量和多變量時間序列數據自動轉換為樣本,隨時可以訓練深度學習模型。 在本教程中,您將了解如何使用 Keras TimeseriesGenerator 準備時間序列數據,以便使用深度學習方法進行建模。 完成本教程后,您將了解: * 如何定義 TimeseriesGenerator 生成器并將其用于適合深度學習模型。 * 如何為單變量時間序列準備發電機并適合 MLP 和 LSTM 模型。 * 如何為多變量時間序列準備生成器并適合 LSTM 模型。 讓我們開始吧。 ![How to Use the TimeseriesGenerator for Time Series Forecasting in Keras](https://img.kancloud.cn/21/a9/21a9500afb8a3b9e5da32093699eecb3_640x424.jpg) 如何在 Keras 中使用 TimeseriesGenerator 進行時間序列預測 照片由 [Chris Fithall](https://www.flickr.com/photos/chrisfithall/13933989150/) 拍攝,保留一些權利。 ## 教程概述 本教程分為六個部分;他們是: 1. 監督學習的時間序列問題 2. 如何使用 TimeseriesGenerator 3. 單變量時間序列示例 4. 多變量時間序列示例 5. 多變量輸入和相關系列示例 6. 多步預測示例 **注**:本教程假設您使用的是 **Keras v2.2.4** 或更高版本。 ## 監督學習的時間序列問題 時間序列數據需要準備才能用于訓練監督學習模型,例如深度學習模型。 例如,單變量時間序列表示為觀察向量: ```py [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ``` 監督學習算法要求數據作為樣本集合提供,其中每個樣本具有輸入分量( _X_ )和輸出分量( _y_ )。 ```py X, y example input, example output example input, example output example input, example output ... ``` 該模型將學習如何將輸入映射到所提供示例的輸出。 ```py y = f(X) ``` 必須將時間序列轉換為具有輸入和輸出組件的樣本。該變換既可以告知模型將要學習什么,也可以在進行預測時告知將來如何使用該模型,例如:做出預測需要什么( _X_ )和做出預測( _y_ )。 對于對一步預測感興趣的單變量時間序列,在先前時間步驟的觀察,即所謂的滯后觀察,被用作輸入,輸出是在當前時間步驟的觀察。 例如,上述 10 步單變量系列可以表示為監督學習問題,其中輸入的三個時間步長和輸出的一個步驟如下: ```py X, y [1, 2, 3], [4] [2, 3, 4], [5] [3, 4, 5], [6] ... ``` 您可以編寫代碼來自行執行此轉換;例如,看帖子: * [如何將時間序列轉換為 Python 中的監督學習問題](https://machinelearningmastery.com/convert-time-series-supervised-learning-problem-python/) 或者,當您對使用 Keras 訓練神經網絡模型感興趣時,可以使用 TimeseriesGenerator 類。 ## 如何使用 TimeseriesGenerator Keras 提供 [TimeseriesGenerator](https://keras.io/preprocessing/sequence/) ,可用于將單變量或多變量時間序列數據集自動轉換為監督學習問題。 使用 TimeseriesGenerator 有兩個部分:定義它并使用它來訓練模型。 ### 定義 TimeseriesGenerator 您可以創建類的實例并指定時間序列問題的輸入和輸出方面,它將提供[序列類](https://keras.io/utils/#sequence)的實例,然后可以使用它來迭代輸入和輸出系列。 在大多數時間序列預測問題中,輸入和輸出系列將是同一系列。 例如: ```py # load data inputs = ... outputs = ... # define generator generator = TimeseriesGenerator(inputs, outputs, ...) # iterator generator for i in range(len(generator)): ... ``` 從技術上講,該類不是一個生成器,因為它不是 [Python 生成器](https://wiki.python.org/moin/Generators),你不能在它上面使用 _next()_ 函數。 除了指定時間序列問題的輸入和輸出方面外,還應該配置一些其他參數;例如: * **長度**:在每個樣本的輸入部分中使用的滯后觀察的數量(例如 3)。 * **batch_size** :每次迭代時返回的樣本數(例如 32)。 您必須根據設計的問題框架定義長度參數。這是用作輸入的所需滯后觀察數。 您還必須在訓練期間將批量大小定義為模型的批量大小。如果數據集中的樣本數小于批量大小,則可以通過計算其長度,將生成器和模型中的批量大小設置為生成器中的樣本總數;例如: ```py print(len(generator)) ``` 還有其他參數,例如定義數據的開始和結束偏移,采樣率,步幅等。您不太可能使用這些功能,但您可以查看[完整 API](https://keras.io/preprocessing/sequence/) 以獲取更多詳細信息。 默認情況下,樣本不會被洗牌。這對于像 LSTM 這樣的一些循環神經網絡很有用,這些神經網絡在一批中的樣本之間保持狀態。 它可以使其他神經網絡(例如 CNN 和 MLP)在訓練時對樣本進行混洗。可以通過將' _shuffle_ '參數設置為 True 來啟用隨機播放。這將具有為每個批次返回的洗樣樣本的效果。 在撰寫本文時,TimeseriesGenerator 僅限于一步輸出。不支持多步時間序列預測。 ### 使用 TimeseriesGenerator 訓練模型 一旦定義了 TimeseriesGenerator 實例,它就可以用于訓練神經網絡模型。 可以使用 TimeseriesGenerator 作為數據生成器來訓練模型。這可以通過使用 _fit_generator()_ 函數擬合定義的模型來實現。 此函數將生成器作為參數。它還需要 _steps_per_epoch_ 參數來定義每個時期中使用的樣本數。可以將此值設置為 TimeseriesGenerator 實例的長度,以使用生成器中的所有樣本。 例如: ```py # define generator generator = TimeseriesGenerator(...) # define model model = ... # fit model model.fit_generator(generator, steps_per_epoch=len(generator), ...) ``` 類似地,生成器可用于通過調用 _evaluate_generator()_ 函數來評估擬合模型,并使用擬合模型使用 _predict_generator()_ 函數對新數據進行預測。 與數據生成器匹配的模型不必使用 evaluate 和 predict 函數的生成器版本。只有在您希望數據生成器為模型準備數據時,才能使用它們。 ## 單變量時間序列示例 我們可以使用一個小的設計單變量時間序列數據集的工作示例使 TimeseriesGenerator 具體化。 首先,讓我們定義我們的數據集。 ```py # define dataset series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) ``` 我們將選擇框架問題,其中最后兩個滯后觀察將用于預測序列中的下一個值。例如: ```py X, y [1, 2] 3 ``` 現在,我們將使用批量大小為 1,以便我們可以探索生成器中的數據。 ```py # define generator n_input = 2 generator = TimeseriesGenerator(series, series, length=n_input, batch_size=1) ``` 接下來,我們可以看到數據生成器將為此時間序列準備多少樣本。 ```py # number of samples print('Samples: %d' % len(generator)) ``` 最后,我們可以打印每個樣本的輸入和輸出組件,以確認數據是按照我們的預期準備的。 ```py for i in range(len(generator)): x, y = generator[i] print('%s => %s' % (x, y)) ``` 下面列出了完整的示例。 ```py # univariate one step problem from numpy import array from keras.preprocessing.sequence import TimeseriesGenerator # define dataset series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # define generator n_input = 2 generator = TimeseriesGenerator(series, series, length=n_input, batch_size=1) # number of samples print('Samples: %d' % len(generator)) # print each sample for i in range(len(generator)): x, y = generator[i] print('%s => %s' % (x, y)) ``` 首先運行該示例打印生成器中的樣本總數,即 8。 然后我們可以看到每個輸入數組的形狀為[1,2],每個輸出的形狀為[1,]。 觀察結果按照我們的預期進行準備,其中兩個滯后觀察結果將用作輸入,序列中的后續值作為輸出。 ```py Samples: 8 [[1\. 2.]] => [3.] [[2\. 3.]] => [4.] [[3\. 4.]] => [5.] [[4\. 5.]] => [6.] [[5\. 6.]] => [7.] [[6\. 7.]] => [8.] [[7\. 8.]] => [9.] [[8\. 9.]] => [10.] ``` 現在我們可以在這個數據上擬合模型,并學習將輸入序列映射到輸出序列。 我們將從一個簡單的多層感知器或 MLP 模型開始。 將定義發生器,以便在給定少量樣品的情況下,將在每批中使用所有樣品。 ```py # define generator n_input = 2 generator = TimeseriesGenerator(series, series, length=n_input, batch_size=8) ``` 我們可以定義一個簡單的模型,其中一個隱藏層有 50 個節點,一個輸出層將進行預測。 ```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') ``` 然后我們可以使用 _fit_generator()_ 函數將模型與生成器擬合。我們在生成器中只有一批數據,因此我們將 _steps_per_epoch_ 設置為 1.該模型適用于 200 個時期。 ```py # fit model model.fit_generator(generator, steps_per_epoch=1, epochs=200, verbose=0) ``` 一旦適合,我們將進行樣本預測。 給定輸入[9,10],我們將進行預測并期望模型預測[11]或接近它。該模型沒有調整;這只是如何使用發電機的一個例子。 ```py # make a one step prediction out of sample x_input = array([9, 10]).reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) ``` 下面列出了完整的示例。 ```py # univariate one step problem with mlp from numpy import array from keras.models import Sequential from keras.layers import Dense from keras.preprocessing.sequence import TimeseriesGenerator # define dataset series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # define generator n_input = 2 generator = TimeseriesGenerator(series, series, length=n_input, batch_size=8) # 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_generator(generator, steps_per_epoch=1, epochs=200, verbose=0) # make a one step prediction out of sample x_input = array([9, 10]).reshape((1, n_input)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行該示例準備生成器,擬合模型,并進行樣本預測,正確預測接近 11 的值。 ```py [[11.510406]] ``` 我們還可以使用生成器來適應循環神經網絡,例如長短期內存網絡或 LSTM。 LSTM 期望數據輸入具有[_ 樣本,時間步長,特征 _]的形狀,而到目前為止描述的生成器提供滯后觀察作為特征或形狀[_ 樣本,特征 _]。 我們可以在從[10,]到[10,1]準備發電機之前重新設計單變量時間序列 10 個時間步長和 1 個特征;例如: ```py # reshape to [10, 1] n_features = 1 series = series.reshape((len(series), n_features)) ``` 然后,TimeseriesGenerator 將系列分為樣品,其形狀為[ _batch,n_input,1_ ]或[8,2,1],用于生成器中的所有八個樣品,兩個滯后觀察用作時間步長。 下面列出了完整的示例。 ```py # univariate one step problem with lstm from numpy import array from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.preprocessing.sequence import TimeseriesGenerator # define dataset series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) # reshape to [10, 1] n_features = 1 series = series.reshape((len(series), n_features)) # define generator n_input = 2 generator = TimeseriesGenerator(series, series, length=n_input, batch_size=8) # define model model = Sequential() model.add(LSTM(100, activation='relu', input_shape=(n_input, n_features))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') # fit model model.fit_generator(generator, steps_per_epoch=1, epochs=500, verbose=0) # make a one step prediction out of sample x_input = array([9, 10]).reshape((1, n_input, n_features)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 再次,運行該示例準備數據,擬合模型,并預測序列中的下一個樣本外值。 ```py [[11.092189]] ``` ## 多變量時間序列示例 TimeseriesGenerator 還支持多變量時間序列問題。 這些是您有多個并行系列的問題,每個系列中的觀察步驟同時進行。 我們可以用一個例子來證明這一點。 首先,我們可以設計兩個并行系列的數據集。 ```py # define dataset in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95, 105]) ``` 具有多變量時間序列格式的標準結構使得每個時間序列是單獨的列,行是每個時間步的觀察值。 我們定義的系列是向量,但我們可以將它們轉換為列。我們可以將每個系列重塑為具有 10 個時間步長和 1 個特征的形狀[10,1]的數組。 ```py # reshape series in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) ``` 我們現在可以通過調用 _hstack()_ NumPy 函數將列水平堆疊到數據集中。 ```py # horizontally stack columns dataset = hstack((in_seq1, in_seq2)) ``` 我們現在可以直接將此數據集提供給 TimeseriesGenerator。我們將使用每個系列的前兩個觀測值作為輸入,并將每個序列的下一個觀測值作為輸出。 ```py # define generator n_input = 2 generator = TimeseriesGenerator(dataset, dataset, length=n_input, batch_size=1) ``` 然后,對于 1 個樣本,2 個時間步長和 2 個特征或平行序列,每個樣本將是[1,2,2]的三維陣列。對于 1 個樣本和 2 個特征,輸出將是[1,2]的二維系列。第一個樣本將是: ```py X, y [[10, 15], [20, 25]] [[30, 35]] ``` 下面列出了完整的示例。 ```py # multivariate one step problem from numpy import array from numpy import hstack from keras.preprocessing.sequence import TimeseriesGenerator # define dataset in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95, 105]) # reshape series in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2)) print(dataset) # define generator n_input = 2 generator = TimeseriesGenerator(dataset, dataset, length=n_input, batch_size=1) # number of samples print('Samples: %d' % len(generator)) # print each sample for i in range(len(generator)): x, y = generator[i] print('%s => %s' % (x, y)) ``` 運行該示例將首先打印準備好的數據集,然后打印數據集中的總樣本數。 接下來,打印每個樣品的輸入和輸出部分,確認我們的預期結構。 ```py [[ 10 15] [ 20 25] [ 30 35] [ 40 45] [ 50 55] [ 60 65] [ 70 75] [ 80 85] [ 90 95] [100 105]] Samples: 8 [[[10\. 15.] [20\. 25.]]] => [[30\. 35.]] [[[20\. 25.] [30\. 35.]]] => [[40\. 45.]] [[[30\. 35.] [40\. 45.]]] => [[50\. 55.]] [[[40\. 45.] [50\. 55.]]] => [[60\. 65.]] [[[50\. 55.] [60\. 65.]]] => [[70\. 75.]] [[[60\. 65.] [70\. 75.]]] => [[80\. 85.]] [[[70\. 75.] [80\. 85.]]] => [[90\. 95.]] [[[80\. 85.] [90\. 95.]]] => [[100\. 105.]] ``` 樣本的三維結構意味著生成器不能直接用于像 MLP 這樣的簡單模型。 這可以通過首先將時間序列數據集展平為一維向量,然后將其提供給 TimeseriesGenerator 并將長度設置為用作輸入的步數乘以系列中的列數( _n_steps 來實現。 * n_features_ )。 這種方法的局限性在于生成器只允許您預測一個變量。幾乎可以肯定,編寫自己的函數來為 MLP 準備多變量時間序列比使用 TimeseriesGenerator 更好。 樣品的三維結構可以由 CNN 和 LSTM 模型直接使用。下面列出了使用 TimeseriesGenerator 進行多變量時間序列預測的完整示例。 ```py # multivariate one step problem with lstm from numpy import array from numpy import hstack from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.preprocessing.sequence import TimeseriesGenerator # define dataset in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95, 105]) # reshape series in_seq1 = in_seq1.reshape((len(in_seq1), 1)) in_seq2 = in_seq2.reshape((len(in_seq2), 1)) # horizontally stack columns dataset = hstack((in_seq1, in_seq2)) # define generator n_features = dataset.shape[1] n_input = 2 generator = TimeseriesGenerator(dataset, dataset, length=n_input, batch_size=8) # define model model = Sequential() model.add(LSTM(100, activation='relu', input_shape=(n_input, n_features))) model.add(Dense(2)) model.compile(optimizer='adam', loss='mse') # fit model model.fit_generator(generator, steps_per_epoch=1, epochs=500, verbose=0) # make a one step prediction out of sample x_input = array([[90, 95], [100, 105]]).reshape((1, n_input, n_features)) yhat = model.predict(x_input, verbose=0) print(yhat) ``` 運行該示例準備數據,擬合模型,并預測每個輸入時間序列中的下一個值,我們期望[110,115]。 ```py [[111.03207 116.58153]] ``` ## 多變量輸入和相關系列示例 存在多變量時間序列問題,其中存在一個或多個輸入序列和要預測的單獨輸出序列,其取決于輸入序列。 為了使這個具體,我們可以設計一個帶有兩個輸入時間序列和一個輸出系列的例子,它是輸入系列的總和。 ```py # define dataset in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95, 105]) out_seq = array([25, 45, 65, 85, 105, 125, 145, 165, 185, 205]) ``` 其中輸出序列中的值是輸入時間序列中同一時間步長的值的總和。 ```py 10 + 15 = 25 ``` 這與先前的示例不同,在給定輸入的情況下,我們希望預測下一時間步的目標時間序列中的值,而不是與輸入相同的時間步長。 例如,我們想要樣本: ```py X, y [10, 15], 25 [20, 25], 45 [30, 35], 65 ... ``` 我們不希望樣品如下: ```py X, y [10, 15], 45 [20, 25], 65 [30, 35], 85 ... ``` 盡管如此,TimeseriesGenerator 類假定我們正在預測下一個時間步驟,并將提供數據,如上面的第二種情況。 例如: ```py # multivariate one step problem from numpy import array from numpy import hstack from keras.preprocessing.sequence import TimeseriesGenerator # define dataset in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95, 105]) out_seq = array([25, 45, 65, 85, 105, 125, 145, 165, 185, 205]) # reshape series 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)) # define generator n_input = 1 generator = TimeseriesGenerator(dataset, out_seq, length=n_input, batch_size=1) # print each sample for i in range(len(generator)): x, y = generator[i] print('%s => %s' % (x, y)) ``` 運行該示例打印樣本的輸入和輸出部分,其具有下一時間步的輸出值而不是當前時間步,因為我們可能期望這種類型的問題。 ```py [[[10\. 15.]]] => [[45.]] [[[20\. 25.]]] => [[65.]] [[[30\. 35.]]] => [[85.]] [[[40\. 45.]]] => [[105.]] [[[50\. 55.]]] => [[125.]] [[[60\. 65.]]] => [[145.]] [[[70\. 75.]]] => [[165.]] [[[80\. 85.]]] => [[185.]] [[[90\. 95.]]] => [[205.]] ``` 因此,我們可以修改目標序列( _out_seq_ )并在開頭插入一個額外的值,以便將所有觀察值向下推一步。 這種人為的轉變將允許優選的問題框架。 ```py # shift the target sample by one step out_seq = insert(out_seq, 0, 0) ``` 下面提供了這種轉變的完整示例。 ```py # multivariate one step problem from numpy import array from numpy import hstack from numpy import insert from keras.preprocessing.sequence import TimeseriesGenerator # define dataset in_seq1 = array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) in_seq2 = array([15, 25, 35, 45, 55, 65, 75, 85, 95, 105]) out_seq = array([25, 45, 65, 85, 105, 125, 145, 165, 185, 205]) # reshape series 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)) # shift the target sample by one step out_seq = insert(out_seq, 0, 0) # define generator n_input = 1 generator = TimeseriesGenerator(dataset, out_seq, length=n_input, batch_size=1) # print each sample for i in range(len(generator)): x, y = generator[i] print('%s => %s' % (x, y)) ``` 運行該示例顯示問題的首選框架。 無論輸入樣本的長度如何,此方法都將起作用。 ```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.] ``` ## 多步預測示例 與許多其他類型的經典和機器學習模型相比,神經網絡模型的一個好處是它們可以進行多步預測。 也就是說,模型可以學習將一個或多個特征的輸入模式映射到多于一個特征的輸出模式。這可用于時間序列預測,以直接預測多個未來時間步驟。 這可以通過直接從模型輸出向量,通過將所需數量的輸出指定為輸出層中的節點數來實現,或者可以通過諸如編碼器 - 解碼器模型的專用序列預測模型來實現。 TimeseriesGenerator 的一個限制是它不直接支持多步輸出。具體而言,它不會創建目標序列中可能需要的多個步驟。 然而,如果您準備目標序列有多個步驟,它將尊重并使用它們作為每個樣本的輸出部分。這意味著你有責任為每個時間步驟準備預期的輸出。 我們可以通過一個簡單的單變量時間序列來證明這一點,在輸出序列中有兩個時間步長。 您可以看到目標序列中的行數必須與輸入序列中的行數相同。在這種情況下,我們必須知道輸入序列中的值之外的值,或者將輸入序列修剪為目標序列的長度。 ```py # define dataset series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) target = array([[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10],[10,11]]) ``` 下面列出了完整的示例。 ```py # univariate multi-step problem from numpy import array from keras.preprocessing.sequence import TimeseriesGenerator # define dataset series = array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) target = array([[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10],[10,11]]) # define generator n_input = 2 generator = TimeseriesGenerator(series, target, length=n_input, batch_size=1) # print each sample for i in range(len(generator)): x, y = generator[i] print('%s => %s' % (x, y)) ``` 運行該示例打印樣本的輸入和輸出部分,顯示兩個滯后觀察值作為輸入,兩個步驟作為多步驟預測問題的輸出。 ```py [[1\. 2.]] => [[3\. 4.]] [[2\. 3.]] => [[4\. 5.]] [[3\. 4.]] => [[5\. 6.]] [[4\. 5.]] => [[6\. 7.]] [[5\. 6.]] => [[7\. 8.]] [[6\. 7.]] => [[8\. 9.]] [[7\. 8.]] => [[ 9\. 10.]] [[8\. 9.]] => [[10\. 11.]] ``` ## 進一步閱讀 如果您希望深入了解,本節將提供有關該主題的更多資源。 * [如何將時間序列轉換為 Python 中的監督學習問題](https://machinelearningmastery.com/convert-time-series-supervised-learning-problem-python/) * [TimeseriesGenerator Keras API](https://keras.io/preprocessing/sequence/) * [序列 Keras API](https://keras.io/utils/#sequence) * [順序模型 Keras API](https://keras.io/models/sequential/) * [Python Generator](https://wiki.python.org/moin/Generators) ## 摘要 在本教程中,您了解了如何使用 Keras TimeseriesGenerator 準備時間序列數據,以便使用深度學習方法進行建模。 具體來說,你學到了: * 如何定義 TimeseriesGenerator 生成器并將其用于適合深度學習模型。 * 如何為單變量時間序列準備發電機并適合 MLP 和 LSTM 模型。 * 如何為多變量時間序列準備生成器并適合 LSTM 模型。 你有任何問題嗎? 在下面的評論中提出您的問題,我會盡力回答。
                  <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>

                              哎呀哎呀视频在线观看