NLTK保存训练有素的布里尔的国防部

2024-05-29 03:03:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用NLTK中提供的py crfsuite培训Brill的POS tagger。但是,当我试图保存一个经过训练的模型时,我得到了以下错误

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()

File "stringsource", line 2, in pycrfsuite._pycrfsuite.Tagger.reduce_cython TypeError: self.c_tagger cannot be converted to a Python object for pickling

我试过用泡菜,莳萝和山药,但错误似乎仍然存在。有什么解决办法吗。这是因为使用CRF标签作为基线吗?谢谢您。在


Tags: tag错误trainpickletaggertemplatesfilebt
2条回答

下面是一个如何在nltkv3.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

^{pr2}$

我意识到问题出在CRFTagger模块中。如果我在Brill's中使用不同的初始标记器,则不会产生错误并保存模型。在

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

当baseline_tagger是CRFTagger()对象时,我无法保存经过训练的模型。使用NgramTagger之类的东西出于某种原因解决了这个问题。在

相关问题 更多 >

    热门问题