Gensim Doc2Vec 模型只生成有限数量的向量

Gensim Doc2Vec model only generates a limited number of vectors

我正在使用 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

我只是想知道我是否做错了,或者是否还有其他我应该设置的参数?

UPDATE: I was playing with the tags parameter in TaggedDocument, and when I changed it to a mixture of text and numbers like: Doc1, Doc2, ... I see a different number for the count of generated vectors, but still I do not have the same number of feature vectors as expected.

查看它在您的语料库中发现的实际标签:

print(model.docvecs.offset2doctag)

你看到规律了吗?

每个文档的 tags 属性 应该是一个 标签列表 ,而不是单个标签。如果您提供一个简单的整数字符串,它会将其视为数字列表,因此只会学习标签 '0''1'、...、'9'.

您可以将 str(index+1) 替换为 [str(index+1)] 并获得您期望的行为。

但是,由于您的文档 ID 只是递增的整数,您也可以只使用普通的 Python 整数作为您的文档标签。这将节省一些内存,避免创建从字符串标签到数组槽(int)的查找字典。为此,请将 str(index+1) 替换为 [index]。 (这从 0 开始了 doc-IDs——这有点多 Pythonic,并且还避免了在保存训练向量的原始数组中浪费未使用的 0 位置。)