Simon的专栏
word2vec介绍
word2vec官网:/p/word2vec/
word2vec是google的一个开源工具,能够根据输入的词的集合计算出词与词之间的距离。
它将term转换成向量形式,可以把对文本内容的处理简化为向量空间中的向量运算,计算出向量空间上的相似度,来表示文本语义上的相似度。
word2vec计算的是余弦值,距离范围为0-1之间,值越大代表两个词关联度越高。
词向量:用Distributed Representation表示词,通常也被称为“Word Representation”或“Word Embedding(嵌入)”。
简言之:词向量表示法让相关或者相似的词,在距离上更接近。
具体使用
收集语料
本文:
网上的英文语料:/dc/text8.zip
语料训练信息:training on 85026035 raw words (62529137 effective words) took 197.4s, 316692 effective words/s
该语料编码格式UTF-8,存储为一行,长度很长……如下:
注意:
理论上语料越大越好
理论上语料越大越好
理论上语料越大越好
重要的事情说三遍。
因为太小的语料跑出来的结果并没有太大意义。
word2vec使用
Python,利用gensim模块。
win7系统下在通常的python基础上gensim模块不太好安装,所以建议使用anaconda,具体参见:python开发之anaconda【以及win7下安装gensim】
直接上代码——
#!/usr/bin/env python# -*- coding: utf-8 -*-"""功能:测试gensim使用时间:5月21日 18:07:50"""from gensim.models import word2vecimport logging# 主程序logging.basicConfig(format="%(asctime)s : %(levelname)s : %(message)s", level=logging.INFO)sentences = word2vec.Text8Corpus(u"C:\\Users\\lenovo\\Desktop\\word2vec实验\\text8") # 加载语料model = word2vec.Word2Vec(sentences, size=200) # 训练skip-gram模型; 默认window=5# 计算两个词的相似度/相关程度y1 = model.similarity("woman", "man")print u"woman和man的相似度为:", y1print "--------\n"# 计算某个词的相关词列表y2 = model.most_similar("good", topn=20) # 20个最相关的print u"和good最相关的词有:\n"for item in y2: print item[0], item[1]print "--------\n"# 寻找对应关系print " "boy" is to "father" as "girl" is to ...? \n"y3 = model.most_similar(["girl", "father"], ["boy"], topn=3)for item in y3: print item[0], item[1]print "--------\n"more_examples = ["he his she", "big bigger bad", "going went being"]for example in more_examples: a, b, x = example.split() predicted = model.most_similar([x, b], [a])[0][0] print ""%s" is to "%s" as "%s" is to "%s"" % (a, b, x, predicted)print "--------\n"# 寻找不合群的词y4 = model.doesnt_match("breakfast cereal dinner lunch".split())print u"不合群的词:", y4print "--------\n"# 保存模型,以便重用model.save("text8.model")# 对应的加载方式# model_2 = word2vec.Word2Vec.load("text8.model")# 以一种C语言可以解析的形式存储词向量model.save_word2vec_format("text8.model.bin", binary=True)# 对应的加载方式# model_3 = word2vec.Word2Vec.load_word2vec_format("text8.model.bin", binary=True)if __name__ == "__main__": pass1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
运行结果
woman和man的相似度为: 0.685955257368--------和good最相关的词有:bad 0.739628911018poor 0.563425064087luck 0.525990724564fun 0.520761489868quick 0.518206238747really 0.491045713425practical 0.479608744383helpful 0.478456377983love 0.477012127638simple 0.475951403379useful 0.474674522877reasonable 0.473541408777safe 0.473105460405you 0.47159832716courage 0.470109701157dangerous 0.469624102116happy 0.468672126532wrong 0.467448621988easy 0.467320919037sick 0.466005086899-------- "boy" is to "father" as "girl" is to ...? mother 0.770967006683wife 0.718966007233grandmother 0.700566351414--------"he" is to "his" as "she" is to "her""big" is to "bigger" as "bad" is to "worse""going" is to "went" as "being" is to "was"--------不合群的词: cereal--------1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
参考资料
深度学习:使用 word2vec 和 gensim:
http://www.open-/lib/view/open1420687622546.html