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

时间:2019-11-05 13:55:28

标签: gensim word2vec

比方说, word2vec.model 是我训练有素的word2vec模型。当出现单词外单词( oov_word )时,我使用 compute_vec(oov_word)方法计算向量 vec 。现在,我想将 oov_word 及其对应的向量 vec 添加/添加到已经训练好的模型 word2vec.model

我已经检查了以下链接。但是他们没有回答我的问题。

Combining/adding vectors from different word2vec models

https://datascience.stackexchange.com/questions/49431/how-to-train-an-existing-word2vec-gensim-model-on-new-words

https://radimrehurek.com/gensim/models/keyedvectors.html#gensim.models.keyedvectors.BaseKeyedVectors.add

2 个答案:

答案 0 :(得分:0)

from gensim.models.keyedvectors import WordEmbeddingsKeyedVectors
vector_length = 100
kv = WordEmbeddingsKeyedVectors(vector_length)

# wordList - list of words
# vectorList - list of the vector corresponding to the words

kv.add(wordList, vectorList)

kv.most_similar(word1) # gives the list of words similar to word1

答案 1 :(得分:0)

对于 4.0 版:

w2v_model.wv.add_vectors(keys, vectors)

检查 doc