簡體   English   中英

NLTK 保存訓練好的 Brill 模型

[英]NLTK saving trained Brill's model

我正在使用 NLTK 中提供的py-crfsuite訓練 Brill 的 POS 標記器。 但是,當我嘗試保存經過訓練的模型時,出現以下錯誤:

crf_tagger = CRFTagger()    
crf_tagger.train(train_sents, 'model_trained.crf.tagger')
templates = nltk.tag.brill.nltkdemo18()
trainer = nltk.tag.brill_trainer.BrillTaggerTrainer(crf_tagger, templates)
bt = trainer.train(train_sents, max_rules=10)

file_writing = file('trained_brill_tagger.yaml', 'w')
yaml.dump(bt, file_writing)

#even pickle fails
file_w = open('trained_brills.pickle', 'wb')
pickle.dump(bt, file_w)
file_w.close()

文件“stringsource”,第 2 行,在 pycrfsuite._pycrfsuite.Tagger 中。 reduce_cython TypeError:self.c_tagger 無法轉換為 Python 對象進行酸洗

我曾嘗試使用pickledillyaml但錯誤似乎仍然存在。 有沒有辦法解決這個問題。 這是因為使用 CRF 標記器作為基線嗎? 謝謝。

我意識到問題出在CRFTagger模塊中。 如果我對 Brill 使用不同的初始標記器,則不會產生錯誤並保存模型。

trainer = nltk.tag.brill_trainer.BrillTaggerTrainer(baseline_tagger, templates)

當baseline_tagger 是CRFTagger()對象時,我無法保存經過訓練的模型。 出於某種原因,使用像NgramTagger這樣的NgramTagger可以解決這個問題。

這是一個如何在 NLTK v3.2.5 中訓練nltk.tag.brill_trainer.BrillTaggerTrainer的示例

from nltk.corpus import treebank

from nltk.tag import BrillTaggerTrainer, RegexpTagger, UnigramTagger
from nltk.tbl.demo import REGEXP_TAGGER, _demo_prepare_data, _demo_prepare_data
from nltk.tag.brill import describe_template_sets, brill24

baseline_backoff_tagger = REGEXP_TAGGER
templates = brill24()
tagged_data = treebank.tagged_sents()
train=0.8
trace=3
num_sents=1000
randomize=False
separate_baseline_data=False

(training_data, baseline_data, gold_data, testing_data) = \
   _demo_prepare_data(tagged_data, train, num_sents, randomize, separate_baseline_data)

baseline_tagger = UnigramTagger(baseline_data, backoff=baseline_backoff_tagger)

# creating a Brill tagger
trainer = BrillTaggerTrainer(baseline_tagger, templates, trace, ruleformat="str")

然后為了拯救訓練師,只需pickle

import pickle
with open('brill-demo.pkl', 'wb') as fout:
    pickle.dump(trainer, fout)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM