본문 바로가기

boostcamp AI tech/boostcamp AI

Word Embedding - Word2Vec, GloVe

728x90

단어를 vector로 나타낼 때, 서로 비슷한 cat, kitty는 벡터의 차원 공간에 두 점으로 나타낼 수 있다. 그리고 이 두 단어의 거리는 burger에 비해 서로 가까워야 할 것이다.

 

When we define a word into a vector. we can understand a word as a point at the N-dimension space. Then we can calculate the distance between words. 

Let's say there are two words in the N-d space, 'cat' and 'kitty'. Distance between these two points should be closer than it from the word 'burger'.

1. Word2Vec

"The cat hunts mice"

We assume that the words in similar context will have similar meaning

 

meow, potato, Korea, pet, mice 와 같은 단어들이 있을 때, P(w | 'cat') 의 분포를 모델이 학습하도록 한다.

sentence : "I study english"
vocab : "I", "study", "english"

위 같은 문장이 있을 때, sliding window기법을 활용하여 (input,output) 쌍을 만든다. window크기가 3이라고 했을 때,

(I, study) (study, I) (study, english), (english, study)가 만들어진다. (특징, 레이블) 쌍과 같은 학습데이터를 만든셈이다.

http://mohitmayank.com/a_lazy_data_science_guide/natural_language_processing/word2vec/

example input : study = [0,1,0]

example output : english = [0,0,1]

input, output dimension을 3, embedding dimension을 2라고 가정했을 때, 

x = study [0,1,0] (3,1)
W (2,3)
W' (3,2)

output, prediction 
= softmax(W' W x) = [p1, p2, p3]

label, y = english [0,0,1] (3,1)과 prediction의 loss를 최소화 하는 방향으로 학습
*두 단어가 서로 가깝도록 학습

Wx를 수행할 때, x는 one-hot-encoding vector이므로 [0,1,0] 처럼 1이 있는 index와 연산되는 W의 column만 가져오는 것과 같다. (실제로 연산량이 많은 행렬 계산이 아니라 이렇게 1이 있는 index의 column만 W에서 뽑아온다.)

 

아무튼 근접한 단어끼리 비슷한 의미를 지닐 것이라는 가정하에 W와 W'을 학습했다. 근접한 단어들 끼리의 cosine유사도는 클 것이고 embedding 했을 때 유사한 값을 가질 것이다. 

http://mohitmayank.com/a_lazy_data_science_guide/natural_language_processing/word2vec/

input vector가 W.T에 해당하고 output vector가 W'에 해당한다. 둘 중 아무거나 embedding matrix로 사용해도 되지만 통상적으로 W.T, input vector를 많이 사용한다.

apple = [1, 0, 0, 0, 0, 0, 0, 0]이므로 첫번째 row가 apple의 embedding vector가 된다.

 

이렇게 단어가 의미를 내포하는 vector로 변환되고나면 vector끼리의 연산도 가능해진다.

한국-서울+도쿄 = 일본

이렇게 단어들의 의미론적 관계를 word2vec이 학습하는 것이다.

math shopping reading science

word embedding이 사용될 수 있는 task를 하나 더 알아보자. 위 같은 문장이 있을 때, 제일 관련없는 단어 하나를 찾아라 라는 task가 있다면 (intrusion detection) embedding vector간의 거리를 활용할 수 있다.

shopping - reading, science, math 거리의 평균이 가장 크므로 제일 관련이 없다고 볼 수 있다.

 

이렇게 word의 vector representation은 NLP의 많은 task에서 활용될 수 있다.

 

2. GloVe

word2vec같은 word embedding의 방법이다.

예를들어 word2vec에서 (math, english)가 쌍으로 자주 묶일 수록 반복 계산되어 자연스럽게 둘 사이의 유사도(내적)는 커질 것이다.

https://wikidocs.net/22885

glove에서는 두 단어가 window내에 함께 등장하는 횟수를 미리 계산해둔다. Pij

input word의 embedding vector, ui와 output word의 embedding vector vj의 내적값이 logPij와 가까워지도록 loss function을 정의한다. 이렇게 하면 word2vec의 중복되는 계산을 줄일 수 있다.

https://wikidocs.net/22885

f(Pij)의 곱으로 Loss function이 완성된다.

the같은 단어는 굉장히 많은 단어랑 함께 출현할 수 있다. 이렇게 빈도가 큰 단어쌍이 학습을 과도하게 지배하는 것을 막을 수 있다. 빈도수에 따른 가중치를 주는 함수로서 작동한다.

 

단어들간의 관계가 유사한 방향과 크기를 나타냄을 살펴볼 수 있다.

728x90
반응형

'boostcamp AI tech > boostcamp AI' 카테고리의 다른 글

Seq2Seq with attention  (1) 2023.11.30
Long-Short Term Memory, GRU  (0) 2023.11.29
Naive-Bayse Classifier  (1) 2023.11.27
Short history of Generative model  (0) 2023.11.24
Transformer  (2) 2023.11.23