Stanford CS224n — Lecture 2 Word Vectors

Evelyn0116
Jul 20, 2017 · 6 min read

Natural Language Processing

Word Meaning

Webster 的字典上表示 meaning 是:
1. 表達字或片語的概念
2. 某人想要藉由字或是標示想要表達的想法

因為 meaning 在語言學上是給予某個特定的符號(文字)定義(donotation),所以為了實現讓電腦系統學會語言,最常見的方法是使用 — 分類(taxonomy),課堂上給出兩個範例:

左邊的圖表示 — 給出 panda 所屬的所有類別(哺乳類、動物等等)。
右邊的圖表示 — 給出和 good 同義的集合(每列都是不同意思的集合)。

但是對於這些離散的表示方法,有幾種缺點

  1. 分不出細微的差別
    ex. I’ m good at deep learning. / I’ m expert at deep learning.
  2. 常常會有缺字(因為無法常常更新)
    ex. wicked, badass, nifty
  3. 佔用過多空間
    因為 NLP 視 words 為原子符號,所以在 vector space 形式時,就會是一個有著很多零與一個一的向量。
One-hot representation

這樣造就的問題就是,基於兩個單字都是一個 one-hot vector,他們之間就沒有任何的相似處,兩個會正交(orthogonal),也就是兩個詞彙之間會有一個鴻溝,我們無法單從這兩個詞之中找到是否有關係。

解決辦法就是,我們用相似度隔離,然後直接用 vector encode 他(有點像把這些向量視為點物品)。

接著要了解如何實現這個問題的解決辦法:

Distributional similarity based representations

藉由一個單字的上下文來理解這個詞彙

這是現代統計 NLP 一個很成功的地方。

Main idea of word2vec

要怎麼用上下文來表示單字?

Basic Idea of Learning Neural Network Word Embeddings

給 words 預測上下文的機率,一旦得到這個機率,我們就可以有 loss function:J= 1 − p( w_-t | w_t)。所以我們要做的就是,把文集用 vector 表示(做 word embedding)以方便運算。

在 word2ve 裡有兩種演算法:

  • Skip-grams (SG)
    給定 target,預測上下文 (position independent)
  • Continuous Bag of Words (CBOW)
    給定上下文,預測 target

還有兩種訓練方法:

  • Hierarchical softmax
  • Negative sampling

Skip-gram prediction

每個計算過程,拿一個單字當作中心字,也就是給中間的 target,用 vector representation 做「一次」(而不是左右各一個)probability distribution 得到一個會有最大機率的上下文。

Skip-gram prediction

Details of word2vec

對於每個 word,我們都預測在半徑 m 之內的 word,也就是左邊 m 個單字、右邊 m 個單字。

所以我們的目標函數(或損失函數)就會是如何在,給定 center word 下,找到機率最大的上下文。

這裡,就是拿一段長度 T 的文集,像是一段 wiki 上的段落,然後中間字左右寬度為 m 的範圍,來得到 J ’(theta)。theta 是 word 的 vector representation。

有些問題之後課堂會仔細講解,其中一個最重要的是因為 vector representation 是 one-hot 表示,所以可能會留下負的機率,這是不可能的。

這就是到底要如何計算出我們要的機率,o 表示 outside word index,c 是 center word index,跟 w_t+j 不一樣,j 是表示在段落中的 position,而 o 跟 c 是表示一個字對於中心字的 index。

Dot product

做內積的話,可以想像成,越接近的向量,內積會越大,也就是越相似。所以 iterator over w = 1…V 表示每一個 word 跟 center word 有多相似。

Softmax function

Standard Map from R^V to the probability distribution

基本上 softmax 上是將 word representation(向量)轉換成機率的辦法。如果只是用 dot product,只能得到數字像是 17、-32 等等,而不是機率,所以我們用 exponential 來讓數字變成正數,再用 sum 正歸化。

  • 這裡有 center 跟 outside vector 就是為了方便計算。
  • 在這裏,距離並沒有影響機率分佈(雖然實際上有)。

)

Evelyn0116

Written by

Struggling and searching for something special.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade