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

标签 python text-mining gensim word2vec 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

关于python - Gensim Doc2Vec - 将语料库句子传递给 Doc2Vec 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38245739/

相关文章:

python - Python 子序列运算符如何绕过索引错误?

python - 通过 Python 将数据合并到 Excel 中的 1 个工作表中

python - TFIDFVectorizer Pipeline 上具有不同 ngram 范围的 Word 和 Char ngram

python - 如何使用 tf-idf 对新文档进行分类?

python - gensim 中的 get_document_topics 和 get_term_topics

python - Word2Vec 比较来自不同模型的不同大小的向量

python - OS X 蓝牙编程

python - 使用正则表达式获取列表的第一个元素

r - "RTextTools"create_matrix 出错

python - gensim - Word2vec 继续训练现有模型 - AttributeError : 'Word2Vec' object has no attribute 'compute_loss'