doc2vec - python中doc2vec training和infer_vector()的输入格式

问题描述 投票:2回答:1

在gensim中,当我提供一个字符串作为训练doc2vec模型的输入时,我收到此错误:

TypeError('不知道如何处理uri%s'%repr(uri))

我提到了这个问题Doc2vec : TaggedLineDocument(),但仍然对输入格式有疑问。

documents = TaggedLineDocument('myfile.txt')

myFile.txt是否应该将令牌作为列表列表,或者每个文档或字符串的每行中都有单独的列表?

For eg - 我有2份文件。

Doc 1:机器学习是从模式识别研究演变而来的计算机科学的一个子领域。

Doc 2:Arthur Samuel将机器学习定义为“让计算机具备学习能力的研究领域”。

那么,myFile.txt应该是什么样的?

案例1:每行中每个文档的简单文本

机器学习是从模式识别研究演变而来的计算机科学的一个子领域

Arthur Samuel将机器学习定义为一个让计算机具备学习能力的研究领域

案例2:列表中包含每个文档的标记

[ ["Machine", "learning", "is", "a", "subfield", "of", "computer", "science", "that", "evolved", "from", "the", "study", "of", "pattern", "recognition"]

["Arthur", "Samuel", "defined", "machine", "learning", "as", "a", "Field", "of", "study", "that", "gives", "computers" ,"the", "ability", "to", "learn"] ]

案例3:单独行中每个文档的标记列表

["Machine", "learning", "is", "a", "subfield", "of", "computer", "science", "that", "evolved", "from", "the", "study", "of", "pattern", "recognition"]

["Arthur", "Samuel", "defined", "machine", "learning", "as", "a", "Field", "of", "study", "that", "gives", "computers" ,"the", "ability", "to", "learn"]

当我在测试数据上运行它时,我想要预测doc向量的句子的格式应该是什么?它应该像下面的案例1或案例2还是其他什么?

model.infer_vector(testSentence, alpha=start_alpha, steps=infer_epoch)

testSentence应该是:

案例1:字符串

testSentence = "Machine learning is an evolving field"

案例2:令牌列表

testSentence = ["Machine", "learning", "is", "an", "evolving", "field"]
python gensim word2vec doc2vec
1个回答
5
投票

TaggedLineDocument是一个便利类,它希望它的源文件(或类文件对象)是以空格分隔的标记,每行一个。 (也就是说,你在第一个问题中称为“案例1”。)

但是你可以编写你自己的可迭代对象来提供给gensim Doc2Vec作为documents语料库,只要这个语料库(1)可迭代地返回next()对象,像TaggedDocument一样,有wordstags列表;并且(2)可以多次迭代,因为Doc2Vec需要进行初始词汇调查和iter训练传球的多次传球。

infer_vector()方法采用令牌列表,类似于单个words类对象的TaggedDocument属性。 (也就是说,你在第二个问题中称为“案例2”。)

© www.soinside.com 2019 - 2024. All rights reserved.