Lecture2_Stanford cs224d_Simple Word Vector Representations: word2vec,GloVe
文章目录

1. Word meaning
A question lies ahead
Q:How do we have a usable meaning in a computer?
Common answer:Use a taxonomy like WordNet that has hypernyms relationships and synonym set.
- WordNet is one of the most famous taxonomic resource and it is popular among computational linguists.Because It is free to download a copy,it provides a lot of taxonomy infomation about words.
- the components of WordNet
- demo implemented by python
The picture above shows you getting a hold of WordNet using the NLTK which is one of the main python packages for nlp.
在python3.7版本中运行结果如下:
显示跟"panda"一词接近的上位词列表如下:
[Synset(‘procyonid.n.01’),
Synset(‘carnivore.n.01’),
Synset(‘placental.n.01’),
Synset(‘mammal.n.01’),
Synset(‘vertebrate.n.01’),
Synset(‘chordate.n.01’),
Synset(‘animal.n.01’),
Synset(‘organism.n.01’),
Synset(‘living_thing.n.01’),
Synset(‘whole.n.02’),
Synset(‘object.n.01’),
Synset(‘physical_entity.n.01’),
Synset(‘entity.n.01’)]
discrete representation
上述有关词的离散表示虽然是一种语言学资源,但在实际应用中,结果可能并没有人们所期望得那么好。因为词的离散表示所找到的同义词在意思上还有细微的差别(nuance),其主要局限性体现在:
- 缺少新词(很难与时俱进取更新)
- 主观化
- 需要大量的人力去创建和维护
- 很难准确计算词语相似性
大量的基于规则的(rule-based)和统计(statistical)自然语言处理任务中将词视为一个不可分割的原子单元。在向量空间中,每一个向量由一个“1”和很多个“0”表示,我们将这种表示方法称为“one-hot” representation.它存在的问题是:
1. 语料库中词汇表的数目大,向量的维度也就变得非常大
Dimensionality:
20K (speech) – 50K (PTB) – 500K (big vocab) – 13M (Google 1T)
- “speech” means speech recognizer
- PTB- penn tree bank
在python中可直接调用:from nltk.corpus import ptb
- big vocab
if we kinda building a machine translation system,we might use a 500,000 word vocabulary.
- Google 1T
Google released sort of 1-terabyte corpus of web crawl.
2. 词向量两两正交(点乘为零 或称 内积为零)
or we can say there is no natural notion of similarity
How to make neighbors represent words?
语言学家J. R. Firth提出,通过一个单词的上下文可以得到它的意思。J. R. Firth甚至建议,如果你能把单词放到正确的上下文中去,才说明你掌握了它的意义。
“you shall know a word by the company it keeps.”
20世纪初在哲学语言学方面深有造诣的Wittgenstein也表示“the right way to think about the meaning of words is understanding their use in text.”
通过向量定义词语的含义Word meaning id defined in terms of vectors
通过调整一个单词及其上下文单词的向量,使得根据两个向量可以推测两个词语的相似度;或根据向量可以预测词语的上下文。这种手法也是递归的(recursive),根据向量来调整向量,与词典中意项的定义相似。
In addition,distributed representations与symbolic representations(localist representation、one-hot representation)相对;discrete representation则与后者及denotation的意思相似。切不可搞混distributed和discrete这两个单词。
学习神经网络word embeddings 的基本思路
- 定义一个用于预测某个单词上下文的模型:
p ( c o n t e x t ∣ w t ) p(context|w_{t}) p(context∣wt)
- 损失函数定义如下:
J = 1 − p ( w − t ∣ w t ) J=1-p(w_{-t}|w_{t}) J=1−p(w−t∣wt)
公式中 w − t w_{-t} w−t表示 w t w_{t} wt的上下文(负号通常表示“除某某以外”),训练模型的最终目标是改变词的向量表示使得损失函数逐渐逼近0
- 然后在一个大型语料库的不同位置得到训练实例,调整词向量,最小化损失函数。
2. Word2vec Introduction
Main idea of word2vec
Skip-gram prediction
Skip-gram 模型思想是:每一次估计,你可以选择一个词作为中心词(center word),并预测它的窗体大小范围内的上下文单词。模型的训练目标是最大化概率分布。
word2vec细节
对每一个单词 t=1,2,…,T(文本词汇表大小),我们需要预测该单词窗体“半径(radius)”为m的上下文单词。
- 目标函数:对于给定的当前中心词,最大化它的每一个上下文单词的预测概率。
J ′ ( θ ) = ∏ t = 1 T ∏ − m ≤ j ≤ m T p ( w t + j ∣ w t ; θ ) J'(\theta)=\displaystyle \prod^{T}_{t=1}\displaystyle \prod^{T}_{-m\leq{j}\leq{m}}p(w_{t+j}|w_{t};\theta) J′(θ)=t=1∏T−m≤j≤m∏Tp(wt+j∣wt