我正在使用gensim Doc2Vec 模型来生成我的特征向量。这是我正在使用的代码(我已经在代码中解释了我的问题):
cores = multiprocessing.cpu_count()
# creating a list of tagged documents
training_docs = []
# all_docs: a list of 53 strings which are my documents and are very long (not just a couple of sentences)
for index, doc in enumerate(all_docs):
# 'doc' is in unicode format and I have already preprocessed it
training_docs.append(TaggedDocument(doc.split(), str(index+1)))
# at this point, I have 53 strings in my 'training_docs' list
model = Doc2Vec(training_docs, size=400, window=8, min_count=1, workers=cores)
# now that I print the vectors, I only have 10 vectors while I should have 53 vectors for the 53 documents that I have in my training_docs list.
print(len(model.docvecs))
# output: 10
我只是想知道我是否犯了错误,或者我是否应该设置其他参数?
更新:我正在使用 TaggedDocument 中的 标记 参数,当我将它改为文本和数字的混合时,如: Doc1,Doc2,... 我看到生成的向量计数不同的数字,但我仍然没有相同的数字特征向量如预期的那样。
答案 0 :(得分:1)
查看它在您的语料库中发现的实际标签:
(2, 3)
你看到一种模式吗?
每个文档的print(model.docvecs.offset2doctag)
属性应该是标记列表,而不是单个标记。如果您提供一个简单的整数字符串,它会将其视为一个数字列表,因此只能学习标记tags
,'0'
,...,{{1 }}
您可以将'1'
替换为'9'
并获得您期望的行为。
但是,由于你的文档ID只是升序整数,你也可以使用普通的Python int作为你的doctags。这将节省一些内存,避免从string-tag到array-slot(int)创建查找字典。为此,请将str(index+1)
替换为[str(index+1)]
。 (这将启动str(index+1)
中的doc-ID - 这是一个非常多的Pythonic,并且还避免在包含训练向量的原始数组中浪费未使用的[index]
位置。)