欢迎 fastText 加入 Hugging Face Hub

发布于 2023 年 6 月 6 日
在 GitHub 上更新

fastText 是一个用于高效学习文本表示和分类的库。fastText 于 2016 年由 Meta AI 开源,它融合了过去几十年来在自然语言处理和机器学习领域具有影响力的几个关键思想:使用词袋和 n-gram 袋来表示句子,使用子词信息,以及利用隐藏表示来在不同类别之间共享信息。

为了加快计算速度,fastText 利用类别不均衡分布的特点,采用了分层 softmax (hierarchical softmax)。所有这些技术都为用户提供了可扩展的文本表示和分类解决方案。

Hugging Face 现在托管了全部 157 种语言的官方词向量镜像和最新的语种识别模型。这意味着,通过 Hugging Face,你只需几条命令就可以轻松下载和使用这些模型。

查找模型

157 种语言的词向量和语种识别模型可以在 Meta AI 组织中找到。例如,你可以在这里找到英文词向量的模型页面,在这里找到语种识别模型。

小组件

本次集成包括对文本分类和特征提取小组件的支持。快来这里试试语种识别小组件,以及在这里试试特征提取小组件吧!

text_classification_widget feature_extraction_widget

如何使用

以下是如何加载和使用预训练词向量

>>> import fasttext
>>> from huggingface_hub import hf_hub_download

>>> model_path = hf_hub_download(repo_id="facebook/fasttext-en-vectors", filename="model.bin")
>>> model = fasttext.load_model(model_path)
>>> model.words

['the', 'of', 'and', 'to', 'in', 'a', 'that', 'is', ...]

>>> len(model.words)

145940

>>> model['bread']

array([ 4.89417791e-01,  1.60882145e-01, -2.25947708e-01, -2.94273376e-01,
       -1.04577184e-01,  1.17962055e-01,  1.34821936e-01, -2.41778508e-01, ...])

以下是如何使用此模型查询英文词向量的最近邻

>>> import fasttext
>>> from huggingface_hub import hf_hub_download

>>> model_path = hf_hub_download(repo_id="facebook/fasttext-en-nearest-neighbors", filename="model.bin")
>>> model = fasttext.load_model(model_path)
>>> model.get_nearest_neighbors("bread", k=5)

[(0.5641006231307983, 'butter'), 
 (0.48875734210014343, 'loaf'), 
 (0.4491206705570221, 'eat'), 
 (0.42444291710853577, 'food'), 
 (0.4229326844215393, 'cheese')]

以下是如何使用此模型检测给定文本的语种

>>> import fasttext
>>> from huggingface_hub import hf_hub_download

>>> model_path = hf_hub_download(repo_id="facebook/fasttext-language-identification", filename="model.bin")
>>> model = fasttext.load_model(model_path)
>>> model.predict("Hello, world!")

(('__label__eng_Latn',), array([0.81148803]))

>>> model.predict("Hello, world!", k=5)

(('__label__eng_Latn', '__label__vie_Latn', '__label__nld_Latn', '__label__pol_Latn', '__label__deu_Latn'), 
 array([0.61224753, 0.21323682, 0.09696738, 0.01359863, 0.01319415]))

你想将你的库集成到 Hub 吗?

这次集成得以实现,得益于我们与 Meta AI 的合作以及 huggingface_hub 库。该库为我们所有支持的库提供了小组件和 API。如果你也想将自己的库集成到 Hub,我们为你准备了一份指南

社区

注册登录 以发表评论