在Gensim Word2Vec模型中匹配单词和向量

帕特里克:

我已经让gensim Word2Vec实现为我计算了一些单词嵌入。据我所知,一切进展都非常好。现在,我对创建的单词向量进行聚类,以期获得一些语义分组。

下一步,我想看看每个群集中包含的单词(而不是向量)。即,如果我有嵌入矢量[x, y, z],我想找出该矢量代表哪个实际词。我可以通过调用来获取单词/词汇项目,model.vocab并通过来获得单词向量model.syn0但是我找不到一个明确匹配的位置。

这比我预期的要复杂,我觉得我可能会错过明显的方法。任何帮助表示赞赏!

问题:

将字词匹配到由...创建的嵌入矢量Word2Vec ()-我该怎么做?

我的方法:

创建模型(下面的代码*)之后,我现在想将分配给每个单词的索引(在build_vocab()阶段中)与输出为的矢量矩阵匹配model.syn0从而

for i in range (0, newmod.syn0.shape[0]): #iterate over all words in model
    print i
    word= [k for k in newmod.vocab if newmod.vocab[k].__dict__['index']==i] #get the word out of the internal dicationary by its index
    wordvector= newmod.syn0[i] #get the vector with the corresponding index
    print wordvector == newmod[word] #testing: compare result of looking up the word in the model -- this prints True
  • 有没有更好的方法来做到这一点,例如通过将向量输入模型以匹配单词?

  • 这甚至能给我正确的结果吗?

*我创建字向量的代码:

model = Word2Vec(size=1000, min_count=5, workers=4, sg=1)
        
model.build_vocab(sentencefeeder(folderlist)) #sentencefeeder puts out sentences as lists of strings

model.save("newmodel")

我发现这个问题很相似,但尚未真正得到解答。

帕特里克:

因此,我找到了一种简单的方法来执行此操作,nmodel模型的名称在哪里

#zip the two lists containing vectors and words
zipped = zip(nmodel.wv.index2word, nmodel.wv.syn0)

#the resulting list contains `(word, wordvector)` tuples. We can extract the entry for any `word` or `vector` (replace with the word/vector you're looking for) using a list comprehension:
wordresult = [i for i in zipped if i[0] == word]
vecresult = [i for i in zipped if i[1] == vector]

这基于gensim代码对于gensim的旧版本,您可能需要wv在模型之后删除

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Gensim单词嵌入(Word2Vec和FastText)模型中的alpha值?

如何使用预先训练的单词向量创建gensim word2vec模型?

如何手动将单词和向量添加到Word2vec gensim?

如何在Gensim Word2Vec中手动更改单词的向量尺寸

Python Gensim从向量创建Word2Vec模型(在ndarray中)

训练 gensim word2vec 模型后单词不在词汇表中,为什么?

使用 gensim word2vec 沿输出或输入向量查找单词相似性?

Gensim Word2Vec从预训练模型中选择次要词向量集

gensim word2vec访问输入/输出向量

Gensim的word2vec返回尴尬的向量

无法加载已保存的 gensim word2vec 模型

使用Gensim减少Google的Word2Vec模型

在Gensim中加载Word2Vec模型时出错

如何加快Gensim Word2vec模型的加载时间?

使用 Gensim 训练 Word2vec 模型

Gensim word2vec在预定义的词典和单词索引数据上

结合使用Gensim的Word2Vec和自定义单词-上下文对

如何从gensim Word2Vec嵌入向量中句子嵌入?

如何使用gensim的word2vec模型和python计算句子相似度

复制 gensim word2vec 的嵌入

gensim word2vec的培训时间

在word2vec或Glove中添加其他单词(可能使用gensim)

gensim word2vec:查找词汇中的单词数

[Word2Vec] [gensim]使用参数min_count处理词汇中的遗漏单词

Word2Vec和Gensim参数等效

Gensim word2vec 和大量文本

Python:Gensim Word2vec模型类中的“大小”参数是什么

为什么要在gensim word2vec中创建多个模型文件?

偶尔,如何使用在gensim中创建的自己的word2vec模型?