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

doc2vec - Input Format for doc2vec training and infer_vector() in python

在 gensim 中,当我给一个字符串作为训练 doc2vec 模型的输入时,我得到这个错误:

TypeError('don\'t know how to handle uri %s' % repr(uri))

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

documents = TaggedLineDocument('myfile.txt')

myFile.txt 是否应该在每个文档或字符串的每一行中将标记作为列表的列表或单独的列表?

For eg - 我有 2 个文档。

文档 1:机器学习是计算机科学的一个子领域,是从模式识别研究发展而来的。

文档 2:Arthur Samuel 将机器学习定义为 "Field of study that gives computers the ability to learn"。

那么,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"]

当我在测试数据上 运行 它时,我想预测文档向量的句子格式应该是什么?应该像下面的案例 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"]

TaggedLineDocument 是一种便利 class,它期望其源文件(或类似文件的对象)是 space 分隔的标记,每行一个。 (也就是说,您在第一个问题中提到的 'Case 1'。)

但是你可以编写自己的可迭代对象作为 documents 语料库提供给 gensim Doc2Vec,只要这个语料库 (1) iterably-returns next() 对象,如 TaggedDocument,具有 wordstags 列表;和 (2) 可以迭代多次,因为多次通过 Doc2Vec 需要初始词汇调查和 iter 训练通过。

infer_vector() 方法采用标记列表,类似于单个 TaggedDocument 类对象的 words 属性。 (也就是说,您在第二个问题中称为 'Case 2'。)