【问题标题】:How do I save a Gensim model while ensuring forwards compatibility?如何在确保向前兼容性的同时保存 Gensim 模型?
【发布时间】:2018-05-16 05:27:45
【问题描述】:

我在 Gensim Phrases 类上使用 save method 来存储模型以供将来使用,但是如果我更新我的 Gensim 版本,我会在重新加载该模型时遇到问题。例如,当我收到以下错误时在 Gensim 2.3.0 中加载一个在 2.2.0 中制作的模型:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<timed exec> in <module>()

~/Stuff/Sources/anaconda3/envs/nlp/lib/python3.6/site-packages/gensim/models/phrases.py in __init__(self, phrases_model)
    395         self.min_count = phrases_model.min_count
    396         self.delimiter = phrases_model.delimiter
--> 397         self.scoring = phrases_model.scoring
    398         self.phrasegrams = {}
    399         corpus = pseudocorpus(phrases_model.vocab, phrases_model.delimiter)

AttributeError: 'Phrases' object has no attribute 'scoring'

有没有更好的方法来确保前向兼容性?

【问题讨论】:

    标签: python machine-learning nlp data-science gensim


    【解决方案1】:

    我只使用过几次gensim,而且是新手,但从Change Logscoring attribute was introduced on a Phrases class in 2.3.0 来看。

    现在,从我在 github 问题中注意到的情况来看,在保存和加载模型时,维护人员正试图保持向后兼容性。看起来“缺少评分”属性问题是addressed in 3.1.0 - 请参阅“加载短语类时向后评分兼容性”评论和pull request 中的相关讨论。修复的想法基本上是将load()方法改进为handle missing attributes and implicitly replacing them with the defaults以避免加载失败。

    我认为在 2.3.0 中 gensim 有这个 generic SaveLoad class for pickling/unpickling models - 如您所见,它非常简单,这里没有特定于模型的逻辑。

    我不确定是否以及如何使模型在 2.2.0 和 2.3.0 之间保持兼容。我会在gensim issue tracker 开一个新问题。

    【讨论】:

    • 哦,很高兴知道。我更新到 3.1.0 但现在我有一个不同的兼容性错误: AttributeError: 'Phrases' object has no attribute 'common_terms' 所以如果 Gensim 确实打算保持向后兼容性,我会按照你的建议提交一个错误。干杯!
    • @Durand 当然。请将此讨论与 github 问题交叉引用 - 我想跟踪正在发生的事情。谢谢!
    • @Durand 我想你刚刚发现了一个真正的新问题——正如我所见,load() method does not handle the missing common_terms atrribute。在模型加载过程中,它应该作为scoring 属性处理。
    • 看起来这个问题有一些进展here所以我想我现在只需要等待。
    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2017-12-14
    • 2016-12-29
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多