天天看點

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

之前寫過的筆記,今天整理檔案時發現了,分享出來,主要參考經典部落格 《Understanding LSTM Networks》,是以我稱為 Understanding Understanding LSTM Networks

Understanding Understanding LSTM Networks

李馳 2019/10/29

RNN

傳統神經網絡很難通過之前的資訊推斷之後的。RNN解決了這個問題,RNN中存在循環,允許資訊保留。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

RNN展開可以看作NN的多重複制,每個網絡把輸出傳遞到下個網絡中。RNN的這種結構很容易了解到它是和序列資訊相關的,用來解決序列相關問題的。

LSTM是RNN取得各種成功的根本原因。

The Problem of Long-Term Dependencies

RNN能夠将之前的資訊與目前的任務聯系起來。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

但是這也要看情況而定,當之前相關的資訊與目前的任務距離太遠,RNN将無法将其聯系起來。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

理論上RNN絕對可以處理“Long-term Dependencies”,但在實際中RNN做不到。還好,LSTM沒這個難題。

LSTM Networks

LSTM是RNN的一種,能夠學習到long-term dependencies。實際上記錄長期資訊本來就是LSTM的預設行為,并不是艱難的事情。

所有的循環神經網絡都有the form of a chain of repeating modules of neural network. 在标準的RNN中,這個重複出現的子產品可能隻是一種很簡單的結構(如隻有一層tanh)。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

LSTM同樣也有這種結構,但是重制子產品的結構不同,不再僅僅是一層網絡,而是十分特殊地互相作用的四層。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

在我們深入探究之前,用到的符号有:

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

在上圖中,每條線都有一個完整向量從一個節點的輸出到其他節點的輸入。粉色的圈代表pointwise operation(有加減乘除四種操作,标量-向量就分别操作,向量-向量就對應位置操作)。黃色框框是學習到的網絡層。合并的線表示把兩條線上所攜帶的向量進行合并concatenation(比如一個帶 h t − 1 h_{t-1} ht−1​,另一個帶 x t x_t xt​,那麼合并後輸出的就是 [ h t − 1 , x t ] [h_{t-1},x_t] [ht−1​,xt​]);分開的線表示将線上的向量複制一份傳到不同的位置。

The core Idea Behind LSTMs

LSTM的關鍵是cell state(整個綠色的框就是一個cell)和圖頂部這條橫穿cell的線。

這個cell state就像是傳送帶,向量從整個cell中穿過,隻是做了少量的線性操作。這種結構能夠很輕松地實作資訊從整個cell中穿過而不做改變。(這樣就可以實作長期記憶保留了?)

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

若隻有上面那條傳送帶是沒辦法實作添加或者删除資訊的。而是通過一種叫做**門“gates”**的結構的小心限制。

門可以實作選擇性的讓資訊通過,它是由一個sigmoid的神經層和一個逐點相乘操作實作的。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

sigmoid層輸出(一個向量)的每個元素都是一個0~1的實數,表示讓對應資訊通過的權重(獲比例)。例如,0代表“不讓任何資訊通過”,1代表“讓全部資訊通過”。

每個LSTM有三種門結構,來實作保護和控制cell state(分别是**“forget gate layer”:遺忘門、“input gate layer”:輸入門、“output gate layer”**:輸出門)。

Step-by-Step LSTM Walk Through

遺忘門

LSTM首先要 決定讓哪些資訊繼續通過 這個cell,這是通過一個叫做“forget gate layer”的sigmoid layer實作的。它的輸入是 h t − 1 h_{t-1} ht−1​和 x t x_t xt​,輸出是一個數值都在0~1之間的向量(向量長度和cell state C t − 1 C_{t-1} Ct−1​一樣),表示讓 C t − 1 C_{t-1} Ct−1​的各部分資訊通過的比重。0表示“不讓任何資訊通過”,1表示“保留所有資訊”。

再回到上文提到的語言模型中,是基于之前所有單詞來預測下個詞的。在這類問題中,cell state中應該包含目前對象的性别 (保留資訊) ,才能使用正确的代詞。是以當我們開始描述新的對象時,我們應該遺忘掉之前對象的性别。 (遺忘資訊)

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

傳入門

LSTM下一步要 決定讓哪些新的資訊加入 這個cell。實作這個需要兩個步驟(見下圖):首先一個叫做“input gate layer”的sigmoid層決定哪些資訊需要更;一個tanh層生成一個向量, C ~ t \tilde{C}_t C~t​,也就是備選的用來更新的内容。在下一步中,我們會聯合這兩部分,對cell的狀态進行一個更新。

在語言模型中,我們需要把新的主語的性别資訊加入到cell state中,用來替換掉老的狀态資訊。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

現在将cell state從 C t − 1 C_{t-1} Ct−1​更新到 C t C_{t} Ct​。到這一步我們要怎麼做已經一目了然了:用 f t f_t ft​逐點乘old cell state C t − 1 C_{t-1} Ct−1​,将不應保留的資訊遺忘掉;然後逐點加 i t ∗ C ~ t i_t * \tilde{C}_t it​∗C~t​。這部分資訊就是我們要添加的新内容。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

在語言模型中,這步就是我們按照之前的的步驟,正式地丢掉舊主語的性别,加上新的資訊。

輸出門

最後,LSTM需要 決定讓哪些資訊輸出 。這個輸出主要基于 C t C_t Ct​,但是過濾後的版本。首先,我們還是先使用一個sigmoid層計算出 C t C_t Ct​中哪些資訊要被輸出。接着,我們把 C t C_t Ct​通過一個sigmoid層(把數值歸到-1~1)。然後把sigmoid層的輸出和tanh層的輸出做逐點乘。這樣我們就得到最後的結果啦。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

LSTM的變種

上述是最普通的LSTM,LSTM有很多變體。

Peephole connections

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

gate layers可以考慮整個cell state。給每一個門都加上peephole。

Coupled forget and input gates

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

和普通LSTM獨立的考慮遺忘那些資訊、新加哪些資訊不同,而是協同決定。隻有相應位置有輸入/遺忘時,才遺忘/輸入資訊。

(Gated Recurrent Unit)GRU

将遺忘門和輸入門結合成一個 更新門(uodate gate)。同時在這個結構中,把細胞狀态和隐藏狀态進行了合并。這個模型比LSTM更簡單也流行。

Understanding Understanding LSTM NetworksUnderstanding Understanding LSTM Networks

其中, r t r_t rt​表示重置門, z t z_t zt​表示更新門。重置門決定是否将之前的狀态忘記。(作用相當于合并了LSTM中的遺忘門和傳入門)當 r t r_t rt​趨于0的時候,前一個時刻的狀态資訊 h t − 1 h_{t-1} ht−1​會被遺忘掉,隐藏狀态 h ~ t \tilde{h}_t h~t​會被重置為目前輸入的資訊。更新門決定是否将隐藏的狀态更新為新的狀态 h ~ t \tilde{h}_t h~t​(作用相當于LSTM中的輸出門)。

GRU和LSTM差別
  • GRU少一個門,同時少了cell state C t C_t Ct​
  • 在LSTM中,通過遺忘門和傳入門控制資訊的保留和傳入;在GRU通過重置門來控制是否保留原來隐藏的狀态,但是不再限制目前資訊的傳入
  • 在LSTM中,輸出 h t = o t ∗ t a n h ( C t ) h_t = o_t*tanh(C_t) ht​=ot​∗tanh(Ct​);在GRU中輸出 h t = ( 1 − z t ) ∗ h t − 1 + z t ∗ h ~ t h_t = (1-z_t)*h_{t-1} + z_t*\tilde{h}_t ht​=(1−zt​)∗ht−1​+zt​∗h~t​

繼續閱讀