【问题标题】:KeyError in Doc2Vec model even when min_count set to 1 during training即使在训练期间 min_count 设置为 1,Doc2Vec 模型中的 KeyError
【发布时间】:2018-03-23 22:40:42
【问题描述】:

我正在使用具有大约 100 万个标题的语料库的 doc2vec。为了训练语料库,我使用以下代码:

model = gensim.models.Doc2Vec(min_count=1, window=10, size=300, workers=4)
model.build_vocab(corpus)
for epoch in range(10):
    model.train(corpus)

一切似乎都训练得很好,我可以使用titles.most_similar来推断一个向量。

但是,当我尝试使用向量时遇到了一个问题。似乎最终模型中缺少一些文档!即:

model.docvecs['SENT_157000']

密钥错误:'SENT_157000'

我检查了 gensim 论坛和 stackoverflow,我能找到的唯一建议是确保 min_count = 1。我这样做了,但我仍然遇到这个问题。

【问题讨论】:

    标签: python gensim word2vec


    【解决方案1】:

    gensimDoc2Vec documentation,输入到Doc2Vec 应该是LabeledSentence 对象的迭代器。

    您的corpus 变量需要按如下方式构造:

    class LabeledLineSentence(object):
        def __init__(self, filename):
            self.filename = filename
    
        def __iter__(self):
            for uid, line in enumerate(open(self.filename)):
                yield LabeledSentence(words=line.split(), labels=['SENT_%s' % uid])
    
    
    corpus = LabeledLineSentence(filename)
    

    紧随其后

    model.train(corpus)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 2019-11-12
      • 2018-07-31
      • 2018-12-10
      • 2016-08-17
      • 1970-01-01
      相关资源
      最近更新 更多