如何使用预先训练的单词向量创建gensim word2vec模型?
问题描述:
我使用分布式word2vec算法创建了词向量。现在我有单词和相应的向量。如何使用这些单词和向量来构建gensim word2vec模型?如何使用预先训练的单词向量创建gensim word2vec模型?
答
我不确定您是否使用gensim
或其他工具创建了word2vec模型,但是如果正确理解您的问题,则只需使用gensim加载word2vec模型。这是通过以下方式进行:
import gensim
w2v_file = codecs.open(WORD2VEC_PATH, encoding='utf-8')
model = gensim.models.KeyedVectors.load_word2vec_format(w2v_file, binary=True) # or binary=False if the model is not compressed
但是,如果你想要做的是培养word2vec使用纯gensim
这里从头模型(即从原始文本)是一个tutorial on how to train word2vec model using gensim。
谢谢,这正是我正在寻找的。 –
您能否提供示例w2v_file或帮助我生成该格式?我将这个单词和它的向量放在一个由空格和单词分隔的行中,并用行分隔。谢谢。 @sophros –
你有没有尝试过以下方法?'from gensim.models import word2vec model = word2vec.Word2Vec.load_word2vec_format('path/to/GoogleNews-vectors-negative300.bin',binary = False)' 重要的部分是'binary = False'。 – sophros