fastText 是 Facebook 实验室在 2016 年发表的《Bag of Tricks for Efficient Text Classification》论文中提出的一个简单高效的文本分类方法。fastText 模型架构如下所示。
This is images...
fastText 的模型架构和 CBOW 模型非常相似,不同之处是 fastText 预测的是类别标签,而 CBOW 模型预测的是目标词。为了能够处理类别数量比较大的分类任务,fastText 引入了基于哈夫曼编码树的 hierarchical softmax。同时,使用 n-gram 作为附加特征来捕获关于局部词序信息,对于分类任务来说,局部词序可能会比全局词序在实践中更有效。
Facebook 实验室开源了 fastText 工具,所以我们可以简单安装并使用其中封装好的方法快速进行文本分类任务。本文主要介绍 fastText 工具的安装。
由于 fastText 使用了 c++11 的特征,所以如果要使用 fastText 需要一个支持 c++11 的编译器,官方推荐下面两种编译器:
这里选择熟悉的 g++ 编译器,避免版本问题,直接安装最新版本的 g++。
$ sudo apt install g++
[sudo] password for chenkc:
Reading package lists... Done
Building dependency tree
Reading state information... Done
...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.2) ...
使用 g++ --version
命令查看 g++ 编译器是否安装成功,并查看当前安装 g++ 编译器的版本号。
$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
为了方便,在家目录下创建一个目录并进入该目录(任意目录下都可以)。
$ pwd
/home/chenkc
$ mkdir fastText
$ cd fastText/
接下来使用 wget 命令从 facebookresearch 的 github 上下载 fastText 工具的压缩包,得到压缩包使用 unzip 命令进行解压。
$ wget https://github.com/facebookresearch/fastText/archive/v0.9.2.zip
$ unzip v0.9.2.zip
进入解压后的 fastText-0.9.2 目录并使用 make 工具自动完成编译工作。
$ cd fastText-0.9.2
$ make
使用 fastText 工具有两种方式,一种是在 fastText-0.9.2 目录下执行 ./fasttext
程序,另外一种是通过 Python 调用。这里我们使用第二种方式,需要注意使用 Python 调用,需要将 fasttext 与 Python 进行绑定。在 fastText-0.9.2 目录下执行 pip install .
即可完成绑定。(命令中的 Python 解释器要和将来要使用 fastText 的 IDE 开发工具所使用的的 Python 解释器一致)
$ pwd
/home/chenkc/fastText/fastText-0.9.2
$ pip install .
打开和命令行中拥有相同 Python 解释器的开发工具。如果成功执行下列代码并输出,则表示已经成功安装了 fastText 工具。
import fasttext
print(fasttext.FastText)
# <module 'fasttext.FastText' from '/home/chenkc/conda/lib/python3.7/site-packages/fasttext/FastText.py'>
本文分享自 AI机器学习与深度学习算法 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!