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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容

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