CS224n-02 Word Vector Representations word2vec

02 Word Vector Representations word2vec

1、我们怎么表示一个单词的意思?

定义:意思这个单词。第一种是通过一个单词或者句子表示的意思;第二种是一个人想要取通过单词或者符号表示的意思;第三种是在写作或者一书中表示的意思。

一般的语言学对于意思的看法是:语言学中“meaning”近似于“指代、所指、符号”。

CS224n-02 Word Vector Representations word2vec

计算机如何对待意思?

通用的回答是:使用一种分类词典,就像是有着上位词关系的WordNet和同义词集。

CS224n-02 Word Vector Representations word2vec

离散表示的问题

1、同义词之间还是有细微的差别:adept, expert, good, practiced, proficient, skillful
2、缺少新词(不可能跟得上发展进步):wicked, badass, nifty, *****, ace, wizard, genius, ninja
3、主观性
4、需要人类劳动去创造和整理
5、很难计算准确的词相似度(word similarity)

无论是基于规则还是统计学的NLP都把词看做最小的单元:hotel, conference, walk。词语只是词表长度的one-hot向量,这是一种局部表示(localist representation)。

从符号到分布式表示

这个问题从网页搜索开始说起:如果用户搜索【Dell notebook battery size】,我们将会在文档中匹配【Dell laptop battery capacity】,如果用户搜索【Seattle motel】我们匹配的文档将包含【Seattle hotel】。
但是
CS224n-02 Word Vector Representations word2vec
我们的查询结果和文档向量是正交的。在ont-hot向量中无法得到相似度。可以分别处理相似度;相反,我们探索一种直接的方法,即向量对其进行编码。

基于表示方法的分布式相似度计算(Distributional similarity based representations)

你可以通过它的邻居来代表一个词来获得很多的价值。大多数成功地想法之一是统计NLP。(暂时没懂)
CS224n-02 Word Vector Representations word2vec

学习神将网络词嵌入的基本想法

我们定义一个在把预测中心词和上下文词作为目标的词向量。
CS224n-02 Word Vector Representations word2vec
损失函数为:

CS224n-02 Word Vector Representations word2vec
这里的w−tw−t表示wtwt的上下文(负号通常表示除了某某之外),如果完美预测,损失函数为零。
我们在大的语料库中查看了很多t的位置。然后通过不断调整词向量来最小化损失函数。

直接学习低维词向量

• Learning representations by back-propagating errors (Rumelhart et al., 1986)


• A neural probabilistic language model (Bengio et al., 2003)



• NLP (almost) from Scratch (Collobert & Weston, 2008)


• A recent, even simpler and faster model: word2vec (Mikolov et al. 2013) 

以前一直没有引起重视,直到Bengio展示了它的用处之大。后来研究才开始火热起来,并逐渐出现了更快更工业化的模型。(hankcs)

2、Word2vec的主要思想

利用单词和上下文的彼此预测。
两个算法:
1、Skip-grams (SG):利用给定的标签预测上下文单词(位置独立)。
2、Continuous Bag of Words (CBOW):从词袋上下文预测目标单词。

两个(适当有效)训练方法:
1、Hierarchical softmax
2、Negative sampling
这里会讲:Naïve softmax

Skip-gram prediction

CS224n-02 Word Vector Representations word2vec

skip-geam是依靠当前词word,预测content。

Word2vec算法的细节

对于每一个单词从t=1、...、T预测每个单词半径为m的窗口大小周围词。目标函数通过最大化对于给定中心词的上下文单词概率。

CS224n-02 Word Vector Representations word2vec

目标函数细节

术语:损失函数=代价函数=目标函数。
通常的概率分布损失:交叉熵损失。
用上下文单词one-hot wt+j作为目标

Word2vec算法的细节

对于每一个中心词预测窗口大小为m的周围单词。
对于CS224n-02 Word Vector Representations word2vec简化公式为:

CS224n-02 Word Vector Representations word2vec
softmax:using word c to obtain probability of word o(利用单词c来获得单词o的概率。)
o是输出的上下文词语中的确切某一个,c是中间的词语。u是对应的上下文词向量,v是词向量。

点积计算

CS224n-02 Word Vector Representations word2vec
点积的意义就是两个词向量越接近(相似),则结果越大。

Softmax function:从实数空间到概率分布的标准映射方法

CS224n-02 Word Vector Representations word2vec
指数函数可以把实数映射成正数,然后归一化得到概率
CS224n-02 Word Vector Representations word2vec
从左到右为center word的one-hot向量,乘以center word的W得到词向量,乘以另一个context word的矩阵W'得到对每个词语的“相似度”,对相似度取softmax得到概率,与标签对比计算损失。

训练模型:计算参数向量的梯度

我们定义所有的参数称作一个长向量
θ,对d维的词向量和大小V的词表来讲,有

CS224n-02 Word Vector Representations word2vec

4、梯度的延伸(详细推到)

CS224n-02 Word Vector Representations word2vec

链式法则

CS224n-02 Word Vector Representations word2vec

推导:对于1,就是在全数据上,对于每一个单词求其周围m个相邻的条件概率,使其总和最大化,这里j!=0就是出去他自身。
CS224n-02 Word Vector Representations word2vec
CS224n-02 Word Vector Representations word2vec


CS224n-02 Word Vector Representations word2vec
CS224n-02 Word Vector Representations word2vec

CS224n-02 Word Vector Representations word2vec

总结梯度:

CS224n-02 Word Vector Representations word2vec

5、损失/目标函数

CS224n-02 Word Vector Representations word2vec

梯度下降

CS224n-02 Word Vector Representations word2vec
CS224n-02 Word Vector Representations word2vec

CS224n-02 Word Vector Representations word2vec

CS224n-02 Word Vector Representations word2vec

参考:
http://web.stanford.edu/class/cs224n/
http://www.hankcs.com/nlp/word-vector-representations-word2vec.html
https://zhuanlan.zhihu.com/p/26530524