1. 程式人生 > >Long-Short-Term-Memories(LSTM)

Long-Short-Term-Memories(LSTM)

1.傳統RNN存在的問題

迴圈神經網路一文中,對前向傳播、後向傳播進行了仔細的推導,回顧一下,可以發現傳統RNN很容易產生梯度消失、爆炸的問題。

可能公式會顯得有點抽象,這裡舉一個例子:

那麼,LSTM是如何解決這個問題的呢?

2.RNN && LSTM

如圖1所示,這就是一個傳統的RNN模型,啟用函式用的是tanh


圖1

圖2與圖1擁有類似的網路結構,只不過內部會更加複雜,即包括輸入、忘記、輸出門


圖2

下面,我們來看看LSTM每一個門具體是如何運轉的

3.初識LSTM

在介紹LSTM各個門之間如何運作之前,先看下LSTM網路中圖示的含義,如圖3所示:


圖3

LSTM 通過精心設計稱作為“門”的結構來去除或者增加資訊到細胞狀態的能力。門是一種讓資訊選擇式通過的方法。他們包含一個 sigmoid 函式和一個 pointwise 乘法操作。


圖4

下面,依據圖2從左到右依次介紹:

忘記門(forget gate),利用sigmoid函式將資料轉換到[0,1]區間,從而實現“忘記”。


圖5

輸入門(input gate)


圖6

更新細胞(cell)狀態


圖7

輸出門(output gate)


圖8

至此,也完成了LSTM前向傳播的介紹過程

4.LSTM變體

(1)Gers & Schmidhuber (2000)

增加了 “peephole connection”。即讓門層也會接受細胞狀態的輸入。


圖9

(2)通過使用 coupled 忘記和輸入門。不同於之前是分開確定什麼忘記和需要新增什麼新的資訊,這裡是同時做出決定。僅僅當將要輸入在當前位置時忘記,僅僅輸入新的值到那些已經忘記舊的資訊的那些狀態 。


圖10

(3)Gated Recurrent Unit (GRU),由 Cho, et al. (2014) 提出。它將忘記門和輸入門合成了一個單一的更新門。同樣還混合了細胞狀態和隱藏狀態,和其他一些改動。最終的模型比標準的 LSTM 模型要簡單(減少了一個門),也是非常流行的變體。


圖10

5.換個角度看LSTM、GRU

下面把LSTM、GRU的核心部分分解開來看:

LSTM:


圖11

其中:

GRU:


圖12

其中:

6.前向傳播、反向傳播推導

下面的推導公式來自於參考文獻2

定義:
- is the weight of the connection from unit i to unit j
- the network input to unit j at time t is denoted
- activation of unit j at time t is
- The subscripts , and refer respectively to the input gate, forget gate and output gate of the block
- The subscripts c refers to one of the C memory cells
- The peephole weights from cell c to the input, forget and output gates are denoted , and respectively
- is the state of cell c at time t (i.e. the activation of the linear cell unit)
- f is the activation function of the gates, and g and h are respectively the cell input and output activation functions
- Let I be the number of inputs, K be the number of outputs and H be the number of cells in the hidden layer.

前向傳播:


後向傳播:

參考文獻:
(1)http://colah.github.io/posts/2015-08-Understanding-LSTMs/
(2)A. Graves. Supervised Sequence Labelling with Recurrent Neural Networks. Textbook, Studies in Computational Intelligence, Springer, 2012.
(3)DeepLearning for NLP