Gensim Doc2Vec - 将语料库句子传递给 Doc2Vec 函数



我使用 MySentences 类从目录中的所有文件中提取句子,并使用此句子来训练 word2vec 模型。我的数据集未标记。

class MySentences(object):
    def __init__(self, dirname):
        self.dirname = dirname
    def __iter__(self):
        for fname in os.listdir(self.dirname):
            for line in open(os.path.join(self.dirname, fname)):
                yield line.split()
sentences = MySentences('sentences')
model = gensim.models.Word2Vec(sentences)

现在我想使用该类来创建一个 doc2vec 模型。我阅读了Doc2Vec参考页面。 Doc2Vec()函数获取句子作为参数,但它不接受上述句子变量并返回错误:

AttributeError: 'list' object has no attribute 'words'

问题出在哪里?该参数的正确类型是什么?

更新:

我认为,未标记的数据是问题所在。似乎doc2vec需要标记数据。

没有理由使用额外的类来解决问题。在库的新更新中,TaggedLineDocument添加了一个新函数来将句子转换为向量。

sentences = TaggedLineDocument(INPUT_FILE)

然后,训练模型

model = Doc2Vec(alpha=0.025, min_alpha=0.025)
model.build_vocab(sentences)
for epoch in range(10):
    model.train(sentences)
    model.alpha -= 0.002
    model.min_alpha = model.alpha
    print epoch

与word2vec不同,doc2vec要求每个火车条目都标有唯一的id。这是必需的,因为稍后当它预测相似性时,其结果将是doc ids(火车条目的唯一ID),就像单词是word2vec的预测一样。

这是我的一段代码,可以完成您想要实现的目标

 class DynamicCorpus(object):
 def __iter__(self):
     with open(csf_file) as fp:
         for line in fp:
             splt = line.split(':')
             text = splt[2].replace('n', '')
             id = splt[0]
             yield TaggedDocument(text.split(), [id])

我的 CSV 文件有格式ID:文本

稍后您可以将语料库馈送到模型

coprus = DynamicCorpus()
d2v = doc2vec.Doc2Vec(min_count=15,
                      window=10,
                      vector_size=300,
                      workers=15,
                      alpha=0.025,
                      min_alpha=0.00025,
                      dm=1)
d2v.build_vocab(corpus)
for epoch in range(training_iterations):
    d2v.train(corpus, total_examples=d2v.corpus_count, epochs=d2v.iter)
    d2v.alpha -= 0.0002
    d2v.min_alpha = d2v.alpha

最新更新