深度學習:長短期記憶模型LSTM的變體和拓展(GRU模型等)

http://blog.csdn.net/pipisorry/article/details/78362537

原始的LSTM

[深度學習:長短期記憶模型LSTM]

LSTM模型的拓展

[Greff, Klaus, et al. "LSTM: A search space odyssey." TNNLS2016] 探討了基於Vanilla LSTM (Graves & Schmidhube (2005))之上的8個變體,並比較了它們之間的性能差異,包括:

  1. 沒有輸入門 (No Input Gate, NIG)
  2. 沒有遺忘門 (No Forget Gate, NFG)
  3. 沒有輸出門 (No Output Gate, NOG)
  4. 沒有輸入激活函數 (No Input Activation Function, NIAF) (也就是沒有輸入門對應的tanh層)
  5. 沒有輸出激活函數 (No Output Activation Function, NOAF) (也就是沒有輸出門對應的tanh層)
  6. 沒有"peephole connection" (No Peepholes, NP)
  7. 遺忘門與輸入門結合 (Coupled Input and Forget Gate, CIFG)
  8. Full Gate Recurrence (FGR)。The FGR variant adds recurrent connections between all the gates (nine additional recurrent weight matrices).

關於不同變體的對比

[Greff, Klaus, et al. "LSTM: A search space odyssey." TNNLS2016]將8種LSTM變體與基本的Vanilla LSTM在TIMIT語音識別、手寫字符識別、復調音樂建模三個應用中的表現情況進行了比較,得出了幾個有趣的結論:

  1. Vanilla LSTM在所有的應用中都有良好的表現,其他8個變體並沒有什麼性能提升;
  2. 將遺忘門與輸出門結合 (Coupled Input and Forget Gate, CIFG)以及沒有"peephole connection" (No Peepholes, NP)簡化了LSTM的結構,而且並不會對結果產生太大影響;
  3. 遺忘門和輸出門是LSTM結構最重要的兩個部分,其中遺忘門對LSTM的性能影響十分關鍵,輸出門 is necessary whenever the cell state is unbounded (用來限制輸出結果的邊界);
  4. 學習率和隱含層個數是LSTM最主要的調節參數,而動量因子被發現影響不大,高斯噪音的引入對TIMIT實驗性能提升顯著,而對於另外兩個實驗則會帶來反效果;
  5. 超參分析表明學習率與隱含層個數之間並沒有什麼關係,因此可以獨立調參,另外,學習率可以先使用一個小的網絡結構進行校準,這樣可以節省很多時間。

[[NL系列] RNN & LSTM 網絡結構及應用]

原始LSTM:不帶忘記門的lstm block

         

[Hochreiter, S., & Schmidhuber, J. Long short-term memory. Neural computation1997] 

                                      

[LSTM memory cell as initially described in Hochreiter [Hochreiter and Schmidhuber, 1997]

現在常說的 LSTM 有 Forget Gate,是由 Gers 在"Learning to Forget: Continual Prediction with LSTM, 2000"中提出的改進版本。

後來,在"LSTM Recurrent Networks Learn Simple Context Free and Context Sensitive Languages, 2001"中 Gers 又加入了 Peephole Connection 的概念。

耦合的輸入和遺忘門/遺忘門與輸入門結合 (Coupled Input and Forget Gate, CIFG)

變成

CIFG可以達到a 20% reduction in computation time (Greff et al., 2016).

某小皮

 

 

其它變型

門控循環單元GRU模型

        GRU即門控循環單元/Gated Recurrent Unit。GRU保持了LSTM的效果同時又使結構更加簡單計算量更小。GRU把LSTM中的forget gateinput gateupdate gate來替代。 把cell state和隱狀態htht進行合併,在計算當前時刻新信息的方法和LSTM有所不同。 

        GRU模型只有兩個門了,分別爲更新門zt和重置門rt。更新門用於控制前一時刻的狀態信息被帶入到當前狀態中的程度,更新門的值越大說明前一時刻的狀態信息帶入越多;重置門用於控制忽略前一時刻的狀態信息的程度,重置門的值越小說明忽略得越多。

        GRU前向傳播公式

其中[]表示兩個向量相連接,*表示矩陣元素相乘。

Note: 有些地方zt和(1 - zt)是相反的,這個順序需要注意一下,比如下面這個等同的圖示。

具體更新過程如下:

 

        如果reset gate接近0,那麼之前的隱藏層信息就會丟棄,允許模型丟棄一些和未來無關 的信息;update gate控制當前時刻的隱藏層輸出ht需要保留多少之前的隱藏層信息, 若zt接近1相當於我們之前把之前的隱藏層信息拷貝到當前時刻,可以學習長距離依賴。 一般來說那些具有短距離依賴的單元reset gate比較活躍(如果rt爲1,而zt爲0 那麼相當於變成了一個標準的RNN,能處理短距離依賴),具有長距離依賴的單元update gate比較活躍。

LSTM與GRU對比

1 門數不同。GRU只有兩個門reset門r和update門z。
2 在GRU中,r和z共同控制瞭如何從之前的隱藏狀態(st−1st−1)計算獲得新的隱藏狀態(stst),而取消了LSTM中的output門。
3 如果reset門爲1,而update門爲0(不同圖示中可能爲1)的話,則GRU完全退化爲一個RNN

4 經過實驗,一般認爲,LSTM和GRU之間並沒有明顯的優勝者。因爲GRU具有較少的參數,所以訓練速度快,而且所需要的樣本也比較少。而LSTM具有較多的參數,比較適合具有大量樣本的情況,可能會獲得較優的模型。

[經典必讀:門控循環單元(GRU)的基本概念與原理]

[RNN LSTM與GRU深度學習模型學習筆記]

[GRU 原論文:https://arxiv.org/pdf/1406.1078v3.pdf]

Recurrent Dropout without Memory Loss

the cell and hidden state update equations for LSTM will incorporate a single dropout (Hinton et al., 2012) gate, as developed in Recurrent Dropout without Memory Loss (Semeniuta et al., 2016), to help regularize the entire model during training.

 DropOut()

[Stanislaw Semeniuta, Aliases Severyn, and Erhardt Barth. Recurrent dropout without memory loss. arXiv 2016]

Layer Normalization

add the option to use a Layer Normalization layer in the LSTM

i^t = σ(LN())

i g f o s^t都需要LN。

Layer Normalization (Ba et al., 2016[Jimmy L. Ba, Jamie R. Kiros, and Geoffrey E. Hinton. Layer normalization. NIPS2016]). The central idea for the normalization techniques is to calculate the first two statistical moments of the inputs to the activation function, and to linearly scale the inputs to have zero mean and unit variance.

增加網絡層數 Going Deep

既然LSTM(RNN)是神經網絡,我們自然可以把一個個LSTM疊加起來進行深度學習。

比如,我們可以用兩個獨立的LSTM來構造兩層的循環神經網絡:一個LSTM接收輸入向量,另一個將前一個LSTM的輸出作爲輸入。這兩個LSTM沒有本質區別——這不外乎就是向量的輸入輸出而已,而且在反向傳播過程中每個模塊都伴隨着梯度操作。

[Gated Feedback Recurrent Neural Networks]

左邊是傳統的stack RNN(三個RNN疊加),右邊是升級版——RNN的hidden state除了會對下一時刻自己產生影響外,還會對其他的RNN產生影響,由reset gates控制。

深層LSTM模型

類似於深層RNN,使用多層的LSTM模型。著名的seq2seq模型使用的就是2層的LSTM來進行encode和decode的。multilayered Long Short-Term Memory (LSTM) to map the input sequence to a vector of a fixed dimensionality。

[Ilya Sutskever, Oriol Vinyals, and Quoc VV Le. Sequence to sequence learning with neural networks. In NIPS, 2014.]

具有2個cell的LSTM模型

一般時間序列只需要一個block,而空間序列可能需要多個block?如下圖。

2個cell的lstm模型就類似於rnn模型中的”動態跨時間步的rnn網絡“。

Figure 2: Example of a net with 8 input units, 4 output units, and 2 memory cell blocks of size 2.
in 1 marks the input gate, out 1 marks the output gate, and cell 1 =block 1 marks the rst memory cell of block 1. cell 1 =block 1 's architecture is identical to the one in Figure 1, with gate units in 1 and out 1

The example assumes dense connectivity: each gate unit and each memory cell see all non-output units.

[Hochreiter, S., & Schmidhuber, J. Long short-term memory. Neural computation1997] 

[LSTM模型]

​帶狀態 (stateful) 的 LSTM 模型

        stateful LSTM:能讓模型學習到你輸入的samples之間的時序特徵,適合一些長序列的預測,哪個sample在前,那個sample在後對模型是有影響的。

        stateless LSTM:輸入samples後,默認就會shuffle,可以說是每個sample獨立,之間無前後關係,適合輸入一些沒有關係的樣本。

        在stateless時,長期記憶網絡並不意味着你的LSTM將記住之前batch的內容,它只是記住句子內部的內容。Keras stateless lstm在訓練時會默認地shuffle samples,所以導致sequence之間的依賴性消失,samplesample之間就沒有時序關係,順序被打亂,這時記憶參數在batch、小序列之間進行傳遞就沒意義了,所以Keras要把記憶參數初始化。

        使 RNN 具有狀態意味着每批樣品的狀態將被重新用作下一批樣品的初始狀態。注意,此處的狀態表示的是原論文公式裏的c,h,即LSTM特有的一些記憶參數,並非w權重。

        在keras中使用帶狀態的lstm時,即在stateful = True 時,我們要在fit中手動使得shuffle = False。隨後,在X[i](表示輸入矩陣中第isample)這個小序列訓練完之後,Keras會將將訓練完的記憶參數傳遞給X[i+bs](表示第i+bs個sample),作爲其初始的記憶參數。bs = batch_size。這樣一來,我們的記憶參數就能順利地在samplesample之間傳遞,X[i+n*bs]也能知道X[i]的信息。

[Keras之stateful LSTM全面解析+實例測試]

加入了peephole的 LSTM

換一個圖:

 

        這裏三條黑線就是所謂的 peephole,傳統的 LSTM 中遺忘門、輸入門和輸出門只用了 h(t-1) 和 xt 來控制門縫的大小,peephole 的意思是說不但要考慮 h(t-1) 和 xt,也要考慮 Ct-1 和 Ct,其中遺忘門和輸入門考慮了 Ct-1,而輸出門考慮了 Ct。

[Gers, Felix A., Schraudolph, Nicol N., and Schmidhuber, J¨urgen. Learning precise timing with lstm recurrent networks. J. Mach. Learn. Res., 3:115–143, March 2003.]

from: http://blog.csdn.net/pipisorry/article/details/78362537

ref:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章