天天看點

LSTM 原理

在了解 RNN 基礎上, 對梯度消失一種解決方方案 LSTM (保留早期狀态資訊)

之前在讨論 RNN (遞歸神經網絡) 的梯度消失 和 梯度爆炸都會對咱的網絡結構産生極大的影響.

梯度爆炸, 也是在參數更新這塊, 調整步伐太大, 産生 NaN 或 Inf, 代碼就搞崩了直解決梯度消失...而對于梯度爆炸而已, 可以采用 clipping 的方式, 對向量進行縮放, 而不改變其方向.

梯度消失, BP的參數訓練, 求導的鍊式法則, 可能會有項直接乘積非常小, 整個式子沒有梯度, 表 詞間的關聯性弱. 而那篇中呢, 其實是特意留了一個待解決的方案沒有去說明. 其實就是為了引出今天要談的 LSTM.

Fix vanishing gradient

The main problem is that it is too difficult for the RNN to learn to preserve (儲存) information over many timesteps (傳統的 RNN 中, 沒有對 每個狀态的資訊進行儲存)

In a vanilla RNN, the hidden state is constantly being rewritten (狀态更新, 就覆寫掉原來的狀态資訊)

\(h^{(t)} = \sigma(W_hh^{(t-1)} + W_xx^{(t)} + b)\)

看這公式就能明白, t 時刻的輸出, 是由 (t-1) 時刻的 輸入得到的.... 也就是說, 随着狀态的不斷改變, h(t) 也在不斷地 插除和改寫自己.

We think that how about a RNN with separate memory (另外給整個地方, 單獨存起來). 那這種思路呢, 就是咱今天說的 LSTM (Long Short Term memory)

LSTM

A type of RNN proposed by Hochreiter and Schmidhuber in 1997 as a solution to the vanishing gradients problem. 兩個德國的大兄弟提出來的, 針對梯度消失這塊.

On step t, there is a hidden state \(h^{(t)}\) and a cell state \(c^{(t)}\)

  • Both are vectors length n (隐含層, 産生這兩個向量)
  • The cell storeslong-term infomation就是用另外一個向量, 去記錄每個時刻的資訊. (差不多這個意思)
  • The LSTM can erase, write and read information from the cell. (能從這個 cell 中, 去讀取, 改寫相應的資訊)

The selection of which information is erased / read / writen is controlled by three corresponding gates

  • The gates are also vectors length n .(每個門也是長度為 n 的向量)
  • On each timestep, each element of the gates can beopend(1), closed(0)或者是 (0~1) 之間的值.
  • The gates aredynamic: their values is computed based on the current context. (基于上下文來改變 gates 值的變化的)

用公式來表達的就是這樣的形式.

LSTM 原理

當然, 如果還是不夠詳細的話, 可以再用更為形象一點的圖來表示哦

LSTM 原理

Why LSTM can solve vanishing

The LSTM architecture makes it easier for the RNN to preserve information over many timesteps (保留早期的狀态資訊)

這也是 LSTM 最為關鍵的一點 與 傳統的 RNN 即保留了早期的狀态資訊呀.

But, LSTM does not guarantee that there is no vanishing. 也沒有能做到完全避免, but it does provide an easier way for the model to learn long-distance dependencies. 在在實踐應用中還是滿成功的, 從幾年來看的話. 行了. LSTM 就先到這裡吧, 隻要在了解 RNN 的基礎上, 掌握它這裡的一個 狀态保留動态的狀态 (達到保留早期資訊的方式來盡可能解決梯度消失) . 即可.