M4-002M4: Sequences & TransformersRecurrent Models (RNN/LSTM/GRU)Easy
⚡Flashcard Drill➔Mastery:

LSTM: Three Gating Mechanisms and the Additive Cell State Highway

📐 Mathematical Definition
ct=ft⊙ct−1+it⊙gt;∂ct∂ct−1=ft (no matrix product)c_t=f_t\odot c_{t-1}+i_t\odot g_t;\qquad \frac{\partial c_t}{\partial c_{t-1}}=f_t\ (\text{no matrix product})
⚡ One-Sentence Core Answer
LSTM introduces forget, input, and output gates around a linear additive cell state Ct=ft⊙Ct−1+it⊙C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t, providing an uninterrupted gradient highway.

📌 Key Takeaways

  • •
    Additive cell state: linear update avoids repeated matrix multiplication, eliminating gradient vanishing
  • •
    Three gates: Forget gate ftf_t (what to discard), Input gate iti_t (what new information to store), Output gate oto_t (what to emit to hidden state hth_t)
  • •
    Gating range: Sigmoid activations constrain gates to (0,1)(0, 1), acting as continuous soft switches

📐 Mathematical Derivations

Mathematical Formulations (Hochreiter & Schmidhuber, 1997):
At time step tt with input xtx_t and previous hidden state ht−1h_{t-1}:
1. Forget Gate: ft=σ(Wf[ht−1,xt]+bf)f_t = \sigma(W_f [h_{t-1}, x_t] + b_f) (fraction of previous memory to retain).
2. Input Gate: it=σ(Wi[ht−1,xt]+bi)i_t = \sigma(W_i [h_{t-1}, x_t] + b_i) (fraction of candidate update to add).
3. Candidate State: C~t=tanh⁡(Wc[ht−1,xt]+bc)\tilde{C}_t = \tanh(W_c [h_{t-1}, x_t] + b_c).
4. Cell State Update: Ct=ft⊙Ct−1+it⊙C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t.
5. Output Gate & Hidden State: ot=σ(Wo[ht−1,xt]+bo)o_t = \sigma(W_o [h_{t-1}, x_t] + b_o), ht=ot⊙tanh⁡(Ct)\quad h_t = o_t \odot \tanh(C_t).
Why Gradients Do Not Vanish:
The Jacobian of the cell state is: ∂Ct∂Ct−1=diag(ft)\frac{\partial C_t}{\partial C_{t-1}} = \text{diag}(f_t).
Across TT time steps, the backward gradient is: ∂L∂C1=∂L∂CT∏t=2Tft\frac{\partial \mathcal{L}}{\partial C_1} = \frac{\partial \mathcal{L}}{\partial C_T} \prod_{t=2}^T f_t.
If the forget gate is near 1 (ft≈1f_t \approx 1), the gradient propagates across hundreds of time steps with zero exponential decay!

🏭 Production Trade-offs

Initialization trick: Always initialize forget gate bias bfb_f to a positive constant (e.g., 1.01.0 or 2.02.0). This forces ft≈σ(1.0)≈0.73−0.88f_t \approx \sigma(1.0) \approx 0.73-0.88 at step 0, ensuring the cell starts by remembering historical information.
⚠️ Common Interview Pitfalls
  • ✕
    Initializing forget gate biases to 0, which sets f0≈0.5f_0 \approx 0.5 and attenuates memory retention by 50%50\% per step at initialization
  • ✕
    Assuming LSTM cell state CtC_t can grow unboundedly; without forget gates (ft=1f_t=1), CtC_t variance drifts to infinity
🎯 Interviewer Follow-up Inquiries
  • ?
    Why must the forget gate bias bfb_f be initialized to a positive value (e.g., 1.0)?
  • ?
    How does the additive cell state in LSTM mathematically mirror the identity skip connection in ResNet?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM4-001: RNN Forward Dynamics, Backpropagation Through Time (BPTT), and Core Limitations📋Back to BankNext →M4-003: Comparing LSTM and GRU: Structural Simplifications and Trade-offs
中文速览与面试要点

解释 LSTM 的三个门与细胞状态的加法路径

遗忘门/输入门/输出门控制细胞状态;c_t=f⊙c_t−1+i⊙g 的加性更新给梯度一条近似恒等的直通路径。

核心要点

  • 数学机理:LSTM 的核心是引入 细胞状态 c_t (一条贯穿时间的'传送带')并用三个门调制它: 遗忘门 f_t=σ(W_f[h_t−1,x_t]) 决定丢弃多少旧信息; 输入门 i_t=σ(W_i[·]) 与候选 g_t=tanh(W_g[·]) 决定写入多少新信息; 输出门 o_t=σ(W_o[·]) 决定从 c_t 读出多少到隐状态 h_t=o_t⊙tanh(c_t)。 更新式 :c_t=f_t⊙c_t−1+i_t⊙g_t。 关键性质 :∂c_t/∂c_t−1= f_t (逐元素, 没有矩阵乘法 !)。这意味着沿时间反传时,梯度连乘的是 门值 (标量,逐元素)而非权重矩阵——若遗忘门学到 f≈1,则梯度可以 几乎无衰减地跨越很多步 (称为 constant error carousel,常数误差传送带)。这与普通 RNN 的 W_h 连乘形成鲜明对比:普通 RNN 的连乘含矩阵(谱半径决定衰减),LSTM 的连乘只是 (0,1) 的标量(由门控制,可接近 1)。此外,加性更新避免了'新信息覆盖旧信息'的乘性冲突,使长期记忆与短期更新可以并存。
  • 深度剖析与工程权衡:① 门的作用机制 ——sigmoid 门是'软开关':f=1 表示完全保留(长记忆模式),f=0 表示完全遗忘(重置模式);网络通过数据学习何时切换。这使 LSTM 能同时处理'需要长记忆'与'需要快速遗忘'的任务。② 参数量账本 ——LSTM 有 4 组权重(f/i/g/o),参数量约为普通 RNN 的 4 倍;GRU 用 3 组(更新门 z、重置门 r)约为 3 倍。这是'表达力 vs 参数量'的取舍。③ 遗忘门偏置初始化 ——把 f 的 bias 初始化为正数(如 1)使初始时 f≈0.73~1,鼓励'默认保留',是训练深层/长序列 LSTM 的经典技巧(Gers & Schmidhuber 的 forget gate bias)。④ 与残差的同构性 ——c_t=f⊙c_t−1+(新信息) 与残差 h_l+1=h_l+F(h_l) 形式同构:都是'恒等路径 + 增量';f 对应残差的恒等系数 1。这解释了为何两者都能缓解梯度消失。⑤ LSTM vs 现代架构 ——LSTM 的 O(1) 状态使其在流式推理上优于 Transformer(无需 KV cache 增长),但训练不可并行;Mamba 用选择性 SSM + 并行扫描把'加性递归'重新带回可并行训练,是 LSTM 思想在现代硬件上的复兴。⑥ 面试要点 ——被问'LSTM 为什么能缓解梯度消失',核心答案是' 细胞状态的加性更新使梯度连乘的是门值而非矩阵 ';能联系到残差连接是加分项。
  • f/i/o 都是 sigmoid 门,取值 (0,1) 做软开关
  • 细胞状态用加法更新,梯度沿 c 连乘的是门值而非矩阵
  • f≈1 时梯度可跨越很多步(常数误差传送带)

常见陷阱

  • 以为 LSTM 完全解决了梯度消失(只是大幅缓解,超长依赖仍困难)
  • 忽略遗忘门 bias 初始化对长记忆的影响