词向量之加载word2vec和glove
来源:互联网 发布:qq飞车雷诺数据 编辑:程序博客网 时间:2024/05/16 13:38
1 Google用word2vec预训练了300维的新闻语料的词向量googlenews-vecctors-negative300.bin,解压后3.39个G。
可以用gensim加载进来,但是需要内存足够大。
#加载Google训练的词向量import gensimmodel = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin',binary=True)print(model['love'])
Glove300维的词向量有5.25个G。
# 用gensim打开glove词向量需要在向量的开头增加一行:所有的单词数 词向量的维度import gensimimport osimport shutilimport hashlibfrom sys import platform#计算行数,就是单词数def getFileLineNums(filename):f = open(filename, 'r')count = 0for line in f:count += 1return count#Linux或者Windows下打开词向量文件,在开始增加一行def prepend_line(infile, outfile, line):with open(infile, 'r') as old:with open(outfile, 'w') as new:new.write(str(line) + "\n")shutil.copyfileobj(old, new)def prepend_slow(infile, outfile, line):with open(infile, 'r') as fin:with open(outfile, 'w') as fout:fout.write(line + "\n")for line in fin:fout.write(line)def load(filename):num_lines = getFileLineNums(filename)gensim_file = 'glove_model.txt'gensim_first_line = "{} {}".format(num_lines, 300)# Prepends the line.if platform == "linux" or platform == "linux2":prepend_line(filename, gensim_file, gensim_first_line)else:prepend_slow(filename, gensim_file, gensim_first_line)model = gensim.models.KeyedVectors.load_word2vec_format(gensim_file)load('glove.840B.300d.txt')生成的glove_model.txt就是可以直接用gensim打开的模型。
0 1
- 词向量之加载word2vec和glove
- GloVe 词向量模型
- 词向量之word2vec(1)
- 词向量之word2vec(2)
- 词向量源码解析:(3.5)GloVe源码解析之glove
- GloVe 教程之实战入门+python gensim 词向量
- CS224n笔记三之词向量模型与GloVe
- word2vec 与 Glove 对比
- Windows版本的Google word2vec和Stanford GloVe工具
- word2vec 词向量
- word2vec 词向量
- word2vec 词向量训练
- GloVe学习之Python中简单的词向量SVD分解
- 词向量源码解析:(3.2)GloVe源码解析之vocab_count
- 词向量源码解析:(3.3)GloVe源码解析之cooccur
- 词向量源码解析:(3.4)GloVe源码解析之shuffle
- Word2Vec之Deep Learning in NLP (一)词向量和语言模型
- word2vec 之 Deep Learning in NLP (一)词向量和语言模型
- PAT-L1古风排版 打印字符串和处理
- 加入人工智能大战 三星正式发布AI助手Bixby
- eclipse引入maven项目
- 画圆+真机调试
- POJ 2027 No Brainer
- 词向量之加载word2vec和glove
- httpbin 学习笔记.
- Linux进程间通信——使用数据报套接字
- linux集群服务器时间同步
- 58-基于 UDP 协议的回射服务器
- 【数据结构基础】非递归后序遍历二叉树
- Angular入门知识点总结
- 什么是神经网络
- 删除语句 Drop/Delete/Truncate比较