首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于特定的训练模型,Doc2Vec.infer_vector每次都会给出不同的结果

对于特定的训练模型,Doc2Vec.infer_vector每次都会给出不同的结果
EN

Stack Overflow用户
提问于 2018-01-21 08:31:49
回答 2查看 4.3K关注 0票数 12

我正在尝试遵循这里提到的官方Doc2Vec Gensim教程- https://github.com/RaRe-Technologies/gensim/blob/develop/docs/notebooks/doc2vec-lee.ipynb

我修改了第10行的代码,以确定给定查询的最佳匹配文档,每次运行时,我都会得到一个完全不同的结果集。我在笔记本的第10行中的新代码是:

inferred_vector = model.infer_vector(['only', 'you', 'can', 'prevent', 'forest', 'fires']) sims = model.docvecs.most_similar([inferred_vector], topn=len(model.docvecs)) rank = [docid for docid, sim in sims] print(rank)

每次我运行这段代码时,我都会得到与这个查询匹配的不同文档集:“只有你才能防止森林火灾”。两者的区别是明显的,似乎并不匹配。

Doc2Vec不是查询和信息提取的合适匹配吗?或者有bug吗?

EN

回答 2

Stack Overflow用户

发布于 2018-01-22 02:13:15

仔细查看代码,在infer_vector中,您使用的算法部分是不确定的。词向量的初始化是确定性的-参见seeded_vector的代码,但当我们进一步观察,即随机采样单词时,负采样(每次迭代仅更新词向量的样本)可能会导致不确定的输出(感谢@gojomo)。

代码语言:javascript
复制
    def seeded_vector(self, seed_string):
        """Create one 'random' vector (but deterministic by seed_string)"""
        # Note: built-in hash() may vary by Python version or even (in Py3.x) per launch
        once = random.RandomState(self.hashfxn(seed_string) & 0xffffffff)
        return (once.rand(self.vector_size) - 0.5) / self.vector_size
票数 15
EN

Stack Overflow用户

发布于 2019-11-21 12:10:08

设置negative=0以避免随机化:

代码语言:javascript
复制
import numpy as np
from gensim.models.doc2vec import Doc2Vec, TaggedDocument

documents = [list('asdf'), list('asfasf')]
documents = [TaggedDocument(doc, [i]) for i, doc in enumerate(documents)]
model = Doc2Vec(documents, vector_size=20,  window=5, min_count=1,  negative=0, workers=6, epochs=10) 
a = list('test sample')
b = list('testtesttest')
for s in (a, b):
    v1 = model.infer_vector(s)
    for i in range(100):
        v2 = model.infer_vector(s)
        assert np.all(v1 == v2), "Failed on %s" % (''.join(s))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48362530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档