循环神经网络

9 minIntermediate2026/6/14

RNN原理、LSTM门控机制、GRU、序列建模与应用。

1. RNN基本原理

1.1 基本RNN

循环神经网络通过隐藏状态处理序列数据:

ht=tanh(Whhht1+Wxhxt+bh)\mathbf{h}_t = \tanh(\mathbf{W}_{hh}\mathbf{h}_{t-1} + \mathbf{W}_{xh}\mathbf{x}_t + \mathbf{b}_h) ot=Whyht+by\mathbf{o}_t = \mathbf{W}_{hy}\mathbf{h}_t + \mathbf{b}_y

    h_{t-1} ──→ [RNN Cell] ──→ h_t ──→ [RNN Cell] ──→ h_{t+1}
                  ↑                         ↑
                x_t                       x_{t+1}

1.2 RNN变体

结构适用场景
one-to-one标准NN像分
one-to-many单输入→序列像描述
many-to-one序列→单输出情感分析
many-to-many序列→序列机器翻译
同步many-to-many同步序列视频分

1.3 BPTT(沿时间反向传播)

LWhh=t=1TLhTk=t+1Thkhk1htWhh\frac{\partial L}{\partial \mathbf{W}_{hh}} = \sum_{t=1}^{T} \frac{\partial L}{\partial \mathbf{h}_T} \prod_{k=t+1}^{T} \frac{\partial \mathbf{h}_k}{\partial \mathbf{h}_{k-1}} \cdot \frac{\partial \mathbf{h}_t}{\partial \mathbf{W}_{hh}}

梯度问题

k=t+1Thkhk1=k=t+1TWhhTdiag(σ(zk))\prod_{k=t+1}^{T} \frac{\partial \mathbf{h}_k}{\partial \mathbf{h}_{k-1}} = \prod_{k=t+1}^{T} \mathbf{W}_{hh}^T \cdot \text{diag}(\sigma'(\mathbf{z}_k))

Whh<1\|\mathbf{W}_{hh}\| < 1梯度消失,>1> 1梯度爆炸。

2. LSTM

2.1 门控机制

LSTM 通过三个门控制信息流动:

遗忘门:决定丢弃哪些信息

ft=σ(Wf[ht1,xt]+bf)\mathbf{f}_t = \sigma(\mathbf{W}_f \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_f)

输入门:决定更新哪些信息

it=σ(Wi[ht1,xt]+bi)\mathbf{i}_t = \sigma(\mathbf{W}_i \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_i)

候选记忆

c~t=tanh(Wc[ht1,xt]+bc)\tilde{\mathbf{c}}_t = \tanh(\mathbf{W}_c \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_c)

细胞状态更新

ct=ftct1+itc~t\mathbf{c}_t = \mathbf{f}_t \odot \mathbf{c}_{t-1} + \mathbf{i}_t \odot \tilde{\mathbf{c}}_t

输出门

ot=σ(Wo[ht1,xt]+bo)\mathbf{o}_t = \sigma(\mathbf{W}_o \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t] + \mathbf{b}_o) ht=ottanh(ct)\mathbf{h}_t = \mathbf{o}_t \odot \tanh(\mathbf{c}_t)

2.2 LSTM缓解梯度消失

ct=ftct1+itc~t\mathbf{c}_t = \mathbf{f}_t \odot \mathbf{c}_{t-1} + \mathbf{i}_t \odot \tilde{\mathbf{c}}_t

ctct1=ft\frac{\partial \mathbf{c}_t}{\partial \mathbf{c}_{t-1}} = \mathbf{f}_t

ft1\mathbf{f}_t \approx 1 时,梯度可以无损传递,有效缓解梯度消失。

2.3 LSTM变体

变体改进说明
Peephole LSTM门控输入包含细胞状态更精确的门控
Coupled LSTM遗忘门和输入门联动减少参数
Layer Normalization每层归一化稳定训练

3. GRU

3.1 门控机制

GRU 是 LSTM 的简化版本,合并了遗忘门和输入门:

重置门

rt=σ(Wr[ht1,xt])\mathbf{r}_t = \sigma(\mathbf{W}_r \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t])

更新门

zt=σ(Wz[ht1,xt])\mathbf{z}_t = \sigma(\mathbf{W}_z \cdot [\mathbf{h}_{t-1}, \mathbf{x}_t])

候选隐藏状态

h~t=tanh(Wh[rtht1,xt])\tilde{\mathbf{h}}_t = \tanh(\mathbf{W}_h \cdot [\mathbf{r}_t \odot \mathbf{h}_{t-1}, \mathbf{x}_t])

隐藏状态更新

ht=(1zt)ht1+zth~t\mathbf{h}_t = (1 - \mathbf{z}_t) \odot \mathbf{h}_{t-1} + \mathbf{z}_t \odot \tilde{\mathbf{h}}_t

3.2 LSTM vs GRU

维度LSTMGRU
门数量3个(遗忘、输入、输出)2个(重置、更新)
细胞状态
参数量少(约1/3)
训练速度较慢较快
表达能力更强足够
适用场景复杂长程依赖中等序列

4. 双向RNN与深层RNN

4.1 双向RNN

同时考虑前向和后向信息:

ht=RNNfw(xt,ht1)\overrightarrow{\mathbf{h}}_t = \text{RNN}_{fw}(\mathbf{x}_t, \overrightarrow{\mathbf{h}}_{t-1}) ht=RNNbw(xt,ht+1)\overleftarrow{\mathbf{h}}_t = \text{RNN}_{bw}(\mathbf{x}_t, \overleftarrow{\mathbf{h}}_{t+1}) ht=[ht;ht]\mathbf{h}_t = [\overrightarrow{\mathbf{h}}_t; \overleftarrow{\mathbf{h}}_t]

4.2 深层RNN

堆叠多层RNN:

x_t → [RNN Layer 1] → [RNN Layer 2] → [RNN Layer 3] → o_t

每层提取不同层次的序列特征。

5. 序列建模应用

5.1 Seq2Seq

编码器: x_1, x_2, ..., x_T → 上下文向量 c
解码器: c → y_1, y_2, ..., y_{T'}

5.2 注意力机制

et,i=score(htd,hie)e_{t,i} = \text{score}(\mathbf{h}_t^d, \mathbf{h}_i^e) αt,i=Softmax(et,i)\alpha_{t,i} = \text{Softmax}(e_{t,i}) ct=iαt,ihie\mathbf{c}_t = \sum_i \alpha_{t,i} \mathbf{h}_i^e

评分函数公式
加性vTtanh(W1hd+W2he)v^T\tanh(\mathbf{W}_1\mathbf{h}^d + \mathbf{W}_2\mathbf{h}^e)
乘性(hd)TWhe(\mathbf{h}^d)^T \mathbf{W} \mathbf{h}^e
点积(hd)The(\mathbf{h}^d)^T \mathbf{h}^e
缩放点积(hd)Thedk\frac{(\mathbf{h}^d)^T \mathbf{h}^e}{\sqrt{d_k}}