CS224N - 02.Word Vectors and Word Senses
- CS224N의 2주차 강의, Word Vectors and Word Senses를 보고 정리한 글입니다.
1. Word2vec Review
2. Optimization Basic
3. The GloVe model of word vectors
4. Evaluating Word Vectors
Word2vec Review
- Idea
- “Iterative updating algorithm” (전체 corpus의 각 단어들을 iterate하라.)
- 전체 word vectors를 이용하여 주변 단어 예측하기
▲ 중심단어 c가 나타났을 때, 주변단어 o가 나타날 확률
Word2vec parametes and computations
PyTorch, Tensorflow등 모든 딥러닝 패키지들: Word vector들을 Row로 표현한다! (일반적으로 수학에서 Column으로 생각하는 사고랑 다름)
‘Word2vec maximizes objective function by putting similar words nearby in space’
Optimization Basic
Gradient Descent
- J(Θ)을 Minimize하기!
(물론, 고차원 Space에서는 아래와 같은 Convex function이 아니라 Horrible한 Non-convex cost일 때도 많음)
Update Rule
Code
while True: theta_grad = evaluate_gradient(J, corpus, theta) theta = theta - alpha * theta_grad
Stochastic Gradient Descent
- 한편, 한 번에 다 Update하는 것은 상당히 Ineffective
Solution: SGD 사용 (Windows를 반복적으로 sampling & update)
while True: window = sample_window(corpus) theta_grad = evaluate_gradienet(J, window, theta) theta = theta - alpha * theta_grad
(다만 mini-batch Gradient Descent가 Stochastic Gradient Descent보다 Effective 한 경우도 많음.)
(이유1. SGD보다 less noisy, 이유2. GPU사용시 Parallelization에 good)- SGD에도 문제점 존재.
- 각 window마다, 기껏해야 2m+1 단어를 갖기 때문에,
는 상당히 Sparse!
- 이 말인 즉슨, 실제로 나타나는 단어들에 대해서만 word vector를 업데이트하게 될 것이라는 것.
- Solution: Word vectors에 대한 hash 들고 다니기
Word2vec: More details
- 두 개의 벡터를 들고 다니는 이유?
- 최적화가 더 쉽다! (1과에 식 이미 정리)
- 두 가지 Variants
- Skip-grams
- Center world 1개로 주변 단어들 한 번에 예측하기
- Continuous Bag of Words (CBOW)
- 그 반대
- Training의 효율을 더하는 방법
- By Negative Sampling (이제껏 집중해온 방식은 naive softmax)
The skip-gram model with negative sampling
- 이제까지 써오던 Probability notation
는 속도면에서 상당히 좋지 않은 아이디어. (이 확률값 하나 계산하려고, 전체 vocab을 다 훑어야함) - Standard word2vec 대신 skip-gram model with negative sampling 을 생각해보자.
핵심 아이디어:
True Pair(center word와 context window내의 단어)와 Noise Pair(center word와 임의의 단어)에 대한 이진로지스틱회귀(Binary Logistic Regressions) 훈련- 목적 함수:
Maximize!
여기서 σ는 Sigmoid function
위 식을 조금 다르게 적어보면, 랜덤하게 뽑힌 K개의 단어에 대해, 다음과 같이 목적함수를 정리해볼 수 있음
목적: context window내의 단어들이 center word 부근에서 나타날 확률은 maximize하고, random word들이 center word 부근에 나타날 확률은 minimize하는 것
But why not capture co-occurrence counts directly?
- Co-occurrence matrix X를 구성할 수 있는 2가지 옵션
- Window의 관점
Full document의 관점
- Window-Based
- Word2Vec과 비슷함 (각 단어 주변에 window를 사용해서, syntatic, semantic information 둘다 caputure)
- Document-Based
Latent Semantic Analysis
를 가능케 하는, general topics를 내어놓을 것
Window-based co-occurrence matrix 예시
- window size 1 가정 (일반적으로는 5~10)
- Symmetric (왼쪽 context이든 오른쪽 context이든 상관 x)
- Example
- I like deep learning.
- I like NLP.
I enjoy flying.
Co-occurrence matrix
이렇게 작은 예만 봐도 별로 안 쓰고 싶음.
a. 단어의 크기가 증가하면?
b. 상당히 High-dimension (많은 저장곤간을 필요로)
c. 이후의 분류 모델들은 Sparsity 문제를 마주하게 될 것
=> Model은 그닥 Robust하지 않다!
Low dimensional vectors
High-dimension이 거슬린다면, 차원을 낮추어주면 되지 않냐는 사고.
(중요 정보들을 Fixed-sized의 작은 Dimension으로, Dense vector로 만들고 싶다.)- 방법1. SVD
- Full SVD
- Reduced SVD
- Full SVD에서 필요없는 항들을 없애(1) 차원을 줄인 후, 가장 작은 Singular value를 버린다(2).
- Reduced SVD를 통한 Estimation은 Least Square Error의 관점에서 original X에 대한 Best rank K estimator이다.
- 방법1. SVD
예시 코드
import numpy as np la = np.linalg words = ['I', 'like', 'enjoy', 'deep', 'learning', 'NLP', 'flying', '.'] X = np.array([[0, 2, 1, 0, 0, 0, 0, 0], [2, 0, 0, 1, 0, 1, 0, 0], [1, 0, 0, 0, 0, 0, 1, 0], [0, 1, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0, 1], [0, 0, 0, 0, 1, 1, 1, 0]]) U, s, Vh = la.svd(X, full_matrices=False)
for i in xrange(len(words)): plt.text(U[i,0], U[i,1], words[i])
Hacks to X
- Count들을 Scaling 해주는 것이 많은 도움이 될 수 있다.
- 예를 들어, the, he, has와 같은 단어들은 상당히 자주 출현할텐데, 이러면 이 syntzx들이 너무 큰 영향을 갖게 됨
- 이럴 때는, raw count를 그대로 사용하는 대신에, 이렇게 자주 출현하는 단어들에 대한 전처리를 해주는 것이 필요
- ex1. log scale
ex2. ceiling function
- 교재에 표기 된 예
- 이런 전처리를 해주었더니, 흥미로운 semantic pattern들이 나타나는 경우도 존재
(Perfect하진 않더라도 일종의 direction이 생김)
“If you can construct a vector space that has this linearity property, then you are definitely gonna do well in analogy.”
Count based vs. direct prediction
- 기존의 NLP 두 가지 큰 방법론
The GloVe model of word vectors
- Count based와 Direct Prediction 둘을 섞으려는 시도
중요 Insight: “Co-occurrence 확률의 비 (Ratio)가 meaning component들을 encode할 수 있다”
예를 들어,
P(x|ice), x=solid -> Large : Ice라는 단어가 주어졌을 때, solid라는 단어가 같이 발생할 (co-occur)할 확률은 大
P(x|stream), x=solid -> Small : Stream이라는 단어가 주여졌을 때, solid라는 단어가 같이 발생할 (co-occur)할 확률은 小
: Ratio of co-occurrence prob.
↓ 실제로 측정을 해보았더니,
- 8.8, 8.5X10^-2: Dimension of meaning between solid & gas
1.36, 0.96: Not the dimension of meaning
- 바라는 것: Ratio of co-occurrence probability가 Word Vector Space에서 Linear한 관계를 가졌으면 좋겠다는 것.
- Q: 어떻게 Word vector space 내에서 ratios of co-occurrence probabilities가 linaer한 meaning components가 되게 할 수 있을까?!
A: Word vector들의 내적이 log(co-occurrence probability)와 같아지게 정의하면 두 벡터간의 차이를 Linear한 형태로 표기해볼 수 있다.
GloVe 모델의 Objective Function
Evaluating Word Vectors
- Word Vectors를 어떻게 평가할까?
- NLP의 일반적인 두 가지 평가기준:
Intrinsic
과Extrinsic
- Intrinsic
- 특정 Subtask에 대한 평가
- 연산이 빠름
- 해당 시스템을 이해하는 데에 도움
- 실제 Task와의 Correlation 관계가 확립되지 않았다면, 정말 실세계에서 정말 도움이 될지는 명확하지는 않음
- 연산이 빠름
- Extrinsic
- 실제 Task (Real task) 상에서의 평가
- Accuracy 계산에 긴 시간이 걸릴 수 있음
- Subsystem 상에 어떤 문제가 있는지에 대해 명확하게 알 수 없음
- 정확히 하나의 Subsystem을 다른 subsystem으로 교체했는데 accuracy가 향상된다면 -> 쩔어!
- Accuracy 계산에 긴 시간이 걸릴 수 있음
- 오늘 강의에서는 Intrinsic Evaluation 파트에 집중할 것.
Intrinsic Evaluation은 Vector Space addition 이후의 cosine distance가 얼마나 intuitive semantic & syntactic analogy questions를 잘 잡아내는지를 근거로 word vector를 평가
- GloVe 모델을 썼더니, Linear한 Property를 정말 갖고 있더라.
- 예