glove有两种已经实现的包
1.Stanford官方glove:https://github.com/stanfordnlp/GloVe
2.简单版本使用:https://github.com/maciejkula/glove-python
本文针对的是第二种glove-python包
glove这个库,在Windows下对python3的支持并不友好,之前费了很大劲,还是各种安装错误,今天看到大佬在GitHub回复了解决方案,在pipy上传了一个新的包,记录一下,也帮助刚刚接触的小伙伴避坑
终极解决方法,无视编译环境
pip install glove-python-binary
.
附上简易教程,引用出处
极简使用︱Glove-python词向量训练与使用_素质云笔记/Recorder...-CSDN博客_python gloveblog.csdn.netimport argparse
import pprint
import gensim
from glove import Glove
from glove import Corpus
#准备数据集
sentense = [['你','是','谁'],['我','是','中国人']]
corpus_model = Corpus()
corpus_model.fit(sentense, window=10)
#corpus_model.save('corpus.model')
print('Dict size: %s' % len(corpus_model.dictionary))
print('Collocations: %s' % corpus_model.matrix.nnz)
#训练
glove = Glove(no_components=100, learning_rate=0.05)
glove.fit(corpus_model.matrix, epochs=10,
no_threads=1, verbose=True)
glove.add_dictionary(corpus_model.dictionary)
#模型保存
glove.save('glove.model')
glove = Glove.load('glove.model')
#语料保存
corpus_model.save('corpus.model')
corpus_model = Corpus.load('corpus.model')
#求相似词
glove.most_similar('我', number=10)