Skip to main content
 首页 » 编程设计

python之为什么 gensim Doc2Vec 会为同一个句子提供不同的向量

2024年10月17日8zdz8207

我正在使用 from gensim.models.doc2vec import Doc2Vec 训练两个相同的句子(文档),当检查每个句子的向量时,它们完全不同。神经网络是否对每个句子进行不同的随机初始化?

# imports 
from gensim.models.doc2vec import LabeledSentence 
from gensim.models.doc2vec import Doc2Vec 
from gensim import utils 
 
# Document iteration class (turns many documents in to sentences 
# each document being once sentence) 
class LabeledDocs(object): 
    def __init__(self, sources): 
        self.sources = sources 
        flipped = {} 
        # make sure that keys are unique 
        for key, value in sources.items(): 
            if value not in flipped: 
                flipped[value] = [key] 
            else: 
                raise Exception('Non-unique prefix encountered') 
 
    def __iter__(self): 
        for source, prefix in self.sources.items(): 
            with utils.smart_open(source) as fin: 
                # print fin.read().strip(r"\n") 
                yield LabeledSentence(utils.to_unicode(fin.read()).split(), 
                                      [prefix]) 
 
    def to_array(self): 
        self.sentences = [] 
        for source, prefix in self.sources.items(): 
            with utils.smart_open(source) as fin: 
                #print fin, fin.read() 
                self.sentences.append( 
                    LabeledSentence(utils.to_unicode(fin.read()).split(), 
                                    [prefix])) 
        return self.sentences 
 
# play and play3 are names of identical documents (diff gives nothing) 
inp = LabeledDocs({"play":"play", "play3":"play3"}) 
model = Doc2Vec(size=20, window=8, min_count=2, workers=1, alpha=0.025, 
                min_alpha=0.025, batch_words=1) 
model.build_vocab(inp.to_array()) 
for epoch in range(10): 
    model.train(inp) 
 
# post to this model.docvecs["play"] is very different from 
# model.docvecs["play3"] 

这是为什么? play 和 play3 都包含:

foot ball is a sport 
played with a ball where 
teams of 11 each try to 
score on different goals 
and play with the ball 

请您参考如下方法:

是,每个句子向量的初始化都不一样。

特别是在 reset_weights方法。随机初始化句子向量的代码是这样的:

for i in xrange(length): 
    # construct deterministic seed from index AND model seed 
    seed = "%d %s" % (model.seed, self.index_to_doctag(i)) 
    self.doctag_syn0[i] = model.seeded_vector(seed) 

在这里你可以看到每个句子向量都是使用模型的随机种子和句子的标签初始化的。因此,在您的示例中 play 和 play3 产生不同的向量是有道理的。

但是,如果您正确训练模型,我希望两个向量最终彼此非常接近。