在 Word2Vec 中,如何将向量值转换回相关单词?

数据挖掘 word2vec
2022-02-23 06:24:50

为新手问题道歉:我刚刚下载了 GoogleNews word2vec bin 文件并使用 convertvec 将其转换为文本文件以查看向量值。我看到它们是介于 -1 和 1 之间的值。有人可以解释这些值如何映射回相关术语吗?

我最终想编写一些代码来获取这个 bin/text 文件,对其进行转换并将其加载到 MongoDB 数据库中以进行进一步操作。

1个回答

如果你在 Python 中使用Gensim,你可以创建一个包含单词和向量的字典。

from gensim.models import Word2Vec

# we create a Word2Vec model using a window size of 4 and word vectors dimensionality of 100
model_1 = Word2Vec(window=4,size=100,sg=1,min_count=1, workers = -1)
# prepare the model vocabulary
model_1.build_vocab(this_is_your_text_dataset)
# train the model
model_1.train(this_is_your_text_dataset,total_examples=model_1.corpus_count,epochs=model_1.iter)

#we create a dictionary with the word and its vector as the value
vectors_model = dict(zip(model_1.wv.index2word, model_1.wv.syn0))

以上将创建一个格式如下的字典:

{
"word_1": vector_1,
"word_2": vector_2,
"word_3": vector_3,
....
}

有了这个,您可以从单词到向量以及从向量到单词来回切换。