# Flair（自然语言处理软件）

**Flair** 是一个用于自然语言处理（NLP）的开源 Python 框架，构建在 PyTorch 深度学习库之上；它为命名实体识别（NER）、词性标注和情感分析等任务打包了预训练模型，并让用户训练自己的序列标注和文本分类模型<sup>[1](https://engineering.zalando.com/posts/2018/11/zalando-research-releases-flair.html)</sup>。它由 Zalando 研究院（Zalando Research）于 2018 年 11 月发布，并以 MIT 许可证分发<sup>[1](https://engineering.zalando.com/posts/2018/11/zalando-research-releases-flair.html)</sup><sup> • </sup><sup>[2](https://arxiv.org/abs/2008.07347)</sup>。

| 关键事实 | 详情 |
|---|---|
| 起源 | 由 Zalando 研究院于 2018 年 11 月发布；基于 PyTorch 的 Python<sup>[1](https://engineering.zalando.com/posts/2018/11/zalando-research-releases-flair.html)</sup> |
| 标志性嵌入 | 来自训练好的字符语言模型的上下文串嵌入（阿克比克（Akbik）等，COLING 2018）<sup>[3](https://aclanthology.org/C18-1139/)</sup> |
| 主要标注器 | `SequenceTagger`，一个默认使用 CRF 解码的 LSTM-CRF 模型<sup>[4](https://flairnlp.github.io/flair/master/api/generated/flair.models.SequenceTagger.html)</sup> |
| NER 准确率 | 在 OntoNotes 5.0 上为 89.7 F1，在 CoNLL-2003 上为 93.1，如 spaCy 的基准表所报告<sup>[5](https://spacy.io/usage/facts-figures)</sup> |
| 处理速度 | 在 10,000 条 Reddit 评论上测量，`pos`-fast 和 `ner`-fast 管道在 CPU 上每秒处理 323 个词，在 GPU 上每秒处理 1,184 个词<sup>[5](https://spacy.io/usage/facts-figures)</sup> |
| 生物医学支持 | HunFlair 标注器覆盖五种生物医学实体类型，基于在约 2400 万篇 PubMed 摘要和 300 万篇 PMC 全文上训练的字符语言模型<sup>[6](https://pmc.ncbi.nlm.nih.gov/articles/PMC8428609/)</sup> |
| 当前版本 | 0.15.1，于 2025 年 2 月 5 日发布在 PyPI 上<sup>[7](https://pypi.org/project/flair/)</sup> |

## 起源与上下文串嵌入

Flair 源于 Zalando 研究院的艾伦·阿克比克（Alan Akbik）、邓肯·布莱斯（Duncan Blythe）和罗兰·沃尔格拉夫（Roland Vollgraf）于 2018 年发表的一篇研究论文；他们提出利用训练好的字符语言模型的内部状态来生成他们所称的上下文串嵌入<sup>[3](https://aclanthology.org/C18-1139/)</sup>。这些嵌入有两个决定性属性：它们在训练时不使用任何显式的词概念，而是将词建模为字符序列；它们由周围文本提供上下文，因此同一个词会因用法不同而得到不同的嵌入<sup>[3](https://aclanthology.org/C18-1139/)</sup>。在四项经典序列标注任务中，这些嵌入超过了此前的最先进水平，包括在 CoNLL-2003 英语和德语 NER 共享任务上取得新的 F1 分数<sup>[3](https://aclanthology.org/C18-1139/)</sup>。作者在 Flair 框架中发布了代码和预训练语言模型，以便复现<sup>[3](https://aclanthology.org/C18-1139/)</sup>。

由于这些嵌入将词建模为字符序列，它们能很好地处理罕见词和拼写错误的词，并能捕获标准词嵌入之外的潜在句法-语义信息<sup>[8](https://flairnlp.github.io/docs/tutorial-embeddings/flair-embeddings)</sup>。NAACL-HLT 2019 上的一篇后续演示论文将 Flair 呈现为一个通用框架，用于训练和分发序列标注、文本分类和语言模型；它采用单一模型架构，无需额外工程即可使用任何类型的词嵌入<sup>[9](https://aclanthology.org/N19-4010.pdf)</sup>。

## 架构与模型

用于序列标注的核心模型类是 `SequenceTagger`，它是库中两种主要标注架构之一。它实现了经典的 LSTM-CRF 设计：词首先使用一个或多个 `TokenEmbeddings` 进行嵌入，嵌入被传入 LSTM，隐藏状态馈入 softmax 分类器，默认使用条件随机场（CRF）进行解码<sup>[4](https://flairnlp.github.io/flair/master/api/generated/flair.models.SequenceTagger.html)</sup>。一个更简单的 `TokenClassifier` 类在不使用 LSTM-CRF 的情况下执行序列标注<sup>[4](https://flairnlp.github.io/flair/master/api/generated/flair.models.SequenceTagger.html)</sup>。对于文本分类，库提供了单独的 `TextClassifier`<sup>[10](https://github.com/flairNLP/flair/blob/master/docs/tutorial/tutorial-training/how-model-training-works.md)</sup>。

**嵌入是核心概念。** 每个嵌入类实现词元级或文档级接口，任意嵌入组合都可以堆叠到一个模型中<sup>[9](https://aclanthology.org/N19-4010.pdf)</sup>。该堆栈包括经典词嵌入（如 GloVe）、Flair 自有的上下文串嵌入，以及 Transformer 嵌入：用于词或词元的 `TransformerWordEmbeddings` 和用于整篇文档的 `TransformerDocumentEmbeddings`，支持 BERT 和 XLNet 等 HuggingFace 架构<sup>[11](https://github.com/flairNLP/flair/blob/master/resources/docs/embeddings/TRANSFORMER_EMBEDDINGS.md)</sup>。Transformer 嵌入选项包括从哪些层提取、如何池化子词元、是否微调，以及是否包含句子边界之外的上下文<sup>[11](https://github.com/flairNLP/flair/blob/master/resources/docs/embeddings/TRANSFORMER_EMBEDDINGS.md)</sup>。

对于最先进的 NER，文档建议使用完整文档上下文微调 Transformer 嵌入，这就是 FLERT 论文中描述的方法；该论文报告在多个 CoNLL-03 基准数据集上取得新的最先进分数；该方法已集成到 Flair 中以便复现<sup>[12](https://flairnlp.github.io/flair/v0.14.0/tutorial/tutorial-training/how-to-train-sequence-tagger.html)</sup><sup> • </sup><sup>[13](https://arxiv.org/abs/2011.06993)</sup>。

## 使用预训练模型

应用预训练模型只需几行代码：创建一个 `Sentence` 对象，加载分类器（如 `Classifier.load('ner')` 或 `Classifier.load('sentiment')`），然后调用 `predict`<sup>[14](https://flairnlp.github.io/docs/intro)</sup>。在示例句子“我爱柏林（Berlin）和纽约（New York）。”上，NER 模型将“Berlin”和“New York”标记为地点实体，情感模型将句子标记为正面，置信度为 0.9982<sup>[14](https://flairnlp.github.io/docs/intro)</sup>。教程系列涵盖基础标注、模型训练与微调、嵌入，以及关于标注、实体链接和训练自定义 NER 模型的 HunFlair2 生物医学教程<sup>[15](https://flairnlp.github.io/flair/master/tutorial/index.html)</sup>。

## 生物医学文本分析

集成到 Flair 中的 HunFlair 是一个生物医学 NER 标注器，覆盖五种实体类型：细胞系、化学物质、疾病、基因和物种<sup>[6](https://pmc.ncbi.nlm.nih.gov/articles/PMC8428609/)</sup><sup> • </sup><sup>[16](https://pmc.ncbi.nlm.nih.gov/articles/PMC11453098/)</sup>。其核心依赖一个 Flair 字符级语言模型，该模型在来自 PubMed 的约 2400 万篇摘要和来自 [PubMed Central](https://www.edgepedia.org/zh/pubmed-central) 的 300 万篇全文上训练，并结合 fastText 词嵌入<sup>[6](https://pmc.ncbi.nlm.nih.gov/articles/PMC8428609/)</sup>。0.14.0 版本（2024 年 7 月）增加了改进的生物医学 NER 和一个最先进的生物医学实体链接模型，并支持参数高效微调<sup>[17](https://github.com/flairNLP/flair/releases)</sup>。后继者 HunFlair2 支持同样五种实体类型的 NER 和归一化，并且同样构建在 Flair 框架上<sup>[16](https://pmc.ncbi.nlm.nih.gov/articles/PMC11453098/)</sup>。

## 基准与比较

在 spaCy 公布的基准表中，Flair 在 OntoNotes 5.0 NER 语料库上达到 89.7 F1，在 CoNLL-2003 上达到 93.1；相比之下，spaCy RoBERTa（2020）为 89.8 和 91.6，Stanza 为 88.8 和 92.1<sup>[5](https://spacy.io/usage/facts-figures)</sup>。在 10,000 条 Reddit 评论上的端到端处理速度方面，Flair 的快速管道在 CPU 上每秒处理 323 个词，在 GPU 上每秒处理 1,184 个词，慢于 spaCy 的 `en_core_web_lg` 管道的每秒 10,014 个词和 14,954 个词<sup>[5](https://spacy.io/usage/facts-figures)</sup>。

Flair 嵌入还被与 BERT 和 ELMo 比较，作为跨 54 种语言的词性标注、词形还原和依存句法分析的预训练输入<sup>[19](https://arxiv.org/abs/1908.07448)</sup>；在词义消歧中，维德曼（Wiedemann）及其同事认为 BERT 风格模型更擅长找到多义词的正确义项<sup>[20](https://aclanthology.org/2023.nlp4call-1.pdf)</sup>。

## 发布与维护

该项目在 GitHub 上的 flairNLP/flair 下积极维护<sup>[21](https://github.com/flairNLP/flair)</sup>。近期版本显示了其方向：0.14.0（2024 年 7 月 25 日）增加了生物医学功能并精简了依赖，使库安装更快、占用空间更小<sup>[17](https://github.com/flairNLP/flair/releases)</sup>；0.15.0（2024 年 12 月 20 日）增加了多 GPU 支持、重新设计了带 API 文档的文档站点、停止支持 Python 3.8，并因 CVE-2024-10073 直接移除了 `flair.models.clustering` 模块<sup>[17](https://github.com/flairNLP/flair/releases)</sup>；随后 0.15.1 于 2025 年 2 月 5 日发布<sup>[7](https://pypi.org/project/flair/)</sup>。

文档报告称，其社区约有 200 名贡献者，Flair 被数百家公司、超过 2,000 个开源项目和 2,000 多篇论文使用<sup>[22](https://flairnlp.github.io/flair/)</sup>。

## References

1. [Zalando Research Releases "Flair", Zalando Engineering Blog (2018)](https://engineering.zalando.com/posts/2018/11/zalando-research-releases-flair.html)
2. [HunFlair: An Easy-to-Use Tool for State-of-the-Art Biomedical Named Entity Recognition, arXiv:2008.07347](https://arxiv.org/abs/2008.07347)
3. [Akbik, Blythe, Vollgraf (2018). Contextual String Embeddings for Sequence Labeling, COLING 2018](https://aclanthology.org/C18-1139/)
4. [flair.models.SequenceTagger, Flair Documentation](https://flairnlp.github.io/flair/master/api/generated/flair.models.SequenceTagger.html)
5. [Facts & Figures, spaCy Usage Documentation](https://spacy.io/usage/facts-figures)
6. [HunFlar: an easy-to-use tool for state-of-the-art biomedical named entity recognition, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC8428609/)
7. [flair on PyPI](https://pypi.org/project/flair/)
8. [Flair embeddings, Flair Documentation](https://flairnlp.github.io/docs/tutorial-embeddings/flair-embeddings)
9. [Akbik et al. (2019). FLAIR: An Easy-to-Use Framework for State-of-the-Art NLP, NAACL-HLT 2019 Demonstrations](https://aclanthology.org/N19-4010.pdf)
10. [How model training works, Flair Documentation (GitHub)](https://github.com/flairNLP/flair/blob/master/docs/tutorial/tutorial-training/how-model-training-works.md)
11. [Transformer Embeddings, Flair Documentation (GitHub)](https://github.com/flairNLP/flair/blob/master/resources/docs/embeddings/TRANSFORMER_EMBEDDINGS.md)
12. [Train a sequence tagger, Flair Documentation v0.14.0](https://flairnlp.github.io/flair/v0.14.0/tutorial/tutorial-training/how-to-train-sequence-tagger.html)
13. [FLERT: Document-Level Features for Named Entity Recognition, arXiv:2011.06993](https://arxiv.org/abs/2011.06993)
14. [Quick Start, Flair Documentation](https://flairnlp.github.io/docs/intro)
15. [Tutorials, Flair Documentation](https://flairnlp.github.io/flair/master/tutorial/index.html)
16. [HunFlair2 in a cross-corpus evaluation of biomedical named entity recognition and normalization tools, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC11453098/)
17. [Releases, flairNLP/flair on GitHub](https://github.com/flairNLP/flair/releases)
18. [6 Results and Analysis, DACL 2022](https://aclanthology.org/2022.dadc-1.pdf)
19. [Evaluating Contextualized Embeddings on 54 Languages in POS Tagging, Lemmatization and Dependency Parsing, arXiv:1908.07448](https://arxiv.org/abs/1908.07448)
20. [2.1 Flair Embeddings, NLP4CALL 2023](https://aclanthology.org/2023.nlp4call-1.pdf)
21. [flairNLP/flair on GitHub](https://github.com/flairNLP/flair)
22. [Home, Flair Documentation](https://flairnlp.github.io/flair/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › 语言与视觉人工智能 › 自然语言处理 › NLP软件、人物与社区 › NLP工具包与库

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/flair-nlp-software

English original: https://www.edgepedia.org/flair-nlp-software
