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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # LSTM自動編碼器的溫和介紹 > 原文: [https://machinelearningmastery.com/lstm-autoencoders/](https://machinelearningmastery.com/lstm-autoencoders/) LSTM自動編碼器是使用編碼器 - 解碼器LSTM架構的用于序列數據的自動編碼器的實現。 一旦擬合,模型的編碼器部分可用于編碼或壓縮序列數據,而序列數據又可用于數據可視化或作為監督學習模型的特征向量輸入。 在這篇文章中,您將發現LSTM Autoencoder模型以及如何使用Keras在Python中實現它。 閱讀這篇文章后,你會知道: * 自動編碼器是一種自我監督的學習模型,可以學習輸入數據的壓縮表示。 * LSTM自動編碼器可以學習序列數據的壓縮表示,并且已經用于視頻,文本,音頻和時間序列序列數據。 * 如何使用Keras深度學習庫在Python中開發LSTM Autoencoder模型。 讓我們開始吧。 ![A Gentle Introduction to LSTM Autoencoders](img/a1145896d29c99171989ea64360abd88.jpg) LSTM自動編碼器 的溫和介紹 [Ken Lund](https://www.flickr.com/photos/kenlund/2379512850/) 的照片,保留一些權利。 ## 概觀 這篇文章分為六個部分;他們是: 1. 什么是自動編碼器? 2. 序列問題 3. 編碼器 - 解碼器LSTM模型 4. 什么是LSTM自動編碼器? 5. LSTM自動編碼器的早期應用 6. 如何在Keras中創建LSTM自動編碼器 ## 什么是自動編碼器? [自動編碼器](https://en.wikipedia.org/wiki/Autoencoder)是一種神經網絡模型,旨在學習輸入的壓縮表示。 它們是一種無監督的學習方法,盡管從技術上講,它們是使用有監督的學習方法訓練的,稱為自我監督。它們通常作為試圖重新創建輸入的更廣泛模型的一部分進行訓練。 例如: ```py X = model.predict(X) ``` 自動編碼器模型的設計有目的地通過將架構限制在模型的中點處的瓶頸來實現這種挑戰,從中執行輸入數據的重建。 有許多類型的自動編碼器,它們的使用各不相同,但更常見的用途可能是學習或自動特征提取模型。 在這種情況下,一旦模型擬合,就可以丟棄模型的重建方面,并且可以使用直到瓶頸點的模型。瓶頸處的模型輸出是固定長度向量,其提供輸入數據的壓縮表示。 然后可以將來自域的輸入數據提供給模型,并且瓶頸處的模型的輸出可以用作監督學習模型中的特征向量,用于可視化,或者更一般地用于降低維度。 ## 序列問題 序列預測問題具有挑戰性,尤其是因為輸入序列的長度可以變化。 這具有挑戰性,因為機器學習算法,特別是神經網絡,設計用于固定長度輸入。 序列數據的另一個挑戰是觀察的時間順序可能使得提取適合用作監督學習模型的輸入的特征具有挑戰性,通常需要在領域或信號處理領域的深入專業知識。 最后,涉及序列的許多預測建模問題需要預測其本身也是序列。這些被稱為序列到序列或seq2seq預測問題。 您可以在此處詳細了解序列預測問題: * [用序列進行預測](https://machinelearningmastery.com/sequence-prediction/) ## 編碼器 - 解碼器LSTM模型 諸如長短期存儲器或LSTM網絡的循環神經網絡專門設計用于支持輸入數據序列。 它們能夠學習輸入序列的時間排序中的復雜動態,以及使用內部存儲器來記憶或使用長輸入序列中的信息。 LSTM網絡可以組織成稱為編碼器 - 解碼器LSTM的架構,該架構允許該模型用于支持可變長度輸入序列并預測或輸出可變長度輸出序列。 該架構是復雜序列預測問題(如語音識別和文本轉換)中許多進步的基礎。 在該架構中,編碼器LSTM模型逐步讀取輸入序列。在讀入整個輸入序列之后,該模型的隱藏狀態或輸出將整個輸入序列的內部學習表示表示為固定長度向量。然后將該向量作為輸入提供給解碼器模型,該解碼器模型在生成輸出序列中的每個步驟時對其進行解釋。 您可以在此處了解有關編碼器 - 解碼器架構的更多信息 * [編碼器 - 解碼器長短期存儲器網絡](https://machinelearningmastery.com/encoder-decoder-long-short-term-memory-networks/) ## 什么是LSTM自動編碼器? LSTM自動編碼器是使用編碼器 - 解碼器LSTM架構的用于序列數據的自動編碼器的實現。 對于給定的序列數據集,編碼器 - 解碼器LSTM被配置為讀取輸入序列,對其進行編碼,對其進行解碼并重新創建它。基于模型重新創建輸入序列的能力來評估模型的表現。 一旦模型達到重建序列的所需表現水平,就可以移除模型的解碼器部分,僅留下編碼器模型。然后,該模型可用于將輸入序列編碼為固定長度的向量。 然后,所得到的向量可以用于各種應用中,尤其是作為序列的壓縮表示,作為另一個監督學習模型的輸入。 ## LSTM自動編碼器的早期應用 LSTM Autoencoder的早期和廣泛引用的應用之一是在2015年的論文“[使用LSTM的視頻表示的無監督學習](https://arxiv.org/abs/1502.04681)”。 ![LSTM Autoencoder Model](img/88127e694b4a784f9ad9742b19e19fcc.jpg) LSTM自動編碼器模型 取自“使用LSTM的無監督學習視頻表示” 在論文中,Nitish Srivastava等人。將LSTM Autoencoder描述為編碼器 - 解碼器LSTM的擴展或應用。 他們使用具有視頻輸入數據的模型來重建視頻幀的序列以及預測視頻幀,這兩者都被描述為無監督學習任務。 > 模型的輸入是一系列向量(圖像塊或特征)。編碼器LSTM以此順序讀取。在讀取了最后一個輸入之后,解碼器LSTM接管并輸出對目標序列的預測。 - [使用LSTM的視頻表示的無監督學習](https://arxiv.org/abs/1502.04681),2015。 作者不僅僅是直接使用模型,而是探索一些有趣的架構選擇,這些選擇可能有助于為模型的未來應用提供信息。 他們設計模型的方式是以相反的順序重新創建視頻幀的目標序列,聲稱它使得模型解決的優化問題更容易處理。 > 目標序列與輸入序列相同,但順序相反。反轉目標序列使得優化更容易,因為模型可以通過查看低范圍相關性來實現。 - [使用LSTM的視頻表示的無監督學習](https://arxiv.org/abs/1502.04681),2015。 他們還探討了訓練解碼器模型的兩種方法,特別是在解碼器生成的先前輸出中調節的版本,以及沒有任何這種調節的另一種方法。 > 解碼器可以是兩種 - 有條件的或無條件的。條件解碼器接收最后生成的輸出幀作為輸入[...]。無條件解碼器不接收該輸入。 - [使用LSTM的視頻表示的無監督學習](https://arxiv.org/abs/1502.04681),2015。 還研究了更精細的自動編碼器模型,其中兩個解碼器模型用于一個編碼器:一個用于預測序列中的下一幀,一個用于重建序列中的幀,稱為復合模型。 > ...重建輸入和預測未來可以組合起來創建一個復合[...]。在這里,編碼器LSTM被要求提出一種狀態,我們既可以預測接下來的幾幀,也可以重建輸入。 - [使用LSTM的視頻表示的無監督學習](https://arxiv.org/abs/1502.04681),2015。 ![LSTM Autoencoder Model With Two Decoders](img/7979859a410719d5f2f8c0ef347fe9fd.jpg) 具有兩個解碼器的LSTM自動編碼器模型 取自“使用LSTM的視頻表示的無監督學習” 模型以多種方式進行評估,包括使用編碼器來播種分類器。看來,不是使用編碼器的輸出作為分類輸入,而是選擇直接使用編碼器模型的權重為獨立的LSTM分類器播種。鑒于實施的復雜性,這是令人驚訝的。 > 我們使用編碼器LSTM從該模型中學習的權重初始化LSTM分類器。 - [使用LSTM的視頻表示的無監督學習](https://arxiv.org/abs/1502.04681),2015。 發現在解碼器上沒有調節的復合模型在他們的實驗中表現最佳。 > 表現最佳的模型是組合自動編碼器和未來預測器的復合模型。條件變量在微調后的分類準確度方面沒有給出任何顯著的改進,但是它們確實給出了略低的預測誤差。 - [使用LSTM的視頻表示的無監督學習](https://arxiv.org/abs/1502.04681),2015。 已經演示了LSTM自動編碼器的許多其他應用,尤其是文本序列,音頻數據和時間序列。 ## 如何在Keras中創建LSTM自動編碼器 在Keras中創建LSTM自動編碼器可以通過實現編碼器 - 解碼器LSTM架構并配置模型來重新創建輸入序列來實現。 讓我們看幾個例子來說明這一點。 ### 重建LSTM自動編碼器 最簡單的LSTM自動編碼器是學習重建每個輸入序列的自動編碼器。 對于這些演示,我們將使用九個時間步驟和一個特征的一個樣本的數據集: ```py [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] ``` 我們可以通過定義序列并將其重新整形為[_樣本,時間步長,特征_]的首選形狀來開始。 ```py # define input sequence sequence = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) # reshape input into [samples, timesteps, features] n_in = len(sequence) sequence = sequence.reshape((1, n_in, 1)) ``` 接下來,我們可以定義編碼器 - 解碼器LSTM架構,該架構期望輸入序列具有九個時間步長和一個特征,并輸出具有九個時間步長和一個特征的序列。 ```py # define model model = Sequential() model.add(LSTM(100, activation='relu', input_shape=(n_in,1))) model.add(RepeatVector(n_in)) model.add(LSTM(100, activation='relu', return_sequences=True)) model.add(TimeDistributed(Dense(1))) model.compile(optimizer='adam', loss='mse') ``` 接下來,我們可以將模型擬合到我們設計的數據集上。 ```py # fit model model.fit(sequence, sequence, epochs=300, verbose=0) ``` 下面列出了完整的示例。 模型的配置,例如單元數和訓練時期,完全是任意的。 ```py # lstm autoencoder recreate sequence from numpy import array from keras.models import Sequential from keras.layers import LSTM from keras.layers import Dense from keras.layers import RepeatVector from keras.layers import TimeDistributed from keras.utils import plot_model # define input sequence sequence = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) # reshape input into [samples, timesteps, features] n_in = len(sequence) sequence = sequence.reshape((1, n_in, 1)) # define model model = Sequential() model.add(LSTM(100, activation='relu', input_shape=(n_in,1))) model.add(RepeatVector(n_in)) model.add(LSTM(100, activation='relu', return_sequences=True)) model.add(TimeDistributed(Dense(1))) model.compile(optimizer='adam', loss='mse') # fit model model.fit(sequence, sequence, epochs=300, verbose=0) plot_model(model, show_shapes=True, to_file='reconstruct_lstm_autoencoder.png') # demonstrate recreation yhat = model.predict(sequence, verbose=0) print(yhat[0,:,0]) ``` 運行該示例適合自動編碼器并打印重建的輸入序列。 結果足夠接近,非常小的舍入誤差。 ```py [0.10398503 0.20047213 0.29905337 0.3989646 0.4994707 0.60005534 0.70039135 0.80031013 0.8997728 ] ``` 創建架構圖以供參考。 ![LSTM Autoencoder for Sequence Reconstruction](img/5c47ad60098371eb69ae603fe30ad184.jpg) 用于序列重建的LSTM自動編碼器 ### 預測LSTM自動編碼器 我們可以修改重建LSTM Autoencoder來預測序列中的下一步。 在我們的小設計問題的情況下,我們期望輸出是序列: ```py [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] ``` 這意味著模型將期望每個輸入序列具有九個時間步長,并且輸出序列具有八個時間步長。 ```py # reshape input into [samples, timesteps, features] n_in = len(seq_in) seq_in = seq_in.reshape((1, n_in, 1)) # prepare output sequence seq_out = seq_in[:, 1:, :] n_out = n_in - 1 ``` 下面列出了完整的示例。 ```py # lstm autoencoder predict sequence from numpy import array from keras.models import Sequential from keras.layers import LSTM from keras.layers import Dense from keras.layers import RepeatVector from keras.layers import TimeDistributed from keras.utils import plot_model # define input sequence seq_in = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) # reshape input into [samples, timesteps, features] n_in = len(seq_in) seq_in = seq_in.reshape((1, n_in, 1)) # prepare output sequence seq_out = seq_in[:, 1:, :] n_out = n_in - 1 # define model model = Sequential() model.add(LSTM(100, activation='relu', input_shape=(n_in,1))) model.add(RepeatVector(n_out)) model.add(LSTM(100, activation='relu', return_sequences=True)) model.add(TimeDistributed(Dense(1))) model.compile(optimizer='adam', loss='mse') plot_model(model, show_shapes=True, to_file='predict_lstm_autoencoder.png') # fit model model.fit(seq_in, seq_out, epochs=300, verbose=0) # demonstrate prediction yhat = model.predict(seq_in, verbose=0) print(yhat[0,:,0]) ``` 運行該示例將打印輸出序列,該序列預測每個輸入時間步的下一個時間步。 我們可以看到模型是準確的,除了一些小的舍入誤差。 ```py [0.1657285 0.28903174 0.40304852 0.5096578 0.6104322 0.70671254 0.7997272 0.8904342 ] ``` 創建架構圖以供參考。 ![LSTM Autoencoder for Sequence Prediction](img/bd8570b95f40c8577397e53902062617.jpg) 用于序列預測的LSTM自動編碼器 ### 復合LSTM自動編碼器 最后,我們可以創建一個復合LSTM自動編碼器,它具有一個編碼器和兩個解碼器,一個用于重建,另一個用于預測。 我們可以使用功能API在Keras中實現這個多輸出模型。您可以在此帖子中了解有關功能API的更多信息: * [如何使用Keras功能API進行深度學習](https://machinelearningmastery.com/keras-functional-api-deep-learning/) 首先,定義編碼器。 ```py # define encoder visible = Input(shape=(n_in,1)) encoder = LSTM(100, activation='relu')(visible) ``` 然后是第一個用于重建的解碼器。 ```py # define reconstruct decoder decoder1 = RepeatVector(n_in)(encoder) decoder1 = LSTM(100, activation='relu', return_sequences=True)(decoder1) decoder1 = TimeDistributed(Dense(1))(decoder1) ``` 然后是用于預測的第二個解碼器。 ```py # define predict decoder decoder2 = RepeatVector(n_out)(encoder) decoder2 = LSTM(100, activation='relu', return_sequences=True)(decoder2) decoder2 = TimeDistributed(Dense(1))(decoder2) ``` 然后我們將整個模型聯系在一起。 ```py # tie it together model = Model(inputs=visible, outputs=[decoder1, decoder2]) ``` 下面列出了完整的示例。 ```py # lstm autoencoder reconstruct and predict sequence from numpy import array from keras.models import Model from keras.layers import Input from keras.layers import LSTM from keras.layers import Dense from keras.layers import RepeatVector from keras.layers import TimeDistributed from keras.utils import plot_model # define input sequence seq_in = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) # reshape input into [samples, timesteps, features] n_in = len(seq_in) seq_in = seq_in.reshape((1, n_in, 1)) # prepare output sequence seq_out = seq_in[:, 1:, :] n_out = n_in - 1 # define encoder visible = Input(shape=(n_in,1)) encoder = LSTM(100, activation='relu')(visible) # define reconstruct decoder decoder1 = RepeatVector(n_in)(encoder) decoder1 = LSTM(100, activation='relu', return_sequences=True)(decoder1) decoder1 = TimeDistributed(Dense(1))(decoder1) # define predict decoder decoder2 = RepeatVector(n_out)(encoder) decoder2 = LSTM(100, activation='relu', return_sequences=True)(decoder2) decoder2 = TimeDistributed(Dense(1))(decoder2) # tie it together model = Model(inputs=visible, outputs=[decoder1, decoder2]) model.compile(optimizer='adam', loss='mse') plot_model(model, show_shapes=True, to_file='composite_lstm_autoencoder.png') # fit model model.fit(seq_in, [seq_in,seq_out], epochs=300, verbose=0) # demonstrate prediction yhat = model.predict(seq_in, verbose=0) print(yhat) ``` 運行該示例,使用兩個解碼器重建并預測輸出序列。 ```py [array([[[0.10736275], [0.20335874], [0.30020815], [0.3983948 ], [0.4985725 ], [0.5998295 ], [0.700336 , [0.8001949 ], [0.89984304]]], dtype=float32), array([[[0.16298929], [0.28785267], [0.4030449 ], [0.5104638 ], [0.61162543], [0.70776784], [0.79992455], [0.8889787 ]]], dtype=float32)] ``` 創建架構圖以供參考。 ![Composite LSTM Autoencoder for Sequence Reconstruction and Prediction](img/03ca4b90546e7dde0560ddc2d03a5cf7.jpg) 用于序列重建和預測的復合LSTM自動編碼器 ### 保留獨立LSTM編碼器 無論選擇哪種方法(重建,預測或復合),一旦自適應編碼器已經適合,就可以移除解碼器并且可以將編碼器保持為獨立模型。 然后,編碼器可用于將輸入序列變換為固定長度的編碼向量。 我們可以通過創建一個與原始模型具有相同輸入的新模型,并在 _RepeatVector_ 層之前直接從編碼器模型的末尾輸出。 ```py # connect the encoder LSTM as the output layer model = Model(inputs=model.inputs, outputs=model.layers[0].output) ``` 下面列出了使用重建LSTM自動編碼器執行此操作的完整示例。 ```py # lstm autoencoder recreate sequence from numpy import array from keras.models import Sequential from keras.models import Model from keras.layers import LSTM from keras.layers import Dense from keras.layers import RepeatVector from keras.layers import TimeDistributed from keras.utils import plot_model # define input sequence sequence = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) # reshape input into [samples, timesteps, features] n_in = len(sequence) sequence = sequence.reshape((1, n_in, 1)) # define model model = Sequential() model.add(LSTM(100, activation='relu', input_shape=(n_in,1))) model.add(RepeatVector(n_in)) model.add(LSTM(100, activation='relu', return_sequences=True)) model.add(TimeDistributed(Dense(1))) model.compile(optimizer='adam', loss='mse') # fit model model.fit(sequence, sequence, epochs=300, verbose=0) # connect the encoder LSTM as the output layer model = Model(inputs=model.inputs, outputs=model.layers[0].output) plot_model(model, show_shapes=True, to_file='lstm_encoder.png') # get the feature vector for the input sequence yhat = model.predict(sequence) print(yhat.shape) print(yhat) ``` 運行該示例將創建一個獨立的編碼器模型,可以使用或保存以供以后使用。 我們通過預測序列并返回編碼器的100個元素輸出來演示編碼器。 顯然,這對于我們微小的九步輸入序列來說太過分了。 ```py [[0.03625513 0.04107533 0.10737951 0.02468692 0.06771207 0. 0.0696108 0\. 0\. 0.0688471 0\. 0. 0\. 0\. 0\. 0\. 0\. 0.03871286 0\. 0\. 0.05252134 0\. 0.07473809 0.02688836 0\. 0\. 0\. 0\. 0\. 0.0460703 0\. 0\. 0.05190025 0\. 0\. 0.11807001 0\. 0\. 0\. 0\. 0\. 0. 0\. 0.14514188 0\. 0\. 0\. 0. 0.02029926 0.02952124 0\. 0\. 0\. 0. 0\. 0.08357017 0.08418129 0\. 0\. 0. 0\. 0\. 0.09802645 0.07694854 0\. 0.03605933 0\. 0.06378153 0\. 0.05267526 0.02744672 0. 0.06623861 0\. 0\. 0\. 0.08133873 0.09208347 0.03379713 0\. 0\. 0\. 0.07517676 0.08870222 0\. 0\. 0\. 0\. 0.03976351 0.09128518 0.08123557 0\. 0.08983088 0.0886112 0\. 0.03840019 0.00616016 0.0620428 0\. 0\. ] ``` 創建架構圖以供參考。 ![Standalone Encoder LSTM Model](img/72a88a13c5cd68011d29785ef96029d4.jpg) 獨立編碼器LSTM模型 ## 進一步閱讀 如果您希望深入了解,本節將提供有關該主題的更多資源。 * [用序列進行預測](https://machinelearningmastery.com/sequence-prediction/) * [編碼器 - 解碼器長短期存儲器網絡](https://machinelearningmastery.com/encoder-decoder-long-short-term-memory-networks/) * [自動編碼器,維基百科](https://en.wikipedia.org/wiki/Autoencoder) * [使用LSTM進行視頻表示的無監督學習](https://arxiv.org/abs/1502.04681),ArXiv 2015。 * [使用LSTM進行視頻表示的無監督學習](http://proceedings.mlr.press/v37/srivastava15.pdf),PMLR,PDF,2015。 * [使用LSTM](https://github.com/emansim/unsupervised-videos) ,GitHub存儲庫進行視頻表示的無監督學習。 * [在Keras建立自動編碼器](https://blog.keras.io/building-autoencoders-in-keras.html),2016年。 * [如何使用Keras功能API進行深度學習](https://machinelearningmastery.com/keras-functional-api-deep-learning/) ## 摘要 在這篇文章中,您發現了LSTM Autoencoder模型以及如何使用Keras在Python中實現它。 具體來說,你學到了: * 自動編碼器是一種自我監督的學習模型,可以學習輸入數據的壓縮表示。 * LSTM自動編碼器可以學習序列數據的壓縮表示,并且已經用于視頻,文本,音頻和時間序列序列數據。 * 如何使用Keras深度學習庫在Python中開發LSTM Autoencoder模型。 你有任何問題嗎? 在下面的評論中提出您的問題,我會盡力回答。
                  <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>

                              哎呀哎呀视频在线观看