M4-002M4: Sequences & TransformersRecurrent Models (RNN/LSTM/GRU)Easy
⚡Flashcard Drill➔Mastery:
LSTM: Three Gating Mechanisms and the Additive Cell State Highway
📐 Mathematical Definition
⚡ One-Sentence Core Answer
LSTM introduces forget, input, and output gates around a linear additive cell state , providing an uninterrupted gradient highway.
📌 Key Takeaways
- •Additive cell state: linear update avoids repeated matrix multiplication, eliminating gradient vanishing
- •Three gates: Forget gate (what to discard), Input gate (what new information to store), Output gate (what to emit to hidden state )
- •Gating range: Sigmoid activations constrain gates to , acting as continuous soft switches
📐 Mathematical Derivations
Mathematical Formulations (Hochreiter & Schmidhuber, 1997):
At time step with input and previous hidden state :
1. Forget Gate: (fraction of previous memory to retain).
2. Input Gate: (fraction of candidate update to add).
3. Candidate State: .
4. Cell State Update: .
5. Output Gate & Hidden State: , .
Why Gradients Do Not Vanish:
The Jacobian of the cell state is: .
Across time steps, the backward gradient is: .
If the forget gate is near 1 (), the gradient propagates across hundreds of time steps with zero exponential decay!
At time step with input and previous hidden state :
1. Forget Gate: (fraction of previous memory to retain).
2. Input Gate: (fraction of candidate update to add).
3. Candidate State: .
4. Cell State Update: .
5. Output Gate & Hidden State: , .
Why Gradients Do Not Vanish:
The Jacobian of the cell state is: .
Across time steps, the backward gradient is: .
If the forget gate is near 1 (), the gradient propagates across hundreds of time steps with zero exponential decay!
🏭 Production Trade-offs
Initialization trick: Always initialize forget gate bias to a positive constant (e.g., or ). This forces at step 0, ensuring the cell starts by remembering historical information.
⚠️ Common Interview Pitfalls
- ✕Initializing forget gate biases to 0, which sets and attenuates memory retention by per step at initialization
- ✕Assuming LSTM cell state can grow unboundedly; without forget gates (), variance drifts to infinity
🎯 Interviewer Follow-up Inquiries
- ?Why must the forget gate bias 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.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
解释 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 初始化对长记忆的影响