1、Word2Vec
Google 团队的 Word2Vec,其主要包含两个模型:跳字模型(Skip-Gram)和连续词袋模型(Continuous Bag of Words,简称
CBOW),以及两种高效训练的方法:负采样(Negative Sampling)和层序 Softmax(Hierarchical Softmax)。
之前写过一篇博客,小结了一下CS224n里Manning老师所讲的那幅经典的Skip-gram图。
欢迎点击:NLP基础笔记5——词向量
这些天发现一篇“The Illustrated Word2vec”的博客文章,值得一看,传送门:The Illustrated Word2vec
中文翻译版:https://mp.weixin.qq.com/s/NMngfR7EWk-pa6c4_FY9Yw
Word2vec原理就看如下这两篇文章,通俗易懂
https://zhuanlan.zhihu.com/p/35500923
https://zhuanlan.zhihu.com/p/58425003
2、达观杯实战
gensim.models.Word2Vec
model = gensim.models.Word2Vec(sentences=sentences, size=vector_size,
window=5, min_count=5,
workers=8, sg=0, iter=5)
函数中的参数如下所示:
- sentences: 我们要分析的语料,可以是一个列表,或者从文件中遍历读出。
- size: 词向量的维度,默认值是100。这个维度的取值一般与我们的语料的大小相关,如果是不大的语料,比如小于100M的文本语料,则使用默认值一般就可以了。如果是超大的语料,建议增大维度。
- window:即词向量上下文最大距离,这个参数在我们的算法原理篇中标记为cc,window越大,则和某一词较远的词也会产生上下文关系。默认值为5。在实际使用中,可以根据实际的需求来动态调整这个window的大小。如果是小语料则这个值可以设的更小。对于一般的语料这个值推荐在[5,10]之间。
- sg: 即我们的word2vec两个模型的选择了。如果是0, 则是CBOW模型,是1则是Skip-Gram模型,默认是0即CBOW模型。
- hs: 即我们的word2vec两个解法的选择了,如果是0, 则是Negative Sampling,是1的话并且负采样个数negative大于0, 则是Hierarchical Softmax。默认是0即Negative Sampling。
- negative:即使用Negative Sampling时负采样的个数,默认是5。推荐在[3,10]之间。这个参数在我们的算法原理篇中标记为neg。
- cbow_mean: 仅用于CBOW在做投影的时候,为0,则算法中的xwxw为上下文的词向量之和,为1则为上下文的词向量的平均值。在我们的原理篇中,是按照词向量的平均值来描述的。个人比较喜欢用平均值来表示xwxw,默认值也是1,不推荐修改默认值。
- min_count:需要计算词向量的最小词频。这个值可以去掉一些很生僻的低频词,默认是5。如果是小语料,可以调低这个值。
- iter: 随机梯度下降法中迭代的最大次数,默认是5。对于大语料,可以增大这个值。
- alpha: 在随机梯度下降法中迭代的初始步长。算法原理篇中标记为ηη,默认是0.025。
- min_alpha: 由于算法支持在迭代的过程中逐渐减小步长,min_alpha给出了最小的迭代步长值。随机梯度下降中每轮的迭代步长可以由iter,alpha, min_alpha一起得出。这部分由于不是word2vec算法的核心内容,因此在原理篇我们没有提到。对于大语料,需要对alpha, min_alpha,iter一起调参,来选择合适的三个值。
代码如下:
根据官方给的数据集中'的word_seg'内容,训练词向量,生成word_idx_dict和vectors_arr两个结果,保存本地
import pandas as pd
import gensim
import time
import pickle
import numpy as np
import csv, sys
vector_size = 100
maxInt = sys.maxsize
decrement = True
while decrement:
# decrease the maxInt value by factor 10
# as long as the OverflowError occurs.
decrement = False
try:
csv.field_size_limit(maxInt)
except OverflowError:
maxInt = int(maxInt / 10)
decrement = True
# 0 辅助函数
#========================================================================================
def sentence2list(sentence):
return sentence.strip().split()
start_time = time.time()
data_path = 'D:/Race2018/DaGuan/ml/data_set/'
feature_path = 'D:/Race2018/DaGuan/ml/feature/feature_file/'
proba_path = 'D:/Race2018/DaGuan/ml/proba/proba_file/'
model_path = 'D:/Race2018/DaGuan/ml/model/model_file/'
result_path = "D:/Race2018/DaGuan/ml/result/"
# 1 准备训练数据
#========================================================================================
print("准备数据................ ")
df_train = pd.read_csv(data_path + 'train_set.csv', engine='python')
df_test = pd.read_csv(data_path + 'test_set.csv', engine='python')
sentences_train = list(df_train.loc[:, 'word_seg'].apply(sentence2list))
sentences_test = list(df_test.loc[:, 'word_seg'].apply(sentence2list))
sentences = sentences_train + sentences_test
print("准备数据完成! ")
# 2 训练
#========================================================================================
print("开始训练................ ")
model = gensim.models.Word2Vec(sentences=sentences, size=vector_size, window=5, min_count=5, workers=8, sg=0, iter=5)
print("训练完成! ")
# 3 提取词汇表及vectors,并保存
#========================================================================================
print(" 保存训练结果........... ")
wv = model.wv
vocab_list = wv.index2word
word_idx_dict = {}
for idx, word in enumerate(vocab_list):
word_idx_dict[word] = idx
vectors_arr = wv.vectors
vectors_arr = np.concatenate((np.zeros(vector_size)[np.newaxis, :], vectors_arr), axis=0) # 第0位置的vector为'unk'的vector
f_wordidx = open(feature_path + 'word_seg_word_idx_dict.pkl', 'wb')
f_vectors = open(feature_path + 'word_seg_vectors_arr.pkl', 'wb')
pickle.dump(word_idx_dict, f_wordidx)
pickle.dump(vectors_arr, f_vectors)
f_wordidx.close()
f_vectors.close()
end_time = time.time()
print("耗时:{}s ".format(end_time - start_time))
实战参考文献