Text classification-FastText

1.Getting and preparing the data

每行包括:label,句子

>> head cooking.stackexchange.txt

__label__sauce __label__cheese How much does potato starch affect a cheese sauce recipe?__label__food-safety __label__acidity Dangerous pathogens capable of growing in acidic environments __label__cast-iron __label__stove How do I cover up the white spots on my cast iron stove?__label__restaurant Michelin Three Star Restaurant; but if the chef is not there__label__knife-skills__label__dicing Without knife skills, how can I quickly and accurately dice vegetables?

在训练之前将数据分为训练集和交叉验证集 4:1

>> wc cooking.stackexchange.txt

  15404  169582 1401900 cooking.stackexchange.txt

>> head -n 12404 cooking.stackexchange.txt > cooking.train

>> tail -n 3000 cooking.stackexchange.txt > cooking.valid

2.Our first classifier

训练分类器

>> ./fasttext supervised -input cooking.train -output model_cooking

Read 0M words

Number of words:  14543

Number of labels: 735

Progress: 100.0% words/sec/thread:  90012 lr:  0.000000 loss: 10.222594 ETA:  0h 0m

 -input 指示训练集 

-output定义在哪里存放model文件

训练的最后会生成 model.bin文件存放分类器


交互式测试分类器:

>> ./fasttext predict model_cooking.bin -

>> Why not put knives in the dishwasher?

__label__baking

>> Why not put knives in the dishwasher?

__label__food-safety

执行交叉验证:

>> ./fasttext test model_cooking.bin cooking.valid

N 3000

P@1 0.138

R@1 0.0595

Number of examples: 3000

precision:P@1

recall:R@1

每5次迭代计算一次precision和recall

>> ./fasttext test model_cooking.bin cooking.valid 5

N 3000

P@5 0.0677

R@5 0.146

Number of examples: 3000

3.Advanced readers: precision and recall

The precision is the number of correct labels among the labels predicted by fastText.

The recall is the number of labels that successfully were predicted, among all the real labels.

4.Making the model better

4.1 preprocessing the data

>>cat cooking.stackexchange.txt | sed -e "s/\([.\!?,'/()]\)/ \1 /g" | tr "[:upper:]" "[:lower:]" > cooking.preprocessed.txt

head -n 12404 cooking.preprocessed.txt > cooking.train

 tail -n 3000 cooking.preprocessed.txt > cooking.valid

使用预训练过的数据训练模型:

>> ./fasttext supervised -input cooking.train -output model_cooking

Read 0M words

Number of words: 8952

Number of labels: 735

Progress: 100.0% words/sec/thread: 101142   lr: 0.000000   loss: 11.018550   ETA: 0h 0m


>> ./fasttext test model_cooking.bin cooking.valid

N 3000

P@1 0.172

R@1 0.0744

Number of examples: 3000

4.2 more epochs and larger learning rate

默认情况下fastText 只进行5次迭代

使用 -epoch 自定义迭代次数

>> ./fasttext supervised -input cooking.train -output model_cooking -epoch 25

Read 0M words

Number of words: 8952

Number of labels: 735

Progress: 100.0% words/sec/thread: 92990   lr: 0.000000   loss: 7.257324   ETA: 0h 0m


>> ./fasttext test model_cooking.bin cooking.valid

N 3000

P@1 0.514

R@1 0.222

Number of examples: 3000

加快学习速度--改变learning rate

>> ./fasttext supervised -input cooking.train -output model_cooking -lr 1.0

Read 0M words

Number of words: 8952

Number of labels: 735

Progress: 100.0% words/sec/thread: 91682     lr: 0.000000     loss: 6.346271     ETA: 0h 0m


>> ./fasttext test model_cooking.bin cooking.valid

N 3000

P@1 0.579

R@1 0.25

Number of examples: 3000


4.3 word n-grams

使用bigrams训练模型

>> ./fasttext supervised -input cooking.train -output model_cooking -lr 1.0 -epoch 25 -wordNgrams 2

Read 0M words

Number of words: 8952

Number of labels: 735

Progress: 100.0% words/sec/thread: 93126    lr: 0.000000     loss: 3.139972     ETA: 0h 0m

>> ./fasttexttestmodel_cooking.bin cooking.valid

N 3000

P@1 0.61

R@1 0.264

Number of examples: 3000

提升模型准确率的几种方法:

preprocessing the data ;

changing the number of epochs (using the option-epoch, standard range[5 - 50]) ;

changing the learning rate (using the option-lr, standard range[0.1 - 1.0]) ;

using word n-grams (using the option-wordNgrams, standard range[1 - 5]).

5.Scaling things up

使用 hierarchical softmax可以让模型训练的更快 This can be done with the option -loss hs:

>> ./fasttext supervised -input cooking.train -output model_cooking -lr 1.0 -epoch 25 -wordNgrams 2 -bucket 200000 -dim 50 -loss hs

Read 0M words

Number of words:  8952

Number of labels: 735

Progress: 100.0% words/sec/thread: 2139399 lr:  0.000000 loss:  2.142308 ETA:  0h 0m

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,873评论 0 10
  • 婚外情似乎成了这个社会很正常的现象,基本上所有人都觉得这是一种不道德的行为,也有部分经历过的人说能够与那些有这种经...
    家家有999本难念的经阅读 857评论 0 1
  • 很久以前听过这本书,心里还想它写的是啥,直到看过后才感受到这部作品的厚重感,与其说它是奥斯卡·辛德勒的个人...
    允弃阅读 1,404评论 3 1
  • 在MVC模式中,M:模型,是用来做数据操作的;V:视图,是用来展示界面的;C:控制器,对视图中的控件的行为进行操作...
    FallPine阅读 311评论 0 0
  • 风总是吹着 吹得让人失了方向 雨总是下着 浸湿了人的衣裳 手边落满灰尘的那本书 藏的是谁的故事? “...
    一只萝卜呀阅读 631评论 2 5

友情链接更多精彩内容