# 双向LSTM

双向LSTM（BiLSTM）是一种循环神经网络，它在同一序列上以相反方向运行两个长短期记忆层，一个从第一个时间步到最后一个时间步，另一个从最后一个时间步到第一个时间步，并合并它们的输出，使每个位置都能同时以其过去和未来的上下文来表示。<sup>[1](https://www.mathworks.com/help/deeplearning/ug/create-bilstm-function.html)</sup> 合并后的表示通常是两个隐藏状态的拼接，使隐藏宽度加倍，用于在需要输出之前整个输入已经可用的任务：诸如命名实体识别之类的词元标注、句子级分类、语音识别声学建模以及时间序列分析。<sup>[2](https://d2l.ai/chapter_recurrent-modern/bi-rnn.html)</sup> 该架构由亚历克斯·格雷夫斯（Alex Graves）和[于尔根·施密德胡伯](https://www.edgepedia.org/zh/jurgen-schmidhuber)（Jürgen Schmidhuber）于2005年提出，用于音素分类。<sup>[3](https://doi.org/10.1016/j.neunet.2005.06.042)</sup>

| 关键事实 | 详情 |
|---|---|
| 结构 | 同一输入上的两个LSTM层，一个前向、一个后向；输出拼接为宽度 \( 2h \)<sup>[2](https://d2l.ai/chapter_recurrent-modern/bi-rnn.html)</sup> |
| 起源 | 双向RNN：Schuster和Paliwal，1997<sup>[4](https://doi.org/10.1109/78.650093)</sup>；LSTM：霍赫赖特和施密德胡伯，1997<sup>[5](https://doi.org/10.1162/neco.1997.9.8.1735)</sup>；BiLSTM：格雷夫斯和施密德胡伯，2005<sup>[3](https://doi.org/10.1016/j.neunet.2005.06.042)</sup> |
| 训练 | 在展开后的网络上进行时间反向传播，两个方向均如此<sup>[6](https://maxwell.ict.griffith.edu.au/spl/publications/papers/ieeesp97_schuster.pdf)</sup> |
| 语音结果 | 深度BiLSTM配合三元语言模型在《华尔街日报》上达到8.2%的词错误率，与基线系统结合后为6.7%<sup>[7](https://proceedings.mlr.press/v32/graves14.html)</sup> |
| 成本 | 一个LSTM层每个方向有 \( O(4 \cdot d_{\mathrm{h}} \cdot (d_{\mathrm{x}} + d_{\mathrm{h}})) \) 个参数；双向性大致使参数和计算量加倍<sup>[8](https://arxiv.org/abs/2410.01201)</sup><sup> • </sup><sup>[9](https://deeplearningnotes.com/rnns-attention/advanced-rnns/bidirectional-rnns)</sup> |
| 硬性限制 | 无法流式处理或自回归生成，因为反向传递需要整个输入序列<sup>[10](https://d2l.smola.org/chapter_recurrent-modern/lstm.html)</sup> |

## 工作原理

LSTM单元用由自连接承载的细胞状态取代了普通RNN的单一循环；该自连接称为恒定误差轮盘（LSTM单元的自连接，用于随时间保持误差），由乘性门保护；1997年的原始单元使用固定的单位权重自连接，而在下文所示的现代循环中，遗忘门对该连接进行缩放。<sup>[11](https://web.stanford.edu/class/psych209/Readings/HochreiterSchmidthuber97LSTM.pdf)</sup> 其动机来自对现有RNN中误差流的分析，该分析发现长时间滞后无法被捕获，因为反向传播的误差要么爆炸要么指数衰减；该单元的线性自[连接](https://www.edgepedia.org/zh/concatenation)旨在在长时间滞后上保持误差流而不是让其消失，而门则学习何时打开和关闭对它的访问。<sup>[12](https://proceedings.neurips.cc/paper_files/paper/1996/file/a4d2f0d23dcc84ce983ff9157f8b7f88-Paper.pdf)</sup>

具体而言，在每个时间步 \( t \)，单元根据前一隐藏状态和当前输入，通过sigmoid函数计算输入门、遗忘门和输出门，并通过tanh函数计算一个候选值，然后更新：<sup>[13](https://aclanthology.org/P16-1101.pdf)</sup>

\[ i_{t} = \sigma(W_{i} h_{t-1} + U_{i} x_{t} + b_{i}), \quad f_{t} = \sigma(W_{f} h_{t-1} + U_{f} x_{t} + b_{f}) \]
\[ \tilde{c}_{t} = \tanh(W_{c} h_{t-1} + U_{c} x_{t} + b_{c}), \quad c_{t} = f_{t} \cdot c_{t-1} + i_{t} \cdot \tilde{c}_{t} \]
\[ o_{t} = \sigma(W_{o} h_{t-1} + U_{o} x_{t} + b_{o}), \quad h_{t} = o_{t} \cdot \tanh(c_{t}) \]

遗忘门将自环权重缩放于0到1之间，接近1时保留过去信息，接近0时丢弃；输入门控制候选值有多少进入细胞状态，输出门控制有多少作为隐藏状态离开。<sup>[14](https://www.ncbi.nlm.nih.gov/books/NBK597502/)</sup><sup> • </sup><sup>[8](https://arxiv.org/abs/2410.01201)</sup>

双向性来自Schuster和Paliwal的双向RNN思想：每个训练序列被正向和反向呈现给两个独立的循环网络，两者都连接到同一输出层，而两个子网络的输出彼此不相连。<sup>[15](https://www.cs.toronto.edu/~graves/nn_2005.pdf)</sup><sup> • </sup><sup>[14](https://www.ncbi.nlm.nih.gov/books/NBK597502/)</sup> 在每个位置 \( t \)，前向网络计算左上下文的表示，后向网络计算右上下文的表示，两者被拼接，给出维度为2h的隐藏状态；在深度双向网络中，这个合并状态成为下一个双向层的输入。<sup>[16](https://aclanthology.org/N16-1030.pdf)</sup><sup> • </sup><sup>[2](https://d2l.ai/chapter_recurrent-modern/bi-rnn.html)</sup> 拼接是默认的合并方式，但框架也提供求和、相乘、平均或不合并。<sup>[17](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Bidirectional)</sup>

## 实现方式

在PyTorch中，在nn.LSTM上设置bidirectional=True会使每个时间步的输出成为前向和反向隐藏状态的拼接，形状为 \( (L, D \cdot H_{\mathrm{out}}) \)，对于无批次输入 \( D = 2 \)，对于有批次输入，默认 \( \mathrm{batch\_first} = \mathrm{False} \) 时为 \( (L, N, D \cdot H_{\mathrm{out}}) \)，\( \mathrm{batch\_first} = \mathrm{True} \) 时为 \( (N, L, D \cdot H_{\mathrm{out}}) \)，而最终的隐藏状态和细胞状态拼接两个方向的最终状态。<sup>[18](https://docs.pytorch.org/docs/stable/generated/torch.nn.LSTM.md)</sup> 在Keras中，围绕任意循环层的Bidirectional包装器以选定的合并模式应用相同的模式。<sup>[17](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Bidirectional)</sup>

训练使用时间反向传播，即将反向传播算法应用于在序列上展开的网络，在整个句子上运行，每个句子开始时隐藏状态重置为零；BPTT的前向和后向传递覆盖网络的两个方向。<sup>[19](https://arxiv.org/abs/1508.01991)</sup><sup> • </sup><sup>[6](https://maxwell.ict.griffith.edu.au/spl/publications/papers/ieeesp97_schuster.pdf)</sup>

## 起源

[双向循环神经网络](https://www.edgepedia.org/zh/bidirectional-recurrent-neural-network)由M. Schuster和K.K. Paliwal在1997年《IEEE信号处理汇刊》的一篇论文中提出，该论文将状态神经元分为正时间方向的前向状态和负时间方向的后向状态，并在TIMIT音素分类上评估了这一思想。<sup>[4](https://doi.org/10.1109/78.650093)</sup><sup> • </sup><sup>[6](https://maxwell.ict.griffith.edu.au/spl/publications/papers/ieeesp97_schuster.pdf)</sup> [长短期记忆](https://www.edgepedia.org/zh/long-short-term-memory)本身由[塞普·霍赫赖特](https://www.edgepedia.org/zh/sepp-hochreiter)（Sepp Hochreiter）和于尔根·施密德胡伯于1997年在《神经计算》上提出。<sup>[5](https://doi.org/10.1162/neco.1997.9.8.1735)</sup>

BiLSTM——两者的结合——由亚历克斯·格雷夫斯和于尔根·施密德胡伯在2005年《神经网络》上关于逐帧音素分类的论文中提出，同时提出了一种修改后的全梯度版LSTM学习算法，允许使用标准BPTT进行训练；使用全梯度比原始截断算法性能略高。<sup>[3](https://doi.org/10.1016/j.neunet.2005.06.042)</sup><sup> • </sup><sup>[15](https://www.cs.toronto.edu/~graves/nn_2005.pdf)</sup> 这一谱系向前延伸至表示学习：ELMo使用深度双向LSTM产生上下文词嵌入，而BERT以双向注意力继承了双向编码的思想。<sup>[10](https://d2l.smola.org/chapter_recurrent-modern/lstm.html)</sup>

## 变体

**BiLSTM-CRF。** Huang、Xu和Yu（2015）将双向LSTM（利用过去和未来的输入特征）与利用句子级标签信息的条件随机场层相结合；他们报告在词性标注、组块分析和NER基准上达到最先进或接近最先进的准确率，并发现该模型稳健，对词嵌入的依赖比先前观察到的更少。<sup>[19](https://arxiv.org/abs/1508.01991)</sup> Lample及其同事（2016）在命名实体识别中达到了相同的架构，CRF在拼接的上下文表示上联合解码。<sup>[16](https://aclanthology.org/N16-1030.pdf)</sup>

**BiLSTM-CNNs-CRF。** Ma和Hovy（2016）在BiLSTM-CRF前面加入了字符级卷积网络，实现了无需手工设计特征的端到端序列标注。<sup>[20](https://doi.org/10.48550/arxiv.1603.01354)</sup><sup> • </sup><sup>[13](https://aclanthology.org/P16-1101.pdf)</sup>

**深度（堆叠）BiLSTM。** 堆叠双向层，每一层接收其下方一层的合并输出，被应用于混合语音识别。<sup>[21](https://www.cs.toronto.edu/~graves/asru_2013.pdf)</sup>

**He-BiLSTM。** Sang和Li于2024年提出的一种用于股票预测的异构双向LSTM，将传统BiLSTM的可学习参数从24个（前向12个、后向12个）减少到20个（每个方向10个）。<sup>[22](https://doi.org/10.3390/app14209158)</sup>

## 应用

双向模型适合在任何输出产生之前完整输入已存在的任务：诸如词性标注、NER和组块分析之类的词元标注、句子级分类、翻译编码器以及离线声学建模。<sup>[2](https://d2l.ai/chapter_recurrent-modern/bi-rnn.html)</sup> 在TIMIT逐帧音素分类上，双向网络优于单向网络，而LSTM比标准RNN和时间窗口MLP快得多且更准确。<sup>[15](https://www.cs.toronto.edu/~graves/nn_2005.pdf)</sup> 一个带有[连接时序分类](https://www.edgepedia.org/zh/connectionist-temporal-classification)的深度双向LSTM语音识别器在《华尔街日报》语料库上配合三元语言模型达到了8.2%的词错误率，将该网络与基线系统结合后将错误率降至6.7%。<sup>[7](https://proceedings.mlr.press/v32/graves14.html)</sup>

## 局限与替代方案

**无法流式处理或生成。** 双向网络无法原样用作因果解码器：每个隐藏状态都以 \( t \) 之后的词元为条件，因此模型无法从左到右自回归地发出词元，而训练预测下一个词元会泄露目标。<sup>[10](https://d2l.smola.org/chapter_recurrent-modern/lstm.html)</sup> 反向传递在最后一个位置已知之前无法开始，因此任何输出产生之前必须获得整个输入序列；序列到序列模型中自回归解码器侧的循环因此必须是因果的，通常为单向。

**成本。** 一个LSTM模块包含 \( O(4 \cdot d_{\mathrm{h}} \cdot (d_{\mathrm{x}} + d_{\mathrm{h}})) \) 个参数，其中 \( d_{\mathrm{x}} \) 是输入大小，\( d_{\mathrm{h}} \) 是隐藏大小；GRU通过使用更少的门和状态将其减少到 \( O(3 \cdot d_{\mathrm{h}} \cdot (d_{\mathrm{x}} + d_{\mathrm{h}})) \)。<sup>[8](https://arxiv.org/abs/2410.01201)</sup> BiLSTM携带两个具有独立权重的循环单元，因此参数、计算量和每位置隐藏状态宽度都大致加倍，尽管两个循环是独立的且可以并行运行，所以在内存和并行性允许时挂钟时间增长不到2倍。训练还受限于长梯度链以及BPTT的顺序前向传递和存储状态。<sup>[2](https://d2l.ai/chapter_recurrent-modern/bi-rnn.html)</sup><sup> • </sup><sup>[14](https://www.ncbi.nlm.nih.gov/books/NBK597502/)</sup>

**对比Transformer。** GRU和LSTM是只能顺序计算的模型，需要BPTT，训练时间为线性但限制了长上下文的可扩展性；Transformer通过并行化训练取代它们成为默认序列模型，代价是序列长度的二次复杂度。<sup>[8](https://arxiv.org/abs/2410.01201)</sup>

**2023年后的发展。** [xLSTM](https://www.edgepedia.org/zh/xlstm)架构以指数门控机制扩展了LSTM思想，旨在实现更好的信息路由，并引入了两种新单元类型：具有标量记忆、标量更新和记忆混合的sLSTM，以及具有矩阵记忆和完全可并行化的协方差（外积）更新规则的mLSTM。<sup>[23](https://doi.org/10.48550/arxiv.2405.04517)</sup><sup> • </sup><sup>[24](https://proceedings.neurips.cc/paper_files/paper/2024/file/c2ce2f2701c10a2b2f2ea0bfa43cfaa3-Paper-Conference.pdf)</sup><sup> • </sup><sup>[25](https://maxbeck.ai/resources/phd_thesis_beck_xlstm.pdf)</sup>

## References

1. [Create Bidirectional LSTM (BiLSTM) Function, MathWorks](https://www.mathworks.com/help/deeplearning/ug/create-bilstm-function.html)
2. [Dive into Deep Learning 1.0.3, §10.4 Bidirectional RNNs](https://d2l.ai/chapter_recurrent-modern/bi-rnn.html)
3. [Alex Graves, Jürgen Schmidhuber (2005). Framewise phoneme classification with bidirectional LSTM and other neural network architectures. Neural Networks.](https://doi.org/10.1016/j.neunet.2005.06.042)
4. [M. Schuster, K.K. Paliwal (1997). Bidirectional recurrent neural networks. IEEE Transactions on Signal Processing.](https://doi.org/10.1109/78.650093)
5. [Sepp Hochreiter, Jürgen Schmidhuber (1997). Long Short-Term Memory. Neural Computation.](https://doi.org/10.1162/neco.1997.9.8.1735)
6. [Bidirectional Recurrent Neural Networks (IEEE Transactions on Signal Processing, 1997)](https://maxwell.ict.griffith.edu.au/spl/publications/papers/ieeesp97_schuster.pdf)
7. [Towards End-To-End Speech Recognition with Recurrent Neural Networks (Graves & Jaitly, ICML 2014)](https://proceedings.mlr.press/v32/graves14.html)
8. [Were RNNs All We Needed? (2024)](https://arxiv.org/abs/2410.01201)
9. [Bidirectional RNNs, DL Notes](https://deeplearningnotes.com/rnns-attention/advanced-rnns/bidirectional-rnns)
10. [Dive into Deep Learning, §12.1 Gated Recurrence](https://d2l.smola.org/chapter_recurrent-modern/lstm.html)
11. [Long Short-Term Memory (Hochreiter & Schmidhuber, 1997)](https://web.stanford.edu/class/psych209/Readings/HochreiterSchmidthuber97LSTM.pdf)
12. [LSTM can Solve Hard Long Time Lag Problems (Hochreiter, NeurIPS 1996)](https://proceedings.neurips.cc/paper_files/paper/1996/file/a4d2f0d23dcc84ce983ff9157f8b7f88-Paper.pdf)
13. [End-to-end Sequence Labeling via Bi-directional LSTM-CNNs-CRF (Ma & Hovy, ACL 2016)](https://aclanthology.org/P16-1101.pdf)
14. [Recurrent Neural Networks (RNNs): Architectures, Training Tricks, and Introduction to Influential Research (NCBI Bookshelf)](https://www.ncbi.nlm.nih.gov/books/NBK597502/)
15. [Framewise Phoneme Classification with Bidirectional LSTM and Other Neural Network Architectures (Graves & Schmidhuber, 2005)](https://www.cs.toronto.edu/~graves/nn_2005.pdf)
16. [Neural Architectures for Named Entity Recognition (Lample et al., NAACL 2016)](https://aclanthology.org/N16-1030.pdf)
17. [tf.keras.layers.Bidirectional, TensorFlow v2.16.1](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Bidirectional)
18. [torch.nn.LSTM, PyTorch documentation](https://docs.pytorch.org/docs/stable/generated/torch.nn.LSTM.md)
19. [Bidirectional LSTM-CRF Models for Sequence Tagging (Huang, Xu, Yu, 2015)](https://arxiv.org/abs/1508.01991)
20. [Ma, Xuezhe, Hovy, Eduard (2016). End-to-end Sequence Labeling via Bi-directional LSTM-CNNs-CRF. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1603.01354)
21. [Hybrid Speech Recognition with Deep Bidirectional LSTM (Graves et al., ASRU 2013)](https://www.cs.toronto.edu/~graves/asru_2013.pdf)
22. [Shuai Sang, Lu Li (2024). A Stock Prediction Method Based on Heterogeneous Bidirectional LSTM. Applied Sciences.](https://doi.org/10.3390/app14209158)
23. [Beck, Maximilian and colleagues (2024). xLSTM: Extended Long Short-Term Memory. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2405.04517)
24. [xLSTM: Extended Long Short-Term Memory (NeurIPS 2024)](https://proceedings.neurips.cc/paper_files/paper/2024/file/c2ce2f2701c10a2b2f2ea0bfa43cfaa3-Paper-Conference.pdf)
25. [xLSTM: Recurrent Neural Network Architectures for Scalable and Efficient Large Language Models (Beck, PhD thesis)](https://maxbeck.ai/resources/phd_thesis_beck_xlstm.pdf)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › 机器学习与神经计算 › 神经网络与深度学习 › 标题：A至H

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/bidirectional-lstm

English original: https://www.edgepedia.org/bidirectional-lstm
