@hanxiaoyang
2016-11-08T15:15:26.000000Z
字数 9648
阅读 17381
word2vec
首先需要一份比较大的中文语料数据,可以考虑中文的维基百科(也可以试试搜狗的新闻语料库)。中文维基百科的打包文件地址为
https://dumps.wikimedia.org/zhwiki/latest/zhwiki-latest-pages-articles.xml.bz2
中文维基百科的数据不是太大,xml的压缩文件大约1G左右。首先用 process_wiki_data.py处理这个XML压缩文件,执行:python process_wiki_data.py zhwiki-latest-pages-articles.xml.bz2 wiki.zh.text
#!/usr/bin/env python# -*- coding: utf-8 -*-# process_wiki_data.py 用于解析XML,将XML的wiki数据转换为text格式import loggingimport os.pathimport sysfrom gensim.corpora import WikiCorpusif __name__ == '__main__':program = os.path.basename(sys.argv[0])logger = logging.getLogger(program)logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')logging.root.setLevel(level=logging.INFO)logger.info("running %s" % ' '.join(sys.argv))# check and process input argumentsif len(sys.argv) < 3:print globals()['__doc__'] % locals()sys.exit(1)inp, outp = sys.argv[1:3]space = " "i = 0output = open(outp, 'w')wiki = WikiCorpus(inp, lemmatize=False, dictionary={})for text in wiki.get_texts():output.write(space.join(text) + "\n")i = i + 1if (i % 10000 == 0):logger.info("Saved " + str(i) + " articles")output.close()logger.info("Finished Saved " + str(i) + " articles")
得到信息:
2016-08-11 20:39:22,739: INFO: running process_wiki.py zhwiki-latest-pages-articles.xml.bz2 wiki.zh.text2016-08-11 20:40:08,329: INFO: Saved 10000 articles2016-08-11 20:40:45,501: INFO: Saved 20000 articles2016-08-11 20:41:23,659: INFO: Saved 30000 articles2016-08-11 20:42:01,748: INFO: Saved 40000 articles2016-08-11 20:42:33,779: INFO: Saved 50000 articles......2016-08-11 20:55:23,094: INFO: Saved 200000 articles2016-08-11 20:56:14,692: INFO: Saved 210000 articles2016-08-11 20:57:04,614: INFO: Saved 220000 articles2016-08-11 20:57:57,979: INFO: Saved 230000 articles2016-08-11 20:58:16,621: INFO: finished iterating over Wikipedia corpus of 232894 documents with 51603419 positions (total 2581444 articles, 62177405 positions before pruning articles shorter than 50 words)2016-08-11 20:58:16,622: INFO: Finished Saved 232894 articles
Python的话可用jieba完成分词,生成分词文件wiki.zh.text.seg
接着用word2vec工具训练:
python train_word2vec_model.py wiki.zh.text.seg wiki.zh.text.model wiki.zh.text.vector
#!/usr/bin/env python# -*- coding: utf-8 -*-# train_word2vec_model.py用于训练模型import loggingimport os.pathimport sysimport multiprocessingfrom gensim.corpora import WikiCorpusfrom gensim.models import Word2Vecfrom gensim.models.word2vec import LineSentenceif __name__ == '__main__':program = os.path.basename(sys.argv[0])logger = logging.getLogger(program)logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')logging.root.setLevel(level=logging.INFO)logger.info("running %s" % ' '.join(sys.argv))# check and process input argumentsif len(sys.argv) < 4:print globals()['__doc__'] % locals()sys.exit(1)inp, outp1, outp2 = sys.argv[1:4]model = Word2Vec(LineSentence(inp), size=400, window=5, min_count=5,workers=multiprocessing.cpu_count())# trim unneeded model memory = use(much) less RAM#model.init_sims(replace=True)model.save(outp1)model.save_word2vec_format(outp2, binary=False)
运行信息
2016-08-12 09:50:02,586: INFO: running python train_word2vec_model.py wiki.zh.text.seg wiki.zh.text.model wiki.zh.text.vector2016-08-12 09:50:02,592: INFO: collecting all words and their counts2016-08-12 09:50:02,592: INFO: PROGRESS: at sentence #0, processed 0 words and 0 word types2016-08-12 09:50:12,476: INFO: PROGRESS: at sentence #10000, processed 12914562 words and 254662 word types2016-08-12 09:50:20,215: INFO: PROGRESS: at sentence #20000, processed 22308801 words and 373573 word types2016-08-12 09:50:28,448: INFO: PROGRESS: at sentence #30000, processed 30724902 words and 460837 word types...2016-08-12 09:52:03,498: INFO: PROGRESS: at sentence #210000, processed 143804601 words and 1483608 word types2016-08-12 09:52:07,772: INFO: PROGRESS: at sentence #220000, processed 149352283 words and 1521199 word types2016-08-12 09:52:11,639: INFO: PROGRESS: at sentence #230000, processed 154741839 words and 1563584 word types2016-08-12 09:52:12,746: INFO: collected 1575172 word types from a corpus of 156430908 words and 232894 sentences2016-08-12 09:52:13,672: INFO: total 278291 word types after removing those with count<52016-08-12 09:52:13,673: INFO: constructing a huffman tree from 278291 words2016-08-12 09:52:29,323: INFO: built huffman tree with maximum node depth 252016-08-12 09:52:29,683: INFO: resetting layer weights2016-08-12 09:52:38,805: INFO: training model with 4 workers on 278291 vocabulary and 400 features, using 'skipgram'=1 'hierarchical softmax'=1 'subsample'=0 and 'negative sampling'=02016-08-12 09:52:49,504: INFO: PROGRESS: at 0.10% words, alpha 0.02500, 15008 words/s2016-08-12 09:52:51,935: INFO: PROGRESS: at 0.38% words, alpha 0.02500, 44434 words/s2016-08-12 09:52:54,779: INFO: PROGRESS: at 0.56% words, alpha 0.02500, 53965 words/s2016-08-12 09:52:57,240: INFO: PROGRESS: at 0.62% words, alpha 0.02491, 52116 words/s2016-08-12 09:52:58,823: INFO: PROGRESS: at 0.72% words, alpha 0.02494, 55804 words/s2016-08-12 09:53:03,649: INFO: PROGRESS: at 0.94% words, alpha 0.02486, 58277 words/s2016-08-12 09:53:07,357: INFO: PROGRESS: at 1.03% words, alpha 0.02479, 56036 words/s......2016-08-12 19:22:09,002: INFO: PROGRESS: at 98.38% words, alpha 0.00044, 85936 words/s2016-08-12 19:22:10,321: INFO: PROGRESS: at 98.50% words, alpha 0.00044, 85971 words/s2016-08-12 19:22:11,934: INFO: PROGRESS: at 98.55% words, alpha 0.00039, 85940 words/s2016-08-12 19:22:13,384: INFO: PROGRESS: at 98.65% words, alpha 0.00036, 85960 words/s2016-08-12 19:22:13,883: INFO: training on 152625573 words took 1775.1s, 85982 words/s2016-08-12 19:22:13,883: INFO: saving Word2Vec object under wiki.zh.text.model, separately None2016-08-12 19:22:13,884: INFO: not storing attribute syn0norm2016-08-12 19:22:13,884: INFO: storing numpy array 'syn0' to wiki.zh.text.model.syn0.npy2016-08-12 19:22:20,797: INFO: storing numpy array 'syn1' to wiki.zh.text.model.syn1.npy2016-08-12 19:22:40,667: INFO: storing 278291x400 projection weights into wiki.zh.text.vector
测试模型效果:
In [1]: import gensimIn [2]: model = gensim.models.Word2Vec.load("wiki.zh.text.model")In [3]: model.most_similar(u"足球")Out[3]:[(u'\u8054\u8d5b', 0.6553816199302673),(u'\u7532\u7ea7', 0.6530429720878601),(u'\u7bee\u7403', 0.5967546701431274),(u'\u4ff1\u4e50\u90e8', 0.5872289538383484),(u'\u4e59\u7ea7', 0.5840631723403931),(u'\u8db3\u7403\u961f', 0.5560152530670166),(u'\u4e9a\u8db3\u8054', 0.5308005809783936),(u'allsvenskan', 0.5249762535095215),(u'\u4ee3\u8868\u961f', 0.5214947462081909),(u'\u7532\u7ec4', 0.5177896022796631)]In [4]: result = model.most_similar(u"足球")In [5]: for e in result:print e[0], e[1]....:联赛 0.65538161993甲级 0.653042972088篮球 0.596754670143俱乐部 0.587228953838乙级 0.58406317234足球队 0.556015253067亚足联 0.530800580978allsvenskan 0.52497625351代表队 0.521494746208甲组 0.51778960228In [6]: result = model.most_similar(u"男人")In [7]: for e in result:print e[0], e[1]....:女人 0.77537125349家伙 0.617369174957妈妈 0.567102909088漂亮 0.560832381248잘했어 0.540875017643谎言 0.538448691368爸爸 0.53660941124傻瓜 0.535608053207예쁘다 0.535151124001mc刘 0.529670000076In [8]: result = model.most_similar(u"女人")In [9]: for e in result:print e[0], e[1]....:男人 0.77537125349我的某 0.589010596275妈妈 0.576344847679잘했어 0.562340974808美丽 0.555426716805爸爸 0.543958246708新娘 0.543640494347谎言 0.540272831917妞儿 0.531066179276老婆 0.528521537781In [10]: result = model.most_similar(u"青蛙")In [11]: for e in result:print e[0], e[1]....:老鼠 0.559612870216乌龟 0.489831030369蜥蜴 0.478990525007猫 0.46728849411鳄鱼 0.461885392666蟾蜍 0.448014199734猴子 0.436584025621白雪公主 0.434905380011蚯蚓 0.433413207531螃蟹 0.4314712286In [12]: result = model.most_similar(u"姨夫")In [13]: for e in result:print e[0], e[1]....:堂伯 0.583935439587祖父 0.574735701084妃所生 0.569327116013内弟 0.562012672424早卒 0.558042645454曕 0.553856015205胤祯 0.553288519382陈潜 0.550716996193愔之 0.550510883331叔父 0.550032019615In [14]: result = model.most_similar(u"衣服")In [15]: for e in result:print e[0], e[1]....:鞋子 0.686688780785穿着 0.672499775887衣物 0.67173999548大衣 0.667605519295裤子 0.662670075893内裤 0.662210345268裙子 0.659705817699西装 0.648508131504洋装 0.647238850594围裙 0.642895817757In [16]: result = model.most_similar(u"公安局")In [17]: for e in result:print e[0], e[1]....:司法局 0.730189085007公安厅 0.634275555611公安 0.612798035145房管局 0.597343325615商业局 0.597183346748军管会 0.59476184845体育局 0.59283208847财政局 0.588721752167戒毒所 0.575558543205新闻办 0.573395550251In [18]: result = model.most_similar(u"铁道部")In [19]: for e in result:print e[0], e[1]....:盛光祖 0.565509021282交通部 0.548688530922批复 0.546967327595刘志军 0.541010737419立项 0.517836689949报送 0.510296344757计委 0.508456230164水利部 0.503531932831国务院 0.503227233887经贸委 0.50156635046In [20]: result = model.most_similar(u"清华大学")In [21]: for e in result:print e[0], e[1]....:北京大学 0.763922810555化学系 0.724210739136物理系 0.694550514221数学系 0.684280991554中山大学 0.677202701569复旦 0.657914161682师范大学 0.656435549259哲学系 0.654701948166生物系 0.654403865337中文系 0.653147578239In [22]: result = model.most_similar(u"卫视")In [23]: for e in result:print e[0], e[1]....:湖南 0.676812887192中文台 0.626506924629収蔵 0.621356606483黄金档 0.582251906395cctv 0.536769032478安徽 0.536752820015非同凡响 0.534517168999唱响 0.533438682556最强音 0.532605051994金鹰 0.531676828861In [24]: result = model.most_similar(u"习近平")In [25]: for e in result:print e[0], e[1]....:胡锦涛 0.809472680092江泽民 0.754633367062李克强 0.739740967751贾庆林 0.737033963203曾庆红 0.732847094536吴邦国 0.726941585541总书记 0.719057679176李瑞环 0.716384887695温家宝 0.711952567101王岐山 0.703570842743In [26]: result = model.most_similar(u"林丹")In [27]: for e in result:print e[0], e[1]....:黄综翰 0.538035452366蒋燕皎 0.52646958828刘鑫 0.522252976894韩晶娜 0.516120731831王晓理 0.512289524078王适 0.508560419083杨影 0.508159279823陈跃 0.507353425026龚智超 0.503159761429李敬元 0.50262516737In [28]: result = model.most_similar(u"语言学")In [29]: for e in result:print e[0], e[1]....:社会学 0.632598280907人类学 0.623406708241历史学 0.618442356586比较文学 0.604823827744心理学 0.600066184998人文科学 0.577783346176社会心理学 0.575571238995政治学 0.574541330338地理学 0.573896467686哲学 0.573873817921In [30]: result = model.most_similar(u"计算机")In [31]: for e in result:print e[0], e[1]....:自动化 0.674171924591应用 0.614087462425自动化系 0.611132860184材料科学 0.607891201973集成电路 0.600370049477技术 0.597518980503电子学 0.591316461563建模 0.577238917351工程学 0.572855889797微电子 0.570086717606In [32]: model.similarity(u"计算机", u"自动化")Out[32]: 0.67417196002404789In [33]: model.similarity(u"女人", u"男人")Out[33]: 0.77537125129824813In [34]: model.doesnt_match(u"早餐 晚餐 午餐 中心".split())Out[34]: u'\u4e2d\u5fc3'In [35]: print model.doesnt_match(u"早餐 晚餐 午餐 中心".split())中心