LSTM 与 GRU 详解:从门控机制到架构图与数值计算
LSTM 和 GRU 都是为了解决普通 RNN 在长序列中难以保留早期信息的问题。本文不只讲结论,而是从"一个数字如何经过门控计算"开始,把两种结构一步一步算出来。
文章目录
- [LSTM 与 GRU 详解:从门控机制到架构图与数值计算](#LSTM 与 GRU 详解:从门控机制到架构图与数值计算)
-
- [一、先看普通 RNN 为什么会忘记](#一、先看普通 RNN 为什么会忘记)
- [二、LSTM 的核心思想:单独维护长期记忆](#二、LSTM 的核心思想:单独维护长期记忆)
-
- [2.1 LSTM 的四个主要计算](#2.1 LSTM 的四个主要计算)
- [2.2 LSTM 单步数值例子](#2.2 LSTM 单步数值例子)
- [三、GRU 的核心思想:把结构做得更紧凑](#三、GRU 的核心思想:把结构做得更紧凑)
-
- [3.1 GRU 的主要计算](#3.1 GRU 的主要计算)
- [3.2 GRU 单步数值例子](#3.2 GRU 单步数值例子)
- 状态到底存了什么?
-
- [LSTM 怎样把旧记忆留下来?](#LSTM 怎样把旧记忆留下来?)
- [GRU 怎样保存状态?](#GRU 怎样保存状态?)
- 状态和模型参数不是一回事
- [四、LSTM 和 GRU 的架构差异](#四、LSTM 和 GRU 的架构差异)
- 五、同一个输入在两种模型中的结果
- [六、什么时候选 LSTM,什么时候选 GRU?](#六、什么时候选 LSTM,什么时候选 GRU?)
- 七、几个常见误解
- 八、总结
一、先看普通 RNN 为什么会忘记
普通 RNN 每一步都用当前输入和上一步隐藏状态计算新状态:
h t = tanh ( W x x t + W h h t − 1 + b ) h_t=\tanh(W_xx_t+W_hh_{t-1}+b) ht=tanh(Wxxt+Whht−1+b)
它把"新信息"和"旧信息"一次性混合。序列很长时,梯度在多次乘法中可能越来越小,导致模型难以学习长期依赖。
#mermaid-svg-EveeSM3L6AoBXAQc{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-EveeSM3L6AoBXAQc .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-EveeSM3L6AoBXAQc .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-EveeSM3L6AoBXAQc .error-icon{fill:#552222;}#mermaid-svg-EveeSM3L6AoBXAQc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-EveeSM3L6AoBXAQc .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-EveeSM3L6AoBXAQc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-EveeSM3L6AoBXAQc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-EveeSM3L6AoBXAQc .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-EveeSM3L6AoBXAQc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-EveeSM3L6AoBXAQc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-EveeSM3L6AoBXAQc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-EveeSM3L6AoBXAQc .marker.cross{stroke:#333333;}#mermaid-svg-EveeSM3L6AoBXAQc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-EveeSM3L6AoBXAQc p{margin:0;}#mermaid-svg-EveeSM3L6AoBXAQc .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-EveeSM3L6AoBXAQc .cluster-label text{fill:#333;}#mermaid-svg-EveeSM3L6AoBXAQc .cluster-label span{color:#333;}#mermaid-svg-EveeSM3L6AoBXAQc .cluster-label span p{background-color:transparent;}#mermaid-svg-EveeSM3L6AoBXAQc .label text,#mermaid-svg-EveeSM3L6AoBXAQc span{fill:#333;color:#333;}#mermaid-svg-EveeSM3L6AoBXAQc .node rect,#mermaid-svg-EveeSM3L6AoBXAQc .node circle,#mermaid-svg-EveeSM3L6AoBXAQc .node ellipse,#mermaid-svg-EveeSM3L6AoBXAQc .node polygon,#mermaid-svg-EveeSM3L6AoBXAQc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-EveeSM3L6AoBXAQc .rough-node .label text,#mermaid-svg-EveeSM3L6AoBXAQc .node .label text,#mermaid-svg-EveeSM3L6AoBXAQc .image-shape .label,#mermaid-svg-EveeSM3L6AoBXAQc .icon-shape .label{text-anchor:middle;}#mermaid-svg-EveeSM3L6AoBXAQc .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-EveeSM3L6AoBXAQc .rough-node .label,#mermaid-svg-EveeSM3L6AoBXAQc .node .label,#mermaid-svg-EveeSM3L6AoBXAQc .image-shape .label,#mermaid-svg-EveeSM3L6AoBXAQc .icon-shape .label{text-align:center;}#mermaid-svg-EveeSM3L6AoBXAQc .node.clickable{cursor:pointer;}#mermaid-svg-EveeSM3L6AoBXAQc .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-EveeSM3L6AoBXAQc .arrowheadPath{fill:#333333;}#mermaid-svg-EveeSM3L6AoBXAQc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-EveeSM3L6AoBXAQc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-EveeSM3L6AoBXAQc .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EveeSM3L6AoBXAQc .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-EveeSM3L6AoBXAQc .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EveeSM3L6AoBXAQc .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-EveeSM3L6AoBXAQc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-EveeSM3L6AoBXAQc .cluster text{fill:#333;}#mermaid-svg-EveeSM3L6AoBXAQc .cluster span{color:#333;}#mermaid-svg-EveeSM3L6AoBXAQc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-EveeSM3L6AoBXAQc .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-EveeSM3L6AoBXAQc rect.text{fill:none;stroke-width:0;}#mermaid-svg-EveeSM3L6AoBXAQc .icon-shape,#mermaid-svg-EveeSM3L6AoBXAQc .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EveeSM3L6AoBXAQc .icon-shape p,#mermaid-svg-EveeSM3L6AoBXAQc .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-EveeSM3L6AoBXAQc .icon-shape .label rect,#mermaid-svg-EveeSM3L6AoBXAQc .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EveeSM3L6AoBXAQc .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-EveeSM3L6AoBXAQc .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-EveeSM3L6AoBXAQc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} x₁
RNN
h₁
RNN
x₂
h₂
RNN
x₃
h₃
输出
例如在句子"我昨天在图书馆借了一本书,今天把它还给了......"中,"它"指代前面出现的"书"。普通 RNN 可能已经很难保留这么远之前的信息。
二、LSTM 的核心思想:单独维护长期记忆
LSTM 维护两个状态:
- c t c_t ct:细胞状态,可以理解为长期记忆;
- h t h_t ht:隐藏状态,可以理解为当前输出。
它通过三个门控制信息流动:
- 遗忘门:旧记忆保留多少;
- 输入门:新信息写入多少;
- 输出门:长期记忆中有多少用于当前输出。
#mermaid-svg-wxD4aTxUhO9JSoxP{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wxD4aTxUhO9JSoxP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wxD4aTxUhO9JSoxP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wxD4aTxUhO9JSoxP .error-icon{fill:#552222;}#mermaid-svg-wxD4aTxUhO9JSoxP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wxD4aTxUhO9JSoxP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wxD4aTxUhO9JSoxP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wxD4aTxUhO9JSoxP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wxD4aTxUhO9JSoxP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wxD4aTxUhO9JSoxP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wxD4aTxUhO9JSoxP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wxD4aTxUhO9JSoxP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wxD4aTxUhO9JSoxP .marker.cross{stroke:#333333;}#mermaid-svg-wxD4aTxUhO9JSoxP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wxD4aTxUhO9JSoxP p{margin:0;}#mermaid-svg-wxD4aTxUhO9JSoxP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-wxD4aTxUhO9JSoxP .cluster-label text{fill:#333;}#mermaid-svg-wxD4aTxUhO9JSoxP .cluster-label span{color:#333;}#mermaid-svg-wxD4aTxUhO9JSoxP .cluster-label span p{background-color:transparent;}#mermaid-svg-wxD4aTxUhO9JSoxP .label text,#mermaid-svg-wxD4aTxUhO9JSoxP span{fill:#333;color:#333;}#mermaid-svg-wxD4aTxUhO9JSoxP .node rect,#mermaid-svg-wxD4aTxUhO9JSoxP .node circle,#mermaid-svg-wxD4aTxUhO9JSoxP .node ellipse,#mermaid-svg-wxD4aTxUhO9JSoxP .node polygon,#mermaid-svg-wxD4aTxUhO9JSoxP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wxD4aTxUhO9JSoxP .rough-node .label text,#mermaid-svg-wxD4aTxUhO9JSoxP .node .label text,#mermaid-svg-wxD4aTxUhO9JSoxP .image-shape .label,#mermaid-svg-wxD4aTxUhO9JSoxP .icon-shape .label{text-anchor:middle;}#mermaid-svg-wxD4aTxUhO9JSoxP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wxD4aTxUhO9JSoxP .rough-node .label,#mermaid-svg-wxD4aTxUhO9JSoxP .node .label,#mermaid-svg-wxD4aTxUhO9JSoxP .image-shape .label,#mermaid-svg-wxD4aTxUhO9JSoxP .icon-shape .label{text-align:center;}#mermaid-svg-wxD4aTxUhO9JSoxP .node.clickable{cursor:pointer;}#mermaid-svg-wxD4aTxUhO9JSoxP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wxD4aTxUhO9JSoxP .arrowheadPath{fill:#333333;}#mermaid-svg-wxD4aTxUhO9JSoxP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wxD4aTxUhO9JSoxP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wxD4aTxUhO9JSoxP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wxD4aTxUhO9JSoxP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wxD4aTxUhO9JSoxP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wxD4aTxUhO9JSoxP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wxD4aTxUhO9JSoxP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wxD4aTxUhO9JSoxP .cluster text{fill:#333;}#mermaid-svg-wxD4aTxUhO9JSoxP .cluster span{color:#333;}#mermaid-svg-wxD4aTxUhO9JSoxP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wxD4aTxUhO9JSoxP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wxD4aTxUhO9JSoxP rect.text{fill:none;stroke-width:0;}#mermaid-svg-wxD4aTxUhO9JSoxP .icon-shape,#mermaid-svg-wxD4aTxUhO9JSoxP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wxD4aTxUhO9JSoxP .icon-shape p,#mermaid-svg-wxD4aTxUhO9JSoxP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wxD4aTxUhO9JSoxP .icon-shape .label rect,#mermaid-svg-wxD4aTxUhO9JSoxP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wxD4aTxUhO9JSoxP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wxD4aTxUhO9JSoxP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wxD4aTxUhO9JSoxP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 当前输入 xₜ
遗忘门 fₜ
上一步输出 hₜ₋₁
输入门 iₜ
候选记忆 gₜ
输出门 oₜ
旧记忆 cₜ₋₁
×
×
+
新记忆 cₜ
tanh
×
当前输出 hₜ
2.1 LSTM 的四个主要计算
f t = σ ( W f h t − 1 , x t + b f ) f_t=\sigma(W_fh_{t-1},x_t+b_f) ft=σ(Wfht−1,xt+bf)
i t = σ ( W i h t − 1 , x t + b i ) i_t=\sigma(W_ih_{t-1},x_t+b_i) it=σ(Wiht−1,xt+bi)
g t = tanh ( W g h t − 1 , x t + b g ) g_t=\tanh(W_gh_{t-1},x_t+b_g) gt=tanh(Wght−1,xt+bg)
o t = σ ( W o h t − 1 , x t + b o ) o_t=\sigma(W_oh_{t-1},x_t+b_o) ot=σ(Woht−1,xt+bo)
其中 σ \sigma σ 的输出范围是 0 到 1,可以理解为"通过比例"。
然后更新细胞状态:
c t = f t c t − 1 + i t g t c_t=f_tc_{t-1}+i_tg_t ct=ftct−1+itgt
最后得到隐藏状态:
h t = o t tanh ( c t ) h_t=o_t\tanh(c_t) ht=ottanh(ct)
2.2 LSTM 单步数值例子
为了把每一步算清楚,假设当前是单维数字,并设:
x t = 0.5 , h t − 1 = 0.7 , c t − 1 = 0.7 x_t=0.5,\qquad h_{t-1}=0.7,\qquad c_{t-1}=0.7 xt=0.5,ht−1=0.7,ct−1=0.7
为方便手算,使用一个简化的门控计算:
f t = σ ( x t + h t − 1 ) f_t=\sigma(x_t+h_{t-1}) ft=σ(xt+ht−1)
i t = σ ( x t ) i_t=\sigma(x_t) it=σ(xt)
g t = tanh ( x t ) g_t=\tanh(x_t) gt=tanh(xt)
o t = σ ( x t ) o_t=\sigma(x_t) ot=σ(xt)
常用近似值:
σ ( 1.2 ) ≈ 0.7685 , σ ( 0.5 ) ≈ 0.6225 , tanh ( 0.5 ) ≈ 0.4621 \sigma(1.2)\approx0.7685,\quad \sigma(0.5)\approx0.6225,\quad \tanh(0.5)\approx0.4621 σ(1.2)≈0.7685,σ(0.5)≈0.6225,tanh(0.5)≈0.4621
第一步:遗忘门
f t = σ ( 0.5 + 0.7 ) = σ ( 1.2 ) ≈ 0.7685 f_t=\sigma(0.5+0.7)=\sigma(1.2)\approx0.7685 ft=σ(0.5+0.7)=σ(1.2)≈0.7685
旧记忆保留约 76.85%。
第二步:输入门和候选记忆
i t = σ ( 0.5 ) ≈ 0.6225 i_t=\sigma(0.5)\approx0.6225 it=σ(0.5)≈0.6225
g t = tanh ( 0.5 ) ≈ 0.4621 g_t=\tanh(0.5)\approx0.4621 gt=tanh(0.5)≈0.4621
第三步:更新长期记忆
c t = f t c t − 1 + i t g t c_t=f_tc_{t-1}+i_tg_t ct=ftct−1+itgt
c t = 0.7685 × 0.7 + 0.6225 × 0.4621 c_t=0.7685\times0.7+0.6225\times0.4621 ct=0.7685×0.7+0.6225×0.4621
c t ≈ 0.5380 + 0.2877 = 0.8257 c_t\approx0.5380+0.2877=0.8257 ct≈0.5380+0.2877=0.8257
第四步:输出门和隐藏状态
o t = σ ( 0.5 ) ≈ 0.6225 o_t=\sigma(0.5)\approx0.6225 ot=σ(0.5)≈0.6225
h t = o t tanh ( c t ) h_t=o_t\tanh(c_t) ht=ottanh(ct)
h t = 0.6225 × tanh ( 0.8257 ) h_t=0.6225\times\tanh(0.8257) ht=0.6225×tanh(0.8257)
h t ≈ 0.6225 × 0.6782 = 0.4223 h_t\approx0.6225\times0.6782=0.4223 ht≈0.6225×0.6782=0.4223
因此:
c t ≈ 0.8257 , h t ≈ 0.4223 \boxed{c_t\approx0.8257,\qquad h_t\approx0.4223} ct≈0.8257,ht≈0.4223
这里最值得注意的是:LSTM 把长期记忆 c t c_t ct 和当前输出 h t h_t ht 分开保存。
三、GRU 的核心思想:把结构做得更紧凑
GRU 不再单独维护 c t c_t ct,只维护一个隐藏状态 h t h_t ht。
它主要使用两个门:
- 更新门 z t z_t zt:决定保留多少旧状态、写入多少新状态;
- 重置门 r t r_t rt:决定计算候选状态时参考多少旧信息。
#mermaid-svg-QL0FN5AVK42eVUrl{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-QL0FN5AVK42eVUrl .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-QL0FN5AVK42eVUrl .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-QL0FN5AVK42eVUrl .error-icon{fill:#552222;}#mermaid-svg-QL0FN5AVK42eVUrl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-QL0FN5AVK42eVUrl .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-QL0FN5AVK42eVUrl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-QL0FN5AVK42eVUrl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-QL0FN5AVK42eVUrl .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-QL0FN5AVK42eVUrl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-QL0FN5AVK42eVUrl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-QL0FN5AVK42eVUrl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-QL0FN5AVK42eVUrl .marker.cross{stroke:#333333;}#mermaid-svg-QL0FN5AVK42eVUrl svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-QL0FN5AVK42eVUrl p{margin:0;}#mermaid-svg-QL0FN5AVK42eVUrl .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-QL0FN5AVK42eVUrl .cluster-label text{fill:#333;}#mermaid-svg-QL0FN5AVK42eVUrl .cluster-label span{color:#333;}#mermaid-svg-QL0FN5AVK42eVUrl .cluster-label span p{background-color:transparent;}#mermaid-svg-QL0FN5AVK42eVUrl .label text,#mermaid-svg-QL0FN5AVK42eVUrl span{fill:#333;color:#333;}#mermaid-svg-QL0FN5AVK42eVUrl .node rect,#mermaid-svg-QL0FN5AVK42eVUrl .node circle,#mermaid-svg-QL0FN5AVK42eVUrl .node ellipse,#mermaid-svg-QL0FN5AVK42eVUrl .node polygon,#mermaid-svg-QL0FN5AVK42eVUrl .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-QL0FN5AVK42eVUrl .rough-node .label text,#mermaid-svg-QL0FN5AVK42eVUrl .node .label text,#mermaid-svg-QL0FN5AVK42eVUrl .image-shape .label,#mermaid-svg-QL0FN5AVK42eVUrl .icon-shape .label{text-anchor:middle;}#mermaid-svg-QL0FN5AVK42eVUrl .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-QL0FN5AVK42eVUrl .rough-node .label,#mermaid-svg-QL0FN5AVK42eVUrl .node .label,#mermaid-svg-QL0FN5AVK42eVUrl .image-shape .label,#mermaid-svg-QL0FN5AVK42eVUrl .icon-shape .label{text-align:center;}#mermaid-svg-QL0FN5AVK42eVUrl .node.clickable{cursor:pointer;}#mermaid-svg-QL0FN5AVK42eVUrl .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-QL0FN5AVK42eVUrl .arrowheadPath{fill:#333333;}#mermaid-svg-QL0FN5AVK42eVUrl .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-QL0FN5AVK42eVUrl .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-QL0FN5AVK42eVUrl .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QL0FN5AVK42eVUrl .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-QL0FN5AVK42eVUrl .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QL0FN5AVK42eVUrl .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-QL0FN5AVK42eVUrl .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-QL0FN5AVK42eVUrl .cluster text{fill:#333;}#mermaid-svg-QL0FN5AVK42eVUrl .cluster span{color:#333;}#mermaid-svg-QL0FN5AVK42eVUrl div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-QL0FN5AVK42eVUrl .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-QL0FN5AVK42eVUrl rect.text{fill:none;stroke-width:0;}#mermaid-svg-QL0FN5AVK42eVUrl .icon-shape,#mermaid-svg-QL0FN5AVK42eVUrl .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QL0FN5AVK42eVUrl .icon-shape p,#mermaid-svg-QL0FN5AVK42eVUrl .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-QL0FN5AVK42eVUrl .icon-shape .label rect,#mermaid-svg-QL0FN5AVK42eVUrl .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QL0FN5AVK42eVUrl .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-QL0FN5AVK42eVUrl .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-QL0FN5AVK42eVUrl :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 当前输入 xₜ
更新门 zₜ
上一步状态 hₜ₋₁
重置门 rₜ
×
候选状态 h̃ₜ
× 1-zₜ
× zₜ
+
当前状态 hₜ
3.1 GRU 的主要计算
z t = σ ( W z h t − 1 , x t + b z ) z_t=\sigma(W_zh_{t-1},x_t+b_z) zt=σ(Wzht−1,xt+bz)
r t = σ ( W r h t − 1 , x t + b r ) r_t=\sigma(W_rh_{t-1},x_t+b_r) rt=σ(Wrht−1,xt+br)
h ~ t = tanh ( W h x t + U h ( r t h t − 1 ) + b h ) \tilde h_t=\tanh(W_hx_t+U_h(r_th_{t-1})+b_h) h~t=tanh(Whxt+Uh(rtht−1)+bh)
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+zth~t
不同资料有时会采用相反的更新门定义,但核心思想相同:在旧状态和候选新状态之间做加权混合。
3.2 GRU 单步数值例子
仍然设:
x t = 0.5 , h t − 1 = 0.7 x_t=0.5,\qquad h_{t-1}=0.7 xt=0.5,ht−1=0.7
使用简化计算:
z t = σ ( x t + h t − 1 ) z_t=\sigma(x_t+h_{t-1}) zt=σ(xt+ht−1)
r t = σ ( x t ) r_t=\sigma(x_t) rt=σ(xt)
h ~ t = tanh ( x t + r t h t − 1 ) \tilde h_t=\tanh(x_t+r_th_{t-1}) h~t=tanh(xt+rtht−1)
第一步:更新门
z t = σ ( 0.5 + 0.7 ) = σ ( 1.2 ) ≈ 0.7685 z_t=\sigma(0.5+0.7)=\sigma(1.2)\approx0.7685 zt=σ(0.5+0.7)=σ(1.2)≈0.7685
第二步:重置门
r t = σ ( 0.5 ) ≈ 0.6225 r_t=\sigma(0.5)\approx0.6225 rt=σ(0.5)≈0.6225
第三步:候选状态
h ~ t = tanh ( 0.5 + 0.6225 × 0.7 ) \tilde h_t=\tanh(0.5+0.6225\times0.7) h~t=tanh(0.5+0.6225×0.7)
h ~ t = tanh ( 0.9358 ) ≈ 0.7333 \tilde h_t=\tanh(0.9358)\approx0.7333 h~t=tanh(0.9358)≈0.7333
第四步:混合旧状态和新状态
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+zth~t
h t = ( 1 − 0.7685 ) × 0.7 + 0.7685 × 0.7333 h_t=(1-0.7685)\times0.7+0.7685\times0.7333 ht=(1−0.7685)×0.7+0.7685×0.7333
h t ≈ 0.1621 + 0.5636 = 0.7257 h_t\approx0.1621+0.5636=0.7257 ht≈0.1621+0.5636=0.7257
因此:
h t ≈ 0.7257 \boxed{h_t\approx0.7257} ht≈0.7257
GRU 没有单独的 c t c_t ct,所有记忆都集中在 h t h_t ht 中。
状态到底存了什么?
前面一直说"保留记忆",但这里的记忆不是一段文字,也不是一个可以直接打开的便签,而是模型内部的一组浮点数。
LSTM 在每个时间步保存两组状态:
- (c_t):细胞状态,负责持续传递长期信息;
- (h_t):隐藏状态,既参与下一步计算,也可以作为当前输出。
GRU 通常只保存一组隐藏状态 (h_t)。
#mermaid-svg-w24AZKSNPSNfiVtA{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-w24AZKSNPSNfiVtA .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-w24AZKSNPSNfiVtA .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-w24AZKSNPSNfiVtA .error-icon{fill:#552222;}#mermaid-svg-w24AZKSNPSNfiVtA .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-w24AZKSNPSNfiVtA .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-w24AZKSNPSNfiVtA .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-w24AZKSNPSNfiVtA .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-w24AZKSNPSNfiVtA .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-w24AZKSNPSNfiVtA .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-w24AZKSNPSNfiVtA .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-w24AZKSNPSNfiVtA .marker{fill:#333333;stroke:#333333;}#mermaid-svg-w24AZKSNPSNfiVtA .marker.cross{stroke:#333333;}#mermaid-svg-w24AZKSNPSNfiVtA svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-w24AZKSNPSNfiVtA p{margin:0;}#mermaid-svg-w24AZKSNPSNfiVtA .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-w24AZKSNPSNfiVtA .cluster-label text{fill:#333;}#mermaid-svg-w24AZKSNPSNfiVtA .cluster-label span{color:#333;}#mermaid-svg-w24AZKSNPSNfiVtA .cluster-label span p{background-color:transparent;}#mermaid-svg-w24AZKSNPSNfiVtA .label text,#mermaid-svg-w24AZKSNPSNfiVtA span{fill:#333;color:#333;}#mermaid-svg-w24AZKSNPSNfiVtA .node rect,#mermaid-svg-w24AZKSNPSNfiVtA .node circle,#mermaid-svg-w24AZKSNPSNfiVtA .node ellipse,#mermaid-svg-w24AZKSNPSNfiVtA .node polygon,#mermaid-svg-w24AZKSNPSNfiVtA .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-w24AZKSNPSNfiVtA .rough-node .label text,#mermaid-svg-w24AZKSNPSNfiVtA .node .label text,#mermaid-svg-w24AZKSNPSNfiVtA .image-shape .label,#mermaid-svg-w24AZKSNPSNfiVtA .icon-shape .label{text-anchor:middle;}#mermaid-svg-w24AZKSNPSNfiVtA .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-w24AZKSNPSNfiVtA .rough-node .label,#mermaid-svg-w24AZKSNPSNfiVtA .node .label,#mermaid-svg-w24AZKSNPSNfiVtA .image-shape .label,#mermaid-svg-w24AZKSNPSNfiVtA .icon-shape .label{text-align:center;}#mermaid-svg-w24AZKSNPSNfiVtA .node.clickable{cursor:pointer;}#mermaid-svg-w24AZKSNPSNfiVtA .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-w24AZKSNPSNfiVtA .arrowheadPath{fill:#333333;}#mermaid-svg-w24AZKSNPSNfiVtA .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-w24AZKSNPSNfiVtA .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-w24AZKSNPSNfiVtA .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-w24AZKSNPSNfiVtA .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-w24AZKSNPSNfiVtA .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-w24AZKSNPSNfiVtA .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-w24AZKSNPSNfiVtA .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-w24AZKSNPSNfiVtA .cluster text{fill:#333;}#mermaid-svg-w24AZKSNPSNfiVtA .cluster span{color:#333;}#mermaid-svg-w24AZKSNPSNfiVtA div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-w24AZKSNPSNfiVtA .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-w24AZKSNPSNfiVtA rect.text{fill:none;stroke-width:0;}#mermaid-svg-w24AZKSNPSNfiVtA .icon-shape,#mermaid-svg-w24AZKSNPSNfiVtA .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-w24AZKSNPSNfiVtA .icon-shape p,#mermaid-svg-w24AZKSNPSNfiVtA .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-w24AZKSNPSNfiVtA .icon-shape .label rect,#mermaid-svg-w24AZKSNPSNfiVtA .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-w24AZKSNPSNfiVtA .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-w24AZKSNPSNfiVtA .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-w24AZKSNPSNfiVtA :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 当前输入 xₜ
LSTM / GRU 单元
上一步状态
LSTM:cₜ 和 hₜ
GRU:hₜ
下一时间步
假设隐藏维度只有 3,某一步的状态可能长这样:
text
LSTM:
c = [ 0.83, -1.25, 0.06]
h = [ 0.42, -0.31, 0.02]
GRU:
h = [ 0.73, -0.18, 0.54]
真实模型可能有 128、256 或 512 个维度。这里的每个数字都是浮点数,保存在内存或显存中。
这些数字通常没有固定的人工含义。不能简单地说"第一个数字代表主语,第二个数字代表情感"。训练会让整个向量共同携带对任务有用的信息,例如词序、语法关系、情绪倾向或主题线索。
LSTM 怎样把旧记忆留下来?
LSTM 的细胞状态更新是:
c t = f t ⊙ c t − 1 + i t ⊙ g t c_t=f_t\odot c_{t-1}+i_t\odot g_t ct=ft⊙ct−1+it⊙gt
其中 (\odot) 表示对应位置分别相乘。假设:
c t − 1 = 0.8 , − 0.4 c_{t-1}=0.8,-0.4 ct−1=0.8,−0.4
f t = 0.99 , 0.1 , i t = 0.01 , 0.9 , g t = 0.2 , 0.7 f_t=0.99,0.1,\quad i_t=0.01,0.9,\quad g_t=0.2,0.7 ft=0.99,0.1,it=0.01,0.9,gt=0.2,0.7
那么:
c t = 0.99 × 0.8 , 0.1 × ( − 0.4 ) + 0.01 × 0.2 , 0.9 × 0.7 c_t=0.99\\times0.8,\\;0.1\\times(-0.4)+0.01\\times0.2,\\;0.9\\times0.7 ct=0.99×0.8,0.1×(−0.4)+0.01×0.2,0.9×0.7
c t = 0.792 , − 0.04 + 0.002 , 0.63 = 0.794 , 0.59 c_t=0.792,-0.04+0.002,0.63=0.794,0.59 ct=0.792,−0.04+0.002,0.63=0.794,0.59
第一个位置的旧值从 0.8 变成 0.794,几乎被保留;第二个位置则从 -0.4 变成 0.59,说明旧信息被大幅丢弃,同时写入了新信息。
最后,LSTM 通过输出门得到隐藏状态:
h t = o t ⊙ tanh ( c t ) h_t=o_t\odot\tanh(c_t) ht=ot⊙tanh(ct)
所以"记住"在数字上的含义就是:过去输入改变了状态向量,而状态向量又继续影响后面的计算。
GRU 怎样保存状态?
GRU 没有单独的 (c_t),把旧状态和候选新状态直接混合:
h t = ( 1 − z t ) ⊙ h t − 1 + z t ⊙ h ~ t h_t=(1-z_t)\odot h_{t-1}+z_t\odot\tilde h_t ht=(1−zt)⊙ht−1+zt⊙h~t
例如某个位置上:
text
旧状态:0.8
候选新状态:0.2
保留旧状态:0.9
写入新状态:0.1
计算结果是:
h t = 0.9 × 0.8 + 0.1 × 0.2 = 0.74 h_t=0.9\times0.8+0.1\times0.2=0.74 ht=0.9×0.8+0.1×0.2=0.74
0.8 的影响仍然存在,所以 GRU 也具备记忆能力,只是把长期状态和输出放在同一组 (h_t) 中。
状态和模型参数不是一回事
还要区分两种存储:
| 类型 | 里面是什么 | 什么时候变化 |
|---|---|---|
| 模型参数 | Embedding 表、权重矩阵、偏置 | 训练时更新 |
| 当前状态 | LSTM 的 (c_t,h_t),GRU 的 (h_t) | 每读入一个时间步就更新 |
模型参数更像"学到的规律",当前状态更像"这一次读到这里后留下的临时工作记忆"。处理完一条新序列后,状态通常会重新初始化。
因此,LSTM/GRU 的"记忆"可以准确地理解成:
一组会随输入逐步更新、并会影响后续输出的浮点数向量。
四、LSTM 和 GRU 的架构差异
#mermaid-svg-5COQizMxjP65RNPX{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-5COQizMxjP65RNPX .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-5COQizMxjP65RNPX .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-5COQizMxjP65RNPX .error-icon{fill:#552222;}#mermaid-svg-5COQizMxjP65RNPX .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-5COQizMxjP65RNPX .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-5COQizMxjP65RNPX .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-5COQizMxjP65RNPX .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-5COQizMxjP65RNPX .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-5COQizMxjP65RNPX .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-5COQizMxjP65RNPX .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-5COQizMxjP65RNPX .marker{fill:#333333;stroke:#333333;}#mermaid-svg-5COQizMxjP65RNPX .marker.cross{stroke:#333333;}#mermaid-svg-5COQizMxjP65RNPX svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-5COQizMxjP65RNPX p{margin:0;}#mermaid-svg-5COQizMxjP65RNPX .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-5COQizMxjP65RNPX .cluster-label text{fill:#333;}#mermaid-svg-5COQizMxjP65RNPX .cluster-label span{color:#333;}#mermaid-svg-5COQizMxjP65RNPX .cluster-label span p{background-color:transparent;}#mermaid-svg-5COQizMxjP65RNPX .label text,#mermaid-svg-5COQizMxjP65RNPX span{fill:#333;color:#333;}#mermaid-svg-5COQizMxjP65RNPX .node rect,#mermaid-svg-5COQizMxjP65RNPX .node circle,#mermaid-svg-5COQizMxjP65RNPX .node ellipse,#mermaid-svg-5COQizMxjP65RNPX .node polygon,#mermaid-svg-5COQizMxjP65RNPX .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-5COQizMxjP65RNPX .rough-node .label text,#mermaid-svg-5COQizMxjP65RNPX .node .label text,#mermaid-svg-5COQizMxjP65RNPX .image-shape .label,#mermaid-svg-5COQizMxjP65RNPX .icon-shape .label{text-anchor:middle;}#mermaid-svg-5COQizMxjP65RNPX .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-5COQizMxjP65RNPX .rough-node .label,#mermaid-svg-5COQizMxjP65RNPX .node .label,#mermaid-svg-5COQizMxjP65RNPX .image-shape .label,#mermaid-svg-5COQizMxjP65RNPX .icon-shape .label{text-align:center;}#mermaid-svg-5COQizMxjP65RNPX .node.clickable{cursor:pointer;}#mermaid-svg-5COQizMxjP65RNPX .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-5COQizMxjP65RNPX .arrowheadPath{fill:#333333;}#mermaid-svg-5COQizMxjP65RNPX .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-5COQizMxjP65RNPX .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-5COQizMxjP65RNPX .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-5COQizMxjP65RNPX .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-5COQizMxjP65RNPX .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-5COQizMxjP65RNPX .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-5COQizMxjP65RNPX .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-5COQizMxjP65RNPX .cluster text{fill:#333;}#mermaid-svg-5COQizMxjP65RNPX .cluster span{color:#333;}#mermaid-svg-5COQizMxjP65RNPX div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-5COQizMxjP65RNPX .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-5COQizMxjP65RNPX rect.text{fill:none;stroke-width:0;}#mermaid-svg-5COQizMxjP65RNPX .icon-shape,#mermaid-svg-5COQizMxjP65RNPX .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-5COQizMxjP65RNPX .icon-shape p,#mermaid-svg-5COQizMxjP65RNPX .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-5COQizMxjP65RNPX .icon-shape .label rect,#mermaid-svg-5COQizMxjP65RNPX .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-5COQizMxjP65RNPX .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-5COQizMxjP65RNPX .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-5COQizMxjP65RNPX :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} GRU
输入 xₜ 与 hₜ₋₁
更新门
重置门
直接更新 hₜ
LSTM
输入 xₜ 与 hₜ₋₁
遗忘门
输入门
输出门
cₜ₋₁
更新细胞状态 cₜ
输出 hₜ
| 对比项 | LSTM | GRU |
|---|---|---|
| 状态 | c t c_t ct 和 h t h_t ht 两个状态 | 只有 h t h_t ht |
| 门数量 | 遗忘门、输入门、输出门 | 更新门、重置门 |
| 参数量 | 较多 | 较少 |
| 训练速度 | 通常较慢 | 通常较快 |
| 内存占用 | 较高 | 较低 |
| 控制精细度 | 更细致 | 更简洁 |
| 长序列表现 | 经常较稳定 | 很多任务中接近 LSTM |
五、同一个输入在两种模型中的结果
| 模型 | 输入 | 旧状态 | 新状态结果 |
|---|---|---|---|
| LSTM | x t = 0.5 x_t=0.5 xt=0.5 | h t − 1 = 0.7 , c t − 1 = 0.7 h_{t-1}=0.7,c_{t-1}=0.7 ht−1=0.7,ct−1=0.7 | h t ≈ 0.4223 , c t ≈ 0.8257 h_t\approx0.4223,c_t\approx0.8257 ht≈0.4223,ct≈0.8257 |
| GRU | x t = 0.5 x_t=0.5 xt=0.5 | h t − 1 = 0.7 h_{t-1}=0.7 ht−1=0.7 | h t ≈ 0.7257 h_t\approx0.7257 ht≈0.7257 |
数值不同并不表示谁一定更好,因为它们使用的参数和状态定义不同。真正的模型会通过训练学习出适合任务的门控方式。
六、什么时候选 LSTM,什么时候选 GRU?
可以先用下面的经验:
- 数据量较小、希望训练更快:优先尝试 GRU;
- 资源有限、需要更少参数:优先尝试 GRU;
- 序列很长、长期依赖很重要:尝试 LSTM;
- 需要更细致地分离长期记忆和当前输出:尝试 LSTM;
- 两者验证集效果接近:一般选参数更少的 GRU。
最终仍应以验证集实验为准。没有一个规则能保证 LSTM 或 GRU 在所有数据集上都更好。
七、几个常见误解
误解 1:GRU 一定比 LSTM 弱。
不一定。GRU 只是结构更简单,很多任务中性能可以接近 LSTM。
误解 2:门的输出是 0 或 1。
门通常经过 Sigmoid,输出是 0 到 1 之间的连续值,例如 0.7685 表示保留约 76.85%。
误解 3:LSTM 的细胞状态就是最终输出。
不是。LSTM 有长期记忆 c t c_t ct,还要通过输出门生成隐藏状态 h t h_t ht。
八、总结
LSTM 和 GRU 的共同点是:都让模型学会控制信息的保留、写入和输出。
核心区别是:
- LSTM:有独立的长期记忆 c t c_t ct,控制更细;
- GRU:把记忆和输出合并到 h t h_t ht,结构更轻。
可以用一句话记忆:
LSTM 像有独立记忆本和输出窗口的系统;GRU 像把两者合并后的轻量系统。