【gensim.word2vec使用】

基于官方网站:https://radimrehurek.com/gensim/models/word2vec.html#gensim.models.word2vec

【示例】

训练模型

from gensim.test.utils import common_texts, get_tmpfile
from gensim.models import Word2Vec

model = Word2Vec(common_texts, size=100, window=5, min_count=1, workers=4)
model.save("word2vec.model")

line4:根据common_texts产生model。其中common_texts是gensim的测试数据。格式如下:

查看结果

训练好的wordvector放在model.mv的KeyedVector实例中。查看一个词的vector方法与使用字典类似:

vector = model.wv['computer']

vector就是词向量,ndarray类型:

“The reason for separating the trained vectors into KeyedVectors is that if you don’t need the full model state any more (don’t need to continue training), the state can discarded, resulting in a much smaller and faster object that can be mmapped for lightning fast loading and sharing the vectors”

把训练好的词向量存在KeyedVectors 中,是因为这样可以删除一些不需要的状态,减少空间。代价是不能使用KeyedVectors 中的词向量继续参与训练。但是可以使用KeyedVectors 中数据完成NLP的工作。KeyedVectors 的详情可以查阅:

 gensim.models.keyedvectors.

训练好模型之后,就可以直接使用model.mv,将model本身删除了:

word_vectors = model.wv
del model

gensim.models.word2vec.BrownCorpus(dirname)】

返回自带的测试数据集BrownCorpus的iterator

gensim.models.word2vec.LineSentence(sourcemax_sentence_length=10000limit=None)】

返回source指定的文件的iterator,一行对应一句,每一句需要先分好词,用空格分开

from gensim.test.utils import datapath
sentences = LineSentence(datapath('lee_background.cor'))
for sentence in sentences:
    pass

gensim.models.word2vec.PathLineSentences(sourcemax_sentence_length=10000limit=None)】

source指定文件夹,返回该路径下遍历所有文件的iterator。要求每个文件都可以用LineSentence遍历(需要注意:不会遍历到子文件夹中的文件)

gensim.models.word2vec.Word2Vec

训练词向量神经网络。

模型用model的save()/load()进行存储和读取

输入参数如下(先翻译过来,有些选项我也没用过):

sentences :可以是a list of list of tokens,也可以是硬盘放置训练数据的路径;

size (intoptional) :训练出的词向量的维度;

window (intoptional):Maximum distance between the current and predicted word within a sentence。是不是就是N-gram中的窗口大小?

min_count (intoptional):一个阈值。训练数据中出现频次小于此频次的词被忽略。用户过滤低频词汇;

workers (intoptional) :训练使用多线程吗?加快训练速度;

sg ({01}optional):使用不同模型训练。1 for skip-gram; otherwise CBOW.

hs ({01}optional):优化方法选择,和‘negative’选项组合使用。If 1, hierarchical softmax will be used for model training. If 0, and negative is non-zero, negative sampling will be used.

negative (intoptional):negative sampling优化参数。If > 0, negative sampling will be used, the int for negative specifies how many “noise words” should be drawn (usually between 5-20). If set to 0, no negative sampling is used.

ns_exponent (floatoptional):negative sampling优化参数。详见文章顶部的官网链接;

cbow_mean ({01}optional):CBOW参数。

alpha (floatoptional):初始学习速率;

min_alpha (floatoptional):学习速率下限。训练进行过程中学习速率会线性下降;

seed (intoptional):初始词向量的值随机产生,这里指定随机数的种子;产生方法见官网链接;

max_vocab_size (intoptional):训练过程词表建设过程中RAM的最大值。1千万词的词表占用1GB的RAM。训练过程中词表占用的RAM超过此参数后,就将频次少的词删除。如果设置为None,则不设限;

max_final_vocab (intoptional):指定词表大小。我的理解,本参数指定vocab词量;“max_vocab_size ”指定所占空间大小,不知理解是否正确。

sample (floatoptional):用于对高频词汇降采样。思考:什么场景降采样?

hashfxn (functionoptional):用于初始化神经网络权重值;

iter (intoptional):指定训练数据迭代训练多少遍;

trim_rule (functionoptional):该参数为函数,用于判断某个词是应该留在词表中,还是该被删除。函数输入参数固定为: (word, count, min_count);

sorted_vocab ({01}optional):1:按照频次从高到低排序;

batch_words (intoptional) :多线程相关。传递给一个worker的example数量;

compute_loss (booloptional) :计算loss。这个值可以用函数获取get_latest_training_loss()

callbacks (iterable of CallbackAny2Vec, optional):回调函数

另外还有一系列可调用的函数,暂略

gensim.models.word2vec.Word2VecTrainables(vector_size=100seed=1hashfxn=<built-in function hash>)】

表示词向量的训练网络

gensim.models.word2vec.Word2VecVocab

词表对象

gensim.models.word2vec.score_cbow_pair(modelwordl1)】

计算一对词语在CBOW训练后的网络上的打分

gensim.models.word2vec.score_sg_pair(modelwordword2)】

计算一对词语在Skip-gram训练后的网络上的打分

gensim.models.word2vec.train_cbow_pair

用CBOW算法,使用一个词和它的上下文训练传入的model

gensim.models.word2vec.train_sg_pair

用Skip-gram算法,使用一个词和它的上下文训练传入的model

猜你喜欢

转载自blog.csdn.net/tong_xin2010/article/details/81199446
今日推荐