Py之fasttext:fasttext的简介(类似CBoW模型)、安装、案例应用之详细攻略
目录
| 背景 | |
| 应用 | fasttext是Facebook开发的一款快速文本分类器。fasttext是一个用于高效学习单词表示和句子分类的库。 |
| 简介 | fasttext原理有点类似于Word2Vec中CBoW模型,fasttext在是实现分类任务中采用了Hierarchical Softmax,突出了两个特性: (1)、Word2Vec是在输入层得到词向量,输出层对应的 Herarchical Softmax 也会生成一系列的向量,但最终都不会使用。而fastText的输出层对应是分类的label,目的是遍历分类树的所有叶节点,找到概率最大的label。 (2)、Word2Vec的输入是上下文窗口内的词,而fasttext对应的整个文本,包括周边词和 N-Gram的内容。 |
| 特点 | (1)、有监督性:fasttext是利用带有监督标记的文本分类数据完成训练。 (2)、速度快:fasttext的网络结构与CBOW基本一致,其最大优势在于预测速度。在一些分类数据集上,fasttext通常可以把要耗时几小时甚至几天的模型训练大幅压缩到几秒钟。 |
Github官网:
GitHub - facebookresearch/fastText: Library for fast text representation and classification.
fasttext原理有点类似于Word2Vec中CBoW模型,fastText在是实现分类任务中采用了Hierarchical Softmax,突出了两个特性:
(1)、Word2Vec是在输入层得到词向量,输出层对应的 Herarchical Softmax 也会生成一系列的向量,但最终都不会使用。而fastText的输出层对应是分类的label,目的是遍历分类树的所有叶节点,找到概率最大的label。
(2)、Word2Vec的输入是上下文窗口内的词,而fasttext对应的整个文本,包括周边词和 N-Gram的内容。
(1)、Word representation learning
(2)、Obtaining word vectors for out-of-vocabulary words
(3)、Text classification
pip install fasttext
案例应用:https://pypi.org/project/fasttext/
fasttext:文本分类、训练词向量、词向量迁移_あずにゃん的博客-CSDN博客_pytorch word2vec
- import fasttext
-
- # Skipgram model :
- model = fasttext.train_unsupervised('data.txt', model='skipgram')
-
- # or, cbow model :
- model = fasttext.train_unsupervised('data.txt', model='cbow')
-
-
- print(model.words) # list of words in dictionary
- print(model['king']) # get the vector of the word 'king'
-
-
- model.save_model("model_filename.bin")
- model = fasttext.load_model("model_filename.bin")
-
-
-
- import fasttext
-
- model = fasttext.train_supervised('data.train.txt')
-
- print(model.words)
- print(model.labels)
-
-
- def print_results(N, p, r):
- print("N\t" + str(N))
- print("P@{}\t{:.3f}".format(1, p))
- print("R@{}\t{:.3f}".format(1, r))
-
- print_results(*model.test('test.txt'))
-
- model.predict("Which baking dish is best to bake a banana bread ?")
- model.predict("Which baking dish is best to bake a banana bread ?", k=3)
- model.predict(["Which baking dish is best to bake a banana bread ?", "Why not put knives in the dishwasher?"], k=3)
-
-