【发布时间】:2019-02-06 16:52:26
【问题描述】:
我正在使用 Python 3.5 进行研究。我想使用 Glove 词嵌入。在 glove.fit 之后如何保存和加载我的 Glove 模型? 我是这样编码的
glove.fit(corpus.matrix,epochs=1,no_threads=4,verbose=True)
glove.save('glove.model')
【问题讨论】:
我正在使用 Python 3.5 进行研究。我想使用 Glove 词嵌入。在 glove.fit 之后如何保存和加载我的 Glove 模型? 我是这样编码的
glove.fit(corpus.matrix,epochs=1,no_threads=4,verbose=True)
glove.save('glove.model')
【问题讨论】:
from gensim.models import KeyedVectors
# load the Stanford GloVe model
model = KeyedVectors.load_word2vec_format(filename, binary=False)
如果您的模型包含在变量“模型”中
你可以这样保存模型:
model.save('model.bin')
您可以像这样加载保存的模型:
new_model = KeyedVectors.load('model.bin')
您现在可以使用加载的模型:
result = new_model.most_similar(positive=['woman', 'king'], negative=['man'], topn=1)
【讨论】:
检查这个here。
现在在对数据进行训练后,使用它:
from gensim.scripts.glove2word2vec import glove2word2vec
glove2word2vec(glove_input_file=file, word2vec_output_file="gensim_glove_vectors.txt")
from gensim.models.keyedvectors import KeyedVectors
model = KeyedVectors.load_word2vec_format("gensim_glove_vectors.txt", binary=False)
之后,您可以像使用 gensim 模型一样使用它。例如,
print("Similarity between {} and {} is {}".format(word1,word2,model.wv.similarity(word1, word2)))
print("Most similar words to {} are :{}\n".format(word1,model.most_similar(positive=[word1],topn=10)))
【讨论】: