Recurrent Neural Network (RNN) with Peephole LSTM's BPTT (Gradient Vanish?)
Outline
- Prerequisite
- Introduction
- Architecture
- Basic Components
- Forward Propagation Through Time
- Backward Propagation Through Time(BPTT)
- Gradient Vanish/Explode
- Why Long Short-Term Memory Can Avoid Gradient Vanish?
- 1997 LSTM without forget gate
- 2000 peephole LSTM with forget gate
- Backward Propagation
- Gradient Calculation
- Summary
Perequisite
Introduction
以往的NN如果有K個sample,則一個個計算出forward activation和backward error來training一個類神經網路.RNN被視為時間序列神經網路,專門用來training sample內的data是具有前後關聯性的. 舉例來說,如果你有K個sample,每個sample有t筆資料(ex: I am Bean),則RNN相當於training t個NN後更新一次權重,重複K次後即完成一次完整的epoch. 但是並不是真的產生t個NN,而是對一個NN依序輸入t筆資料後,計算出t個forward activation和backward error後來更新權重.
Architecture
這是一個神經元的NN

cost function如下
這是t=4的RNN,實際上只有一個神經元,這是四個時間點下的同一個

Basic Components
- Forward Propagation
RNN的cost function定義如下

- Backward Propagation Through Time(BPTT)
由於J = J(0)+J(1)+J(2)+J(3),由chain rule可得知W的推導如下圖所示,以J(3)為例,其餘類推


- Gradient Vanish/Explode
從上圖和上式可發現一個事實,當t很大的時候,連乘\(\prod\)的項數也會很長,這會造成什麼影響?

從上圖可以知道,\(\prod\)是由tanh’和W組成,其中tanh’介於0-1,因此如果|W| < 1,連乘的結果會趨近於0,也就是說第一項X(0)能對J(3)梯度造成的影響一定很小(gradient vanish),這不一定合理,因為X(0)也許是很重要的關鍵字. 反之|W|>1,則有可能變很大(gradient explode). 因此1997年有人提出LSTM解決這個問題
Why Long Short-Term Memory Can Avoid Gradient Vanish?
- 1997 LSTM without forget gate
1997提出的LSTM without forget gate為什麼可以解決gradient vanish的問題呢?

由上圖\(S_{C_{j}}=S_{C_{j-1}}+gy^{in_{j}}\)可知由於tanh改成ReLU,W=1.0,gradient vanish就不會發生,但是連乘項的影響讓gradient explode還是有可能會發生,這個部分可以透過gradient clipping(設threshold)來解決。
- 2000 peephole LSTM with forget gate
現在標準的LSTM是2000年加入的peephole with forget gate的版本有sigmoid和tanh,為什麼也可以解決gradient vanish的問題?先來理解他的Forwardd Propagation推導

下面是參數圖與推導,這是展開圖


- Backward Propagation
\(\delta^{(t)}_{o_{a}}\)公式與示意圖

\(\delta^{(t)}_{f_{a}}\)公式與示意圖

\(\delta^{(t)}_{i_{a}}\)公式與示意圖

\(\delta^{(t)}_{c}\)公式與示意圖,\(\delta^{(t)}_{c}\)共有五個來源,如示意圖所示

\(\delta^{(t)}_{z_{a}}\)公式與示意圖

\(\delta^{(t)}_{y}\)公式與示意圖,j在此代表RNN \(j_{th}\)node

- Gradient Calculation
其他參數的gradient以此類推,j代表RNN \(j_{th}\)node

Summary
最後從W的偏微分可知,gradient是由很多項的\(\delta\) propagation連加而得,組合有tanh’和sigmoid’的乘法,但並非連乘,而且由於最後會由T組連加(包括最複雜的\(\delta^{(t)}_{c}\)是由input, output, forget gate propagation組成),所以最後SUM並不一定會產生gradient vanish or explode。RNN就這麼因為加入了Peephole LSTM而Backward Propagation變複雜連加,gradient vanish和explode而不會直接發生了,但是gradient explode還是有可能因為連加項T過大而間接產生。


…
References
- A Gentle Tutorial of Recurrent Neural Network with Error Backpropagation https://arxiv.org/pdf/1610.02583.pdf
- On the difficulty of training recurrent neural networks http://www.jmlr.org/proceedings/papers/v28/pascanu13.pdf
- VISUALIZING AND UNDERSTANDING RECURRENT NETWORKS http://jmlr.csail.mit.edu/proceedings/papers/v28/pascanu13.pdf
- Long short-term memory in Recurrent Neural Network http://www.felixgers.de/papers/phd.pdf
- 1997 LSTM https://www.researchgate.net/publication/13853244_Long_Short-term_Memory
- LSTM: A Search Space Odyssey https://arxiv.org/pdf/1503.04069.pdf
- Supervised Sequence Labelling with Recurrent Neural Networks https://www.cs.toronto.edu/~graves/preprint.pdf