Stanford - CS224n, NLP with Deep learning
Winter 2019
Lecture 1, Introduction and Word Vectors
What do we hope to learn?
-
RNN
-
Attention
-
PyTorch
-
Word meaning, dependency parsing, machine translation, question answering
Traditional NLP use one-hot encoding for words. The problem is that the one-hot matrix can be very big. There is no natural notion of similarity of one-hot vectors because they are orthogonal.
We can use a word similarity tabl, yet the table will be very large (500,000 words X 500,000 words)!
Denotational Semantics VS. Distributional Semantics
-
Denotational Semantics: signifier (symbol) ⟺ signified (idea or thing)
-
Distributional Semantics: A word’s meaning is given by the words that frequently appear close-by
Word2vec
Idea:
-
We have a large corpus of text
-
Every word in a fixed vocabulary is represented by a vector
-
Go through each position t in the text, which has a center word c and context (“outside”) words o
-
Use the similarity of the word vectors for c and o to calculate the probability of o given c (or vice versa)
-
Keep adjusting the word vectors to maximize this probability
Example windows and process for computing \({P(w_{t+j} | w_t)}\)

We want to maximize the overall log likelihood of getting a correct prediction of context words within a window of fixed size m given center word \(w_j\). The objective function is:
\[Likelihood = L(\theta)=\prod_{t=1}^{T} \prod_{-m \leq j \leq m \atop j \neq 0} P\left(w_{t+j} | w_{t} ; \theta\right)\]Then the objective function is the average negative log likelihood.
\[{ J(\theta)=-\frac{1}{T} \log L(\theta)=-\frac{1}{T} \sum_{t=1}^{T} \sum_{-m \leq j \leq m \atop j \neq 0} \log P\left(w_{t+j} | w_{t} ; \theta\right) }\]Question: How to calculate \({P(w_{t+j} | w_t)}\) ? Answer: Use two vectors per word \(w\)
-
\(v_w\) when w is center word
-
\(u_w\) when w is a context word
Then for a center word c and a context word o:
\[P(o | c)=\frac{\exp \left(u_{o}^{T} v_{c}\right)}{\sum_{w \in V} \exp \left(u_{w}^{T} v_{c}\right)}\]
HW1
Got problem using virtual environment. The nltk can not find reuters. Deactivate virtual environment solved this. Link to HW1
Lecture 2, Word Vectors and Word Senses
idf-weighting
Lecture 5, Dependency Parsing
After two lectures of mathematical background in deep learning, we can finally started to learn some NLP stuff.
1. Two views of linguistic structure:
Phrase structure: organizes words into nested constituents.
- Can represent the grammar with CFG rules
Constituency = phrase structure grammar = context-free grammars (CFGs)
NP: noun phrase
PP: preposition
Det: Determinant
- Some of the grammar rules
- NP -> Det N (the cat, a dog)
- NP -> Det (Adj) N
- NP -> Det(Adj) N PP
- PP -> PP NP
Dependency structure
- Dependency structure shows which words depend on (modify or are arguments of ) which other words.
Why do we need sentence structure? We, human, need a certain sentence structure in order to convey complex meaning.
Example of prepositional phrase attachment ambiguity
This is an example of why NLP is hard. We human are communicate through languages, but language alone will not do it. We talk to people with the assumption that the other person have similar knowledge as ourself. This is “Common Sense”.
Here is an example of ambiguous sentences: San Jose cops kill man with knife
There are two meaning for this one sentences. First, cops stabbed and killed a man using knife. Second, cops killed a man who is holding a knife. According to our common sense, we tend to believe that the second is true. The meaning of a sentence is depended on the dependency structure of words. This is also why human languages are very different from programming languages.
- Catalan numbers: ${ C_n = (2n)! / [(n+1)!n!]}$
- An exponentially growing series, which arises in many tree-like contexts. CS228: Prob. Graphic Model.
Coordination Scope Ambiguity
Shuttle veteran and longtime NASA executive Fred Gregory appointed to board.
Either Fred Gregory is a shuttle veteran and a longtime NASA executive, or two people where one is shuttle veteran and the other is NASA executive.
Adjectival Modifier Ambiguity
This is a graduate level class, so prof. actually showed this example in his lecture!!!
Student get first hand job experience
Verb Phrase (VP) attachment ambiguity
The rise of annotated data, Universal Dependencies Treebands
Build a tree band seems a lot slower and less useful than building a grammar. But treebank have following advantages:
- Reusability of the labor
- Many parsers, part-of-speech taggers, etc. can be build on it
- Valuable resource for linguistics
- Broad coverage, not just a few intuitions
- Frequencies and distributional information
- A way to evaluate systems
Dependency can cross over: I will give a talk tomorrow on bootstrapping
on bootstrapping cross over talk.
Lecture 7, Vanishing Gradients, Fancy RNNs
Topics: Vanishing (exploding) gradient, LSTM, GRU, bidirectional, Multi-layers
Lecture 1, 引言与词向量
我们希望学到什么?
-
RNN
-
Attention
-
PyTorch
-
词义、依存句法分析、机器翻译、问答
传统 NLP 使用 one-hot 编码表示词。 问题是 one-hot 矩阵会非常大。 one-hot 向量之间没有自然的相似性概念,因为它们是正交的。
我们可以用词相似度表,但这个表会非常大(500,000 词 X 500,000 词)!
指称语义 VS. 分布语义
-
指称语义:能指(符号)⟺ 所指(概念或事物)
-
分布语义:一个词的含义由其经常出现在附近的词给出
Word2vec
思路:
-
我们有一个大型文本语料库
-
固定词表中的每个词用一个 向量 表示
-
遍历文本中的每个位置 t,该位置有中心词 c 和上下文(“外部”)词 o
-
用 c 和 o 的 词向量相似度 来计算给定 c 时 o 的概率(或反过来)
-
不断调整词向量 以最大化这个概率
计算 \({P(w_{t+j} | w_t)}\) 的窗口示例与过程

我们希望最大化在给定中心词 \(w_j\) 时,固定窗口大小 m 内正确预测上下文词的总体对数似然。目标函数 是:
\[Likelihood = L(\theta)=\prod_{t=1}^{T} \prod_{-m \leq j \leq m \atop j \neq 0} P\left(w_{t+j} | w_{t} ; \theta\right)\]然后 目标函数 是平均负对数似然。
\[{ J(\theta)=-\frac{1}{T} \log L(\theta)=-\frac{1}{T} \sum_{t=1}^{T} \sum_{-m \leq j \leq m \atop j \neq 0} \log P\left(w_{t+j} | w_{t} ; \theta\right) }\]问题:如何计算 \({P(w_{t+j} | w_t)}\) ? 答案:每个词 \(w\) 使用两个向量
-
\(v_w\) 当 w 是中心词时
-
\(u_w\) 当 w 是上下文词时
然后对于中心词 c 和上下文词 o:
\[P(o | c)=\frac{\exp \left(u_{o}^{T} v_{c}\right)}{\sum_{w \in V} \exp \left(u_{w}^{T} v_{c}\right)}\]
HW1
使用虚拟环境时遇到问题。nltk 找不到 reuters。停用虚拟环境后解决了。 HW1 链接
Lecture 2, 词向量与词义
idf-weighting
Lecture 5, 依存句法分析
经过两讲深度学习的数学背景后,我们终于可以开始学一些 NLP 内容了。
1. 语言结构的两种观点:
短语结构:将词组织成嵌套的成分。
- 可以用 CFG 规则表示语法
Constituency = 短语结构语法 = 上下文无关文法 (CFGs)
NP: 名词短语
PP: 介词
Det: 限定词
- 部分语法规则
- NP -> Det N (the cat, a dog)
- NP -> Det (Adj) N
- NP -> Det(Adj) N PP
- PP -> PP NP
依存结构
- 依存结构显示哪些词依赖于(修饰或作为论元)哪些其他词。
为什么需要句子结构?我们人类需要一定的句子结构来传达复杂含义。
介词短语附着歧义的例子
这是 NLP 困难的一个例子。我们人类通过语言交流,但仅靠语言不够。我们与人交谈时假设对方拥有与我们相似的知识。这就是“常识”。
下面是一个歧义句子的例子: San Jose cops kill man with knife
这句话有两种含义。第一,警察用刀刺死了一个人。 第二,警察杀了一个拿着刀的人。根据我们的常识,我们倾向于相信第二种是真的。 句子的含义取决于词的依存结构。这也是人类语言与编程语言非常不同的原因。
- Catalan 数: ${ C_n = (2n)! / [(n+1)!n!]}$
- 一个指数增长的序列,出现在许多树状上下文中。CS228: Prob. Graphic Model.
并列范围歧义
Shuttle veteran and longtime NASA executive Fred Gregory appointed to board.
要么 Fred Gregory 既是航天飞机老兵又是长期 NASA 高管,要么是两个人,一个是航天飞机老兵,另一个是 NASA 高管。
形容词修饰歧义
这是研究生课程,所以教授在讲座中真的展示了这个例子!!!
Student get first hand job experience
动词短语 (VP) 附着歧义
标注数据的兴起,Universal Dependencies 树库
构建树库看起来比构建语法慢得多且用处更小。但树库有以下优点:
- 劳动的可重用性
- 可以在其上构建许多解析器、词性标注器等
- 对语言学来说是宝贵资源
- 覆盖面广,不仅仅是一些直觉
- 频率和分布信息
- 一种评估系统的方法
依存可以交叉: I will give a talk tomorrow on bootstrapping
on bootstrapping 跨越了 talk。
Lecture 7, 梯度消失,花式 RNN
主题:梯度消失(爆炸)、LSTM、GRU、双向、多层
Comments