word2vec

Word2vec的One-hot文本离散表示个人理解

匿名 (未验证) 提交于 2019-12-02 23:43:01
one-hot编码 中文名叫独热编码、一位有效编码。方法是使用N位状态寄存器来对N个状态进行编码,每个状态都有其独立的寄存器位,并且任意时刻,有且仅有一个状态位是有效的。比如,手写数字识别,数字为0-9共10个,那么每个数字的one-hot编码为10位,数字i的第i位为1,其余为0,如数字2的one-hot表示为:[0,0,1,0,0,0,0,0,0,0]。 one-hot在提取文本特征上的应用 one-hot在特征提取上属于词袋模型(bags of words) 假设语料库有这么三段话: “我爱中国” “爸爸妈妈爱我” “爸爸妈妈爱中国” 对语料库分词并进行编号(字典形式) {1:我,2:爱,3:爸爸,4:妈妈,5:中国} 对每段话用one-hot提取特征向量 则三段话由one-hot表示为: 我爱中国 ->[1,1,0,0,1] 爸爸妈妈爱我 ->[1,1,1,1,0] 爸爸妈妈爱中国 ->[0,1,1,1,1] 优点 :可以将数据用one-hot进行离散化,在一定程度上起到了扩充特征的作用 缺点 :没有考虑词与词之间的顺序,并且假设词与词之间相互独立,得到的特征是离散稀疏的(如果365天用one-hot,就是365维,会很稀疏),例如,在这样的编码格式下,“爸爸妈妈爱我”和“我爱爸爸妈妈”被认为是一样的。 文章来源: https://blog.csdn.net/qq

NlP之word2vec的发展历程

本秂侑毒 提交于 2019-12-02 21:58:19
#NlP之word2vec 的前世今生 2013年,Google开源了一款用于词向量计算的工具——word2vec,引起了工业界和学术界的关注。首先,word2vec可以在百万数量级的词典和上亿的数据集上进行高效地训练;其次,该工具得到的训练结果——词向量(word embedding),可以很好地度量词与词之间的相似性。随着深度学习(Deep Learning)在自然语言处理中应用的普及,很多人误以为word2vec是一种深度学习算法。其实word2vec算法的背后是一个浅层神经网络。 #词向量的基础 ##one-hot 表示 在很早之前就有用词向量表示词,但是词向量较为冗长,词向量的维度是整个词表大小,只有词对应的位置为一,其余都是0。 词向量的维度过大,且构成的矩阵极其稀疏,并且one-hot表示会丢掉词于此之间的关系。 Dristributed representation可以解决One hot representation的问题,它的思路是通过训练,将每个词都映射到一个较短的词向量上来。所有的这些词向量就构成了向量空间,进而可以用普通的统计学的方法来研究词与词之间的关系。这个较短的词向量维度是多大呢?这个一般需要我们在训练时自己来指定。如下图所示,将“red mini van”这个单词映射到3维向量上,实际是对向量进行了降维处理。 因为使用disturbuted

【词向量】从Word2Vec到Bert,聊聊词向量的前世今生(一)

半世苍凉 提交于 2019-12-02 21:57:56
机器学习算法与自然语言处理推荐 来源:https://zhuanlan.zhihu.com/p/58425003 作者:小川Ryan 【机器学习算法与自然语言处理导读】 BERT并不是凭空而来,如何从word2vec给出一些思考,本文进行了介绍! 由于近日所做的工作与预训练模型联系比较紧密,却发现自己对几个词向量内部的细节有所遗忘了,因此打算写篇文章来拾起一些记忆,同时也方便以后供自己和他人查阅。 1. 语言模型 n-gram model 谈到词向量 则必须要从语 言模型讲起,传统的统计语言模型是对于给定长度为m的句子,计算其概率分布P(w1, w2, ..., wm),以表示该句子存在的可能性。该概率可由下列公式计算得到: 但实际过程中句子的长度稍长便会为估计带来很大难度,因此n-gram 模型对上述计算进行简化:假定第i个词的出现仅与其前n-1个词有关,即: 实际计算中,通常采用n元短语在语料中出现的频率来估计其概率: 为保留句子原有的顺序信息,我们当然希望n越大越好,但实际上当n略大时,该n元短语在语料中出现的频率就会越低,用上式估计得到的概率就容易出现数据稀疏的问题。而神经网络语言模型的出现,有效地解决了这个问题。 Neural Network Language Model 神经网络语言模型不使用频率来估计n元短语出现的概率,而是通过神经网络训练得到一个语言模型。

Bert由来之--word2vec和transformer

♀尐吖头ヾ 提交于 2019-12-02 21:46:52
word2vec -> Transformer -> ELMo -> Bert word2vec是训练词向量。有skim-gram和CBOW两种形式。 重点:训练样本是一个句子的中心词和周围的词拼接成。 例如skim-gram例如 I love apple very much。以apple 为中心词,窗口大小是2,则可以凑出样本:【apple,love】【apple,I】【apple,very】【apple,much】。样本的前一个input,后一个是output,训练一个词的v_c和U(词向量和outputvector) CBOW是反过来的,通过周围的词来预测中心词。 Transformer是全部注意力,舍去CNN和RNN架构 例如:机器翻译 encoder的具体长相 self-attention得到特征向量Z,即下图中的Attention 而Feed Forward阶段,是两层全连接,第一层的激活函数是ReLU,第二层的激活函数是 decoder的长相 可以看出,decoder和encoder不同的是attention是两个了, 解释说: attention1)是当前翻译的和已经翻译的关系,就是y_t和y_t-1的关系 attention2)是当前翻译的和句子的语义向量的关系 层与层之间的关系 详细介绍self-attention 上面的图,矩阵中间的q1,q2,k1,k2

词向量经典模型:从word2vec、glove、ELMo到BERT

两盒软妹~` 提交于 2019-12-02 21:45:48
前言 词向量技术将自然语言中的词转化为稠密的向量,相似的词会有相似的向量表示,这样的转化方便挖掘文字中词语和句子之间的特征。生成词向量的方法从一开始基于统计学的方法(共现矩阵、SVD分解)到基于不同结构的神经网络的语言模型方法。这里总结一下比较经典的语言模型方法:word2vec、glove、ELMo、BERT。 其中BERT是最新Google发表的模型,在11个经典的NLP任务中全面超越最佳模型,并且为下游任务设计了简单至极的接口,改变了之前花销的Attention、Stack等盖楼似的堆叠结构的玩法,应该属于NLP领域里程碑式的贡献。 word2vec word2vec来源于2013年的论文《Efficient Estimation of Word Representation in Vector Space》,它的核心思想是通过词的上下文得到词的向量化表示,有两种方法:CBOW(通过附近词预测中心词)、Skip-gram(通过中心词预测附近的词): CBOW : 通过目标词的上下文的词预测目标词,图中就是取大小为2的窗口,通过目标词前后两个词预测目标词。具体的做法是,设定词向量的维度d,对所有的词随机初始化为一个d维的向量,然后要对上下文所有的词向量编码得到一个隐藏层的向量,通过这个隐藏层的向量预测目标词,CBOW中的做法是简单的相加,然后做一个softmax的分类

Why does word2Vec use cosine similarity?

ε祈祈猫儿з 提交于 2019-12-02 20:39:58
I have been reading the papers on Word2Vec (e.g. this one ), and I think I understand training the vectors to maximize the probability of other words found in the same contexts. However, I do not understand why cosine is the correct measure of word similarity. Cosine similarity says that two vectors point in the same direction, but they could have different magnitudes. For example, cosine similarity makes sense comparing bag-of-words for documents. Two documents might be of different length, but have similar distributions of words. Why not, say, Euclidean distance? Can anyone one explain why

SpaCy: how to load Google news word2vec vectors?

微笑、不失礼 提交于 2019-12-02 19:12:25
I've tried several methods of loading the google news word2vec vectors ( https://code.google.com/archive/p/word2vec/ ): en_nlp = spacy.load('en',vector=False) en_nlp.vocab.load_vectors_from_bin_loc('GoogleNews-vectors-negative300.bin') The above gives: MemoryError: Error assigning 18446744072820359357 bytes I've also tried with the .gz packed vectors; or by loading and saving them with gensim to a new format: from gensim.models.word2vec import Word2Vec model = Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True) model.save_word2vec_format('googlenews2.txt') This

Use LSTM tutorial code to predict next word in a sentence?

三世轮回 提交于 2019-12-02 17:35:53
I've been trying to understand the sample code with https://www.tensorflow.org/tutorials/recurrent which you can find at https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/ptb_word_lm.py (Using tensorflow 1.3.0.) I've summarized (what I think are) the key parts, for my question, below: size = 200 vocab_size = 10000 layers = 2 # input_.input_data is a 2D tensor [batch_size, num_steps] of # word ids, from 1 to 10000 cell = tf.contrib.rnn.MultiRNNCell( [tf.contrib.rnn.BasicLSTMCell(size) for _ in range(2)] ) embedding = tf.get_variable( "embedding", [vocab_size, size], dtype=tf

TensorFlow Embedding Lookup

微笑、不失礼 提交于 2019-12-02 17:20:13
I am trying to learn how to build RNN for Speech Recognition using TensorFlow. As a start, I wanted to try out some example models put up on TensorFlow page TF-RNN As per what was advised, I had taken some time to understand how word IDs are embedded into a dense representation (Vector Representation) by working through the basic version of word2vec model code. I had an understanding of what tf.nn.embedding_lookup actually does, until I actually encountered the same function being used with two dimensional array in TF-RNN ptb_word_lm.py , when it did not make sense any more. what I though tf

What is a projection layer in the context of neural networks?

非 Y 不嫁゛ 提交于 2019-12-02 15:33:00
I am currently trying to understand the architecture behind the word2vec neural net learning algorithm, for representing words as vectors based on their context. After reading Tomas Mikolov paper I came across what he defines as a projection layer . Even though this term is widely used when referred to word2vec , I couldn't find a precise definition of what it actually is in the neural net context. My question is, in the neural net context, what is a projection layer? Is it the name given to a hidden layer whose links to previous nodes share the same weights? Do its units actually have an