Google词向量word2vec的使用
"""
1.在自然语言处理中常常使用预训练的word2vec,这个预训练的词向量可以使用google的GoogleNews-vectors-negative300.bin
2.GoogleNews-vectors-negative300.bin是训练好的300维的新闻语料词向量
3.本函数的作用就是把一个词转换成词向量,以供我们后期使用。没有在该word2vec中的词采用其他的方式构建,如采用均匀分布或者高斯分布等随机初始化
"""
import numpy as np # loads 300x1 word vectors from file.
def load_bin_vec(fname, vocab):
word_vecs = {}
with open(fname, "rb") as f:
header = f.readline()
vocab_size, layer1_size = map(int, header.split()) # 3000000 300
binary_len = np.dtype('float32').itemsize * layer1_size #
for line in range(vocab_size):
word = []
while True:
ch = f.read(1)
if ch == ' ':
word = ''.join(word)
break
if ch != '\n':
word.append(ch)
if word in vocab:
word_vecs[word] = np.fromstring(f.read(binary_len), dtype='float32')
else:
f.read(binary_len)
return word_vecs # add random vectors of unknown words which are not in pre-trained vector file.
# if pre-trained vectors are not used, then initialize all words in vocab with random value.
def add_unknown_words(word_vecs, vocab, min_df=1, k=300):
for word in vocab:
if word not in word_vecs and vocab[word] >= min_df:
word_vecs[word] = np.random.uniform(-0.25, 0.25, k) vectors_file = './GoogleNews-vectors-negative300.bin'
vocab = ['I', 'can', 'do'] vectors = load_bin_vec(vectors_file, vocab) # pre-trained vectors
add_unknown_words(vectors, vocab)
print(vectors['I'])
print('*'*40)
print(vectors['can'])
print('*'*40)
print(vectors['do'])
Google词向量word2vec的使用的更多相关文章
- 关于Google词向量模型(googlenews-vectors-negative300.bin)的导入问题
起因 项目中有如下代码: word2vec = KeyedVectors.load_word2vec_format('./GoogleNews-vectors-negative300.bin', bi ...
- 词向量word2vec(图学习参考资料)
介绍词向量word2evc概念,及CBOW和Skip-gram的算法实现. 项目链接: https://aistudio.baidu.com/aistudio/projectdetail/500940 ...
- 词向量 word2vec
看的这一篇的笔记 http://licstar.net/archives/328 看不太懂. 要学的话,看这里吧,这里把一些资料做了整合: http://www.cnblogs.com/wuzhitj ...
- 词向量1.md
词向量 我们以句子分类为例,我们使用深度学习模型对句子进行分类,本质上这个模型的接受的舒服需要是数值型.因为文字是人们抽象出来的一个概念,这个 东西是不能被计算机直接理解的,我们需要人为的将这个文字转 ...
- 学习笔记CB009:人工神经网络模型、手写数字识别、多层卷积网络、词向量、word2vec
人工神经网络,借鉴生物神经网络工作原理数学模型. 由n个输入特征得出与输入特征几乎相同的n个结果,训练隐藏层得到意想不到信息.信息检索领域,模型训练合理排序模型,输入特征,文档质量.文档点击历史.文档 ...
- 基于word2vec训练词向量(一)
转自:https://blog.csdn.net/fendouaini/article/details/79905328 1.回顾DNN训练词向量 上次说到了通过DNN模型训练词获得词向量,这次来讲解 ...
- 使用word2vec训练中文词向量
https://www.jianshu.com/p/87798bccee48 一.文本处理流程 通常我们文本处理流程如下: 1 对文本数据进行预处理:数据预处理,包括简繁体转换,去除xml符号,将单词 ...
- 【word2vec】Distributed Representation——词向量
Distributed Representation 这种表示,它最早是 Hinton 于 1986 年提出的,可以克服 one-hot representation 的缺点. 其基本想法是: 通过训 ...
- Word2Vec词向量(一)
一.词向量基础(一)来源背景 word2vec是google在2013年推出的一个NLP工具,它的特点是将所有的词向量化,这样词与词之间就可以定量的去度量他们之间的关系,挖掘词之间的联系.虽然源码是 ...
随机推荐
- 为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?【转载】
转载自:http://www.cnblogs.com/dancefire/archive/2013/01/02/why-notifyDataSetChanged-does-not-work.html ...
- MyBatis踩坑记录
在线文档: 动态SQL http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html 1. Error setting null for paramete ...
- iOS_UITextField 基本操作
基本操作 UITextField *userNameTextField = [[UITextField alloc] init]; userNameTextField.frame = CGRectMa ...
- Eclipse------导入项目后出现Java compiler level does not match the version of the installed Java project facet
报错信息:Java compiler level does not match the version of the installed Java project facet 解决方法: 1.点击工具 ...
- Mybatis -- 批量更新 -- updateBatch
mysql数据库配置: 数据库连接必须配置:&allowMultiQueries=true并且‘&’ 用&替换 jdbc.url=jdbc:mysql://192.168.10 ...
- html+jquery制作网页地图
http://jvectormap.com/ <!--StartFragment --> JVectorMap 是一个显示矢量地图的jQuery插件.它使用 SVG 在Firefox 3 ...
- 【Python】Docx解析
1.cd D:\ProgramData\Anaconda3 2.pip install python-docx 3.python代码处理 # -*- coding: utf-8 -*- import ...
- java-使用SImpleDateFormat格式化时间输出
之前有篇博客是使用NSDateFormatter来对时间进行格式化输出,但使用起来有点繁琐,今天介绍下最近刚刚使用的SimpleDateFormat. public class SimpleDateF ...
- 基础知识《零》---一张图读懂JDK,JRE,JVM的区别与联系
- 【PHP】 毫秒级时间戳和日期格式转换
在并发量搞得情况下.需要开启毫秒级运算 mysql 支持: `create_time` datetime() DEFAULT NULL COMMENT '创建时间', 效果 PHP 代码实现: &l ...