Python:pickle.load __init__错误

时间:2017-07-19 15:15:32

标签: python python-2.7 nltk pickle hidden-markov-models

我已经训练了一个HMM模型,将标点符号添加到阿拉伯语文本中,我想保存它,以便每次我在模型中输入文本进行标记时不重复训练阶段。我使用pickle来完成这些任务。在教程中。我确实喜欢他们,但它失败并给我这些错误!。

Traceback (most recent call last):
  File "C:\Python27\file_pun_tag.py", line 205, in <module>
    hmm_tagger("test_file.txt")
  File "C:\Python27\file_pun_tag.py", line 179, in hmm_tagger
    hmm = pickle.load(saved_model)
  File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 1133, in load_reduce
    value = func(*args)
TypeError: __init__() takes at least 3 arguments (2 given)

我尝试了几种解决方案,但没有一个与我合作...... 这是我保存模型的代码。它可以正常保存模型并创建“hmm.pickle”:

file = codecs.open("train_sents_hmm.txt", "r", "utf_8")
train_sents = file.readlines()
labelled_sequences, tag_set, symbols = load_pun(train_sents)
trainer = nltk.HiddenMarkovModelTrainer (tag_set, symbols)
hmm = trainer.train_supervised (labelled_sequences, estimator=lambda fd, bins: LidstoneProbDist(fd, 0.1, bins))

# save object
save_model = open("hmm.pickle", "wb")
pickle.dump(hmm, save_model, -1)
save_model.close()

以下是我保存后尝试加载模型时的代码,这里它给出了错误:

saved_model = open("hmm.pickle", "rb")
hmm = pickle.load(saved_model)
saved_model.close()

0 个答案:

没有答案