跳轉到

2014-GRU門控循環單元#

案件摘要#

LSTM 雖是長期記憶之王,卻背著三個門、四組矩陣的重擔,訓練慢、參數多。2014 年,神經機器翻譯(NMT)競賽白熱化,Kyunghyun Cho 等人在 EMNLP 發表《Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation》,在過程中設計出更輕巧的門控循環單元 GRU:只有兩個門,卻與 LSTM 性能相當。同期 Chung 等人的實證評估(NIPS 2014 workshop)為此背書。GRU 以「簡潔的勝利」成為輕量序列建模標準,Cho 的 RNN Encoder-Decoder 更與 Sutskever 的 Seq2Seq 共同奠定了神經機器翻譯的地基。

前因 -- 為什麼會有這個案子#

  • LSTM 參數量驚人:每個 cell 要 4 組 \(d \times (d+V)\) 矩陣,訓練慢、記憶體吃緊,2014 年的 GPU 算力仍相當有限。
  • Cho 團隊(Jacobs University 與 Yoshua Bengio 的 MILA 合作)要用 RNN 做編碼器-解碼器架構的神經機器翻譯,需要一個訓練得起、收斂得快的門控單元。
  • 2014 年 NMT 白熱化:Sutskever 等人同年提出 Seq2Seq(NIPS 2014),兩個團隊在相似的思路上賽跑。
  • Kalman 濾波的啟發:GRU 的更新門數學上類似離散 Kalman 濾波的增益項——「新觀測」與「舊估計」的凸組合。
  • 學界想驗證一個問題:LSTM 的成功究竟需要全部三個門,還是「門控」本身就是重點?

線索與推理 -- 數學式、程式、理論#

GRU 的數學:兩個門的極簡設計#

GRU 砍掉輸出門與獨立細胞狀態,只留更新門與重置門:

\[ z_t = \sigma\big(W_z [h_{t-1}, x_t]\big) \quad \text{(更新門:新舊狀態的混合比例)} \]
\[ r_t = \sigma\big(W_r [h_{t-1}, x_t]\big) \quad \text{(重置門:候選狀態要用多少舊記憶)} \]
\[ \tilde{h}_t = \tanh\big(W [r_t \odot h_{t-1},\, x_t]\big), \qquad h_t = (1 - z_t) \odot h_{t-1} + z_t \odot \tilde{h}_t \]

更新門 \(z_t\) 對舊狀態與候選狀態做凸組合(權重和為 1),這正是 LSTM 遺忘門與輸入門( \(f_t \approx 1 - i_t\) )耦合後的化簡;梯度沿這條線性插值通路倒傳時同樣不易消失。

與 LSTM 的對照#

  • LSTM:3 個門(f/i/o)+ 細胞狀態 \(c_t\) ;GRU:2 個門(z/r), \(h_t\) 兼具記憶與輸出角色。
  • 參數量:LSTM 每層 \(4d^2\) 量級的循環參數,GRU 僅 \(3d^2\) ——省下 25% 的參數與計算。
  • 小資料時 GRU 常更準、收斂更快;大資料時兩者相當(Chung et al. 2014 的實證結論)。

RNN Encoder-Decoder:Seq2Seq 的孿生兄弟#

Cho 等人的翻譯架構:編碼器(GRU)把源句讀完,壓縮成一個向量 \(c\) ;解碼器(另一個 GRU)以 \(c\) 為初始狀態,逐詞生成譯文:

\[ c = \mathrm{enc}(x_1, \dots, x_{T_x}), \qquad p(y_t \mid y_{<t}, c) = \mathrm{softmax}(W_{out}\, \mathrm{dec}(h_{t-1}, y_{t-1}, c)) \]

Python 示範:numpy 手刻 GRU cell 前向 + 參數量對照#

import numpy as np

rng = np.random.default_rng(2)
sigmoid = lambda z: 1 / (1 + np.exp(-z))

def gru_forward(xs, d):
    """xs: list of one-hot vectors; d: hidden size"""
    V = len(xs[0])
    Wz = rng.normal(0, .3, (d, d + V)); Wr = rng.normal(0, .3, (d, d + V))
    W  = rng.normal(0, .3, (d, d + V))
    h = np.zeros(d)
    for x in xs:
        z = np.concatenate([h, x])
        r = np.concatenate([h, x])
        gz = sigmoid(Wz @ z)
        gr = sigmoid(Wr @ r)
        h_cand = np.tanh(W @ np.concatenate([gr * h, x]))
        h = (1 - gz) * h + gz * h_cand     # 凸組合更新
    return h

# 參數量對照:隱層 d=128, 詞表 V=5000, 單層循環矩陣參數數
d, V = 128, 5000
lstm_params = 4 * d * (d + V)
gru_params  = 3 * d * (d + V)
print(f"LSTM 單層參數量: {lstm_params:,}")
print(f"GRU  單層參數量: {gru_params:,}  (省下 {(1 - gru_params / lstm_params):.0%})")

# 前向展示:同一序列下 GRU 隱層的形狀與數值範圍
w2i = {w: i for i, w in enumerate("當我睡覺了時。".replace("", " ").split()) if w}
sent = ["當", "我", "睡", "覺", "了"]
vocab = {w: i for i, w in enumerate(sent)}
xs = [np.eye(len(sent))[vocab[w]] for w in sent]
h = gru_forward(xs, d=8)
print("GRU 最終隱層:", np.round(h, 3), "| 範數:", round(np.linalg.norm(h), 3))

結案 -- 後果與影響#

  • GRU 成為輕量序列建模標準:小資料集、低算力場景(邊緣裝置、移動端)的首選。
  • Cho 的 RNN Encoder-Decoder 與 Sutskever 的 Seq2Seq(同年)共同奠定 NMT;神經機器翻譯從此取代統計機翻(phrase-based SMT)。
  • 注意力機制直接疊加在 GRU/LSTM 之上:Bahdanau 等人 2014 年的注意力機翻,用的就是 GRU 編碼器-解碼器。
  • 門控思想簡潔的勝利:GRU 證明「門控」本身才是重點,而非門的數量;此思想延續到 Mamba/SSM(2023)的選擇性門控。
  • Chung 等人的實證評估確立了「架構等效性」研究傳統:比較架構而非崇拜單一模型。

關鍵人物與文獻(條列,含真實文獻書目)#

  • Kyunghyun Cho, Bart van Merriënboer, Caglar Gulcehre, Dzmitry Bahdanau, Fethi Bougares, Holger Schwenk & Yoshua Bengio (2014). Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine Translation. EMNLP 2014, 1724–1734.
  • Junyoung Chung, Caglar Gulcehre, Kyunghyun Cho & Yoshua Bengio (2014). Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling. NIPS 2014 Deep Learning Workshop, arXiv:1412.3555.
  • Ilya Sutskever, Oriol Vinyals & Quoc V. Le (2014). Sequence to Sequence Learning with Neural Networks. NIPS 2014.
  • Dzmitry Bahdanau, Kyunghyun Cho & Yoshua Bengio (2015). Neural Machine Translation by Jointly Learning to Align and Translate. ICLR 2015.
  • Rudolf E. Kalman (1960). A New Approach to Linear Filtering and Prediction Problems. Journal of Basic Engineering, 82(1), 35–45.
  • Sepp Hochreiter & Jürgen Schmidhuber (1997). Long Short-Term Memory. Neural Computation, 9(8), 1735–1780.
  • Ashish Vaswani et al. (2017). Attention Is All You Need. NIPS 2017(後續:以注意力取代循環的轉折點).