# FastText

**fastText** 是一个用于高效学习词表示和文本分类的开源库，由 Facebook 人工智能研究院（FAIR）实验室开发，于 2016 年 8 月 18 日发布<sup>[1](https://engineering.fb.com/2016/08/18/ml-applications/fair-open-sources-fasttext/)</sup>。它将快速线性文本分类器与基于字符 n 元语法构建的词嵌入相结合，其实验表明，其准确率往往与深度学习分类器相当，而训练和评估速度却快上若干个数量级<sup>[2](https://arxiv.org/abs/1607.01759)</sup>。FAIR 报告称，在情感分析和标签预测等标准问题上，训练时间从数天缩短到了几秒<sup>[1](https://engineering.fb.com/2016/08/18/ml-applications/fair-open-sources-fasttext/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 起源 | 由 Facebook 人工智能研究院于 2016 年 8 月 18 日开源<sup>[1](https://engineering.fb.com/2016/08/18/ml-applications/fair-open-sources-fasttext/)</sup> |
| 奠基论文 | 《Bag of Tricks for Efficient Text Classification》（Joulin、Grave、Bojanowski、Mikolov，2016）与《Enriching Word Vectors with Subword Information》（Bojanowski、Grave、Joulin、Mikolov，TACL 2017）<sup>[2](https://arxiv.org/abs/1607.01759)</sup><sup> • </sup><sup>[3](https://arxiv.org/abs/1607.04606)</sup> |
| 训练速度 | 在标准多核 CPU 上，不到十分钟即可训练超过十亿词；不到一分钟即可将五十万个句子分类到 312K 个类别中<sup>[2](https://arxiv.org/abs/1607.01759)</sup> |
| 子词模型 | 每个词都是字符 n 元语法（默认 3 至 6 个字符）的集合；词的向量是其 n 元语法向量之和，因此可以为未登录词计算向量<sup>[3](https://arxiv.org/abs/1607.04606)</sup><sup> • </sup><sup>[4](https://github.com/facebookresearch/fastText/blob/main/README.md?plain=1)</sup> |
| 分类器架构 | 词特征和 n 元语法特征被嵌入，平均为一个隐藏变量，送入带秩约束的线性分类器；层次 softmax 将训练复杂度降至 O(h log₂(k))<sup>[5](https://aclanthology.org/E17-2068.pdf)</sup> |
| 许可证与状态 | 许可证在 0.2.0 版本中从 BSD+专利改为 MIT；GitHub 仓库于 2024 年 3 月 19 日归档，最新版本为 v0.9.2<sup>[6](https://github.com/facebookresearch/fastText/releases)</sup> |

## 分类器

《Bag of Tricks for Efficient Text Classification》中描述的文本分类器是一个带秩约束的线性模型。第一个权重矩阵 A 是词上的查找表；词表示被平均为文本表示，送入线性分类器。该架构类似于 word2vec 的连续词袋模型，只是中间词被替换为标签，并使用随机梯度下降和线性衰减的学习率在多 CPU 上异步训练<sup>[5](https://aclanthology.org/E17-2068.pdf)</sup>。

两个技巧带来了大部分的速度和准确率提升。第一，当类别数量很大时，计算完整的 softmax 对于 k 个类别和隐藏维度 h 的代价为 O(kh)；fastText 改用基于[霍夫曼编码](https://www.edgepedia.org/zh/huffman-coding)树的层次 softmax，将训练复杂度降至 O(h log₂(k))，并允许以 O(log(T)) 的代价用二叉堆找到前 T 个目标<sup>[5](https://aclanthology.org/E17-2068.pdf)</sup>。第二，由于词袋忽略词序，fastText 添加词 n 元语法的集合作为额外特征，以捕获部分局部词序信息。n 元语法通过哈希技巧进行映射，仅使用二元语法时使用 1000 万个桶，否则使用 1 亿个桶，因此无需存储 n 元语法词典<sup>[5](https://aclanthology.org/E17-2068.pdf)</sup>。

**报告结果。** 在八个情感分析数据集上，使用 10 个隐藏单元和二元语法的 fastText 在 AG 上得分为 92.5，在 Sogou 上为 96.8，在 DBpedia 上为 98.6，在 Yelp 极性上为 95.7，在 Yelp 完整版上为 63.9，在 Yahoo Answers 上为 72.3，在 Amazon 完整版上为 60.2，在 Amazon 极性上为 94.6，略优于 char-CNN 和 char-CRNN，略逊于 VDCNN<sup>[5](https://aclanthology.org/E17-2068.pdf)</sup>。添加二元语法信息使性能提升了 1% 到 4%<sup>[5](https://aclanthology.org/E17-2068.pdf)</sup>。速度差异大于准确率差异：在 AG 上训练一个 epoch 耗时 1 秒，在 Amazon 极性上耗时 10 秒，而极深卷积网络 VDCNN 则需要 24 分钟到 7 小时<sup>[5](https://aclanthology.org/E17-2068.pdf)</sup>。在类别超过 300K 的标签预测数据集上，fastText 的训练速度比 Tagspace 快一个数量级以上，且质量更好，测试阶段加速达到 600 倍<sup>[5](https://aclanthology.org/E17-2068.pdf)</sup>。

## 子词词向量

第二篇论文《Enriching Word Vectors with Subword Information》解决了 word2vec 等模型的一个局限：这些模型为每个词分配一个独立的向量，因而忽略了形态学，这对于词汇量大、罕见词多的语言尤其成问题。fastText 的方法基于跳元模型，其中每个词被表示为字符 n 元语法的集合；每个 n 元语法关联一个向量，词被表示为这些向量之和<sup>[3](https://arxiv.org/abs/1607.04606)</sup>。由于词的表示由片段组成，该模型可以为训练数据中未出现的词计算向量<sup>[3](https://arxiv.org/abs/1607.04606)</sup>。默认情况下，词向量考虑 3 到 6 个字符的字符 n 元语法<sup>[4](https://github.com/facebookresearch/fastText/blob/main/README.md?plain=1)</sup>。

该方法在九种语言的词相似度和类比任务上进行了评估，与近期提出的形态学词表示相比，这些向量达到了最先进的性能<sup>[3](https://arxiv.org/abs/1607.04606)</sup>。后来一项针对 Word2Vec、GloVe 和 fastText 的稳定性研究发现，fastText 是三种嵌入方法中最稳定的，其次是 GloVe 和 Word2Vec<sup>[7](https://arxiv.org/abs/2104.08433)</sup>。

## 库与工具

该库支持两个主要用例：词表示学习和文本分类，通过 `skipgram`、`cbow` 和 `supervised` 等子命令暴露，并用 `test` 和 `predict` 进行评估和推理<sup>[4](https://github.com/facebookresearch/fastText/blob/main/README.md?plain=1)</sup>。训练好的模型可以通过 `print-word-vectors` 为未登录词计算向量<sup>[4](https://github.com/facebookresearch/fastText/blob/main/README.md?plain=1)</sup>。监督模型可以用 `quantize` 命令进行量化，生成内存占用更小的 `.ftz` 文件；该过程遵循论文《FastText.zip: Compressing text classification models》，该论文考虑生成适合有限内存的紧凑分类架构<sup>[4](https://github.com/facebookresearch/fastText/blob/main/README.md?plain=1)</sup><sup> • </sup><sup>[8](https://arxiv.org/abs/1612.03651)</sup>。教程工作流程还记录了主要的调优选项：epoch 数（标准范围 5 到 50）、学习率（标准范围 0.1 到 1.0）、词 n 元语法（标准范围 1 到 5），以及用于多标签分类的 `one-vs-all` 损失<sup>[9](https://github.com/facebookresearch/fastText/blob/main/docs/supervised-tutorial.md?plain=1)</sup>。

发布历史记录了项目的演进：0.2.0 版本将许可证从 BSD+专利改为 MIT，并引入了测试版 C++ API 和官方 Python 支持；v0.9.1 将官方 `fastText` 模块与非官方 PyPI `fasttext` 模块合并；v0.9.2（2020 年 4 月 28 日）添加了 [WebAssembly](https://www.edgepedia.org/zh/webassembly) 绑定和用于自动超参数优化的 autotune 功能<sup>[6](https://github.com/facebookresearch/fastText/releases)</sup>。GitHub 仓库于 2024 年 3 月 19 日被其所有者归档，现为只读<sup>[6](https://github.com/facebookresearch/fastText/releases)</sup>，而 PyPI 上的 `fasttext` 包则在 MIT 许可证下于 2024 年 6 月 12 日继续发布了 0.9.3 版本<sup>[10](https://pypi.org/project/fasttext/)</sup>。

## 应用与局限

**语言识别。** fastText 附带预训练的语言识别模型，旧版本覆盖 157 种语言<sup>[11](https://huggingface.co/facebook/fasttext-language-identification?library=fasttext)</sup>。2020 年，Facebook 人工智能研究院创建了 LASER 2.0 和改进的 fastText 语言识别模型，以提高为其多对多多语言翻译模型挖掘平行数据的质量<sup>[12](https://about.fb.com/news/2020/10/first-multilingual-machine-translation-model/)</sup>。该方法在短输入上有已知弱点：一项语言识别模型的基准比较观察到，fastText 在混合多语言句子中仅检测出 3 种语言中的 2 种，且一个错误检测获得了比正确的中文更高的概率<sup>[13](https://reflector.vtti.vt.edu/cran/web/packages/fastText/vignettes/language_identification.html)</sup>。

**低资源环境下的数据质量。** 现成的多语言 fastText 嵌入可能受到严重污染。Alabi 等人（2020）发现，约鲁巴语的 fastText 嵌入中，15 万词里估计有 13.5 万词属于英语、法语和阿拉伯语等其他语言，而在较小但经过整理的高质量数据集上训练的新嵌入优于现成向量<sup>[14](https://aclanthology.org/2022.acl-long.265.pdf)</sup>。

**与 Transformer 的对比。** fastText 仍被用作快速基线和生产分类器。在一项临床编码研究中，对于长度超过 512 个词元的叙述文本，fastText 的分类性能略优于基于 Transformer 的架构，这一长度限制约束了 BERT 风格的模型，但不约束 fastText<sup>[15](https://pmc.ncbi.nlm.nih.gov/articles/PMC11541716/)</sup>。然而，与 Word2Vec 和 GloVe 一样，fastText 为每个词生成一个静态的、与上下文无关的向量，因此无法捕获多义词或依赖上下文的含义，而且字符 n 元语法方法在 n 元语法跨不相关词共享时会引入歧义<sup>[16](https://mbrenndoerfer.com/writing/subword-tokenization-fasttext-character-ngram-embeddings-robust-word-representations)</sup>。

## References

1. [FAIR open-sources fastText, Engineering at Meta (August 18, 2016)](https://engineering.fb.com/2016/08/18/ml-applications/fair-open-sources-fasttext/)
2. [Joulin, Grave, Bojanowski, Mikolov (2016). Bag of Tricks for Efficient Text Classification. arXiv:1607.01759](https://arxiv.org/abs/1607.01759)
3. [Bojanowski, Grave, Joulin, Mikolov (2016/2017). Enriching Word Vectors with Subword Information. arXiv:1607.04606](https://arxiv.org/abs/1607.04606)
4. [fastText README, facebookresearch/fastText, GitHub](https://github.com/facebookresearch/fastText/blob/main/README.md?plain=1)
5. [Joulin et al. (2017). Bag of Tricks for Efficient Text Classification, EACL 2017](https://aclanthology.org/E17-2068.pdf)
6. [Releases, facebookresearch/fastText, GitHub](https://github.com/facebookresearch/fastText/releases)
7. [Are Word Embedding Methods Stable and Should We Care About It? arXiv:2104.08433](https://arxiv.org/abs/2104.08433)
8. [Joulin et al. (2016). FastText.zip: Compressing text classification models. arXiv:1612.03651](https://arxiv.org/abs/1612.03651)
9. [Text classification tutorial, fastText docs](https://github.com/facebookresearch/fastText/blob/main/docs/supervised-tutorial.md?plain=1)
10. [fasttext, PyPI](https://pypi.org/project/fasttext/)
11. [facebook/fasttext-language-identification, Hugging Face](https://huggingface.co/facebook/fasttext-language-identification?library=fasttext)
12. [Introducing the First AI Model That Translates 100 Languages Without Relying on English, Meta (2020)](https://about.fb.com/news/2020/10/first-multilingual-machine-translation-model/)
13. [Language Identification using the fastText package (a Benchmark), fastText R package vignette](https://reflector.vtti.vt.edu/cran/web/packages/fastText/vignettes/language_identification.html)
14. [Garbage in, Garbage out: data quality issues for African languages, ACL 2022](https://aclanthology.org/2022.acl-long.265.pdf)
15. [Multi-label text classification via secondary use of large clinical real-world data sets, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC11541716/)
16. [Subword Tokenization and FastText, mbrenndoerfer.com](https://mbrenndoerfer.com/writing/subword-tokenization-fasttext-character-ngram-embeddings-robust-word-representations)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Language and vision AI › Natural language processing › NLP tasks and methods › Text classification and categorization

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/fasttext

English original: https://www.edgepedia.org/fasttext
