Transformer 계열의 언어모델에서의 contextualized embedding에 대해 분석한 논문을 정리하고자 한다(논문, 저자 블로그).
1. Introduction
기존의 static word embedding(ex. Word2Vec)은 문맥과 상관없이 단어에 대해 하나의 embedding만을 부여했다. 하지만 transformer 계열 모델들은 문맥을 반영한 embedding을 계산하게 되는데 저자는 문맥에 따라 embedding이 얼마나 차이나는지를 보고자 하였다.
'mouse'라고 하는 단어는 쥐, 컴퓨터기기 두 가지 의미가 있는데 1) 두 의미 각각에 대한 representation이 계산되거나(왼쪽 그림) 2) 아니면 문맥에 따라 매우 다른 representation이 계산(오른쪽 그림)될 수 있다.
2. Approach
Contextuality(문맥에 반영된 정도)는 무엇일까? 아래와 같이 두 문장이 있다고 하자.
- A panda dog runs
- A dog is trying to get bacon off its back
만약 vec(dog) == vec(dog)라면 이 representation은 문맥정보를 반영하지 않으며 vec(dog) != vec(dog)라면 어느 정도의 문맥정보를 담고있다고 볼 수 있다. 저자는 이 아이디어에서 출발해 Contextuality를 분석하기 위해 세 가지 척도를 제시하였다.
먼저 sentence, word, representation을 아래와 같이 정의한다.
- sentences: $\{ s_{1}, \cdots, s_{n} \}$
- words: 각 sentence에 존재하는 동일한 단어 집합($w = s_{1}[i_{1}] = \cdots = s_{n}[i_{n}]$)($\{ i_{1}, \cdots, i_{n} \}$는 각 sentence에서의 word index)
- representation $ f_{l}(s,i)$: 문장의 $i$번째 단어 $s[i]$에 대해 모델 $f$의 $l$번째 layer에서의 representation
1) Self-Similarity(SelfSim)
서로 다른 문장 $s$에 존재하는 동일 단어 $w$에 대한 layer $l$에서의 representation $f_{l}(s,i)$이 있을 때 SelfSim은 이들의 cosine similarity의 평균으로 정의한다.
$$\text{SelfSim}_{l}(w) = \frac{1}{n^{2}-n}\sum_{j}\sum_{k\neq j}cos(\ f_{l}(s_{j}, i_{j}), \ f_{l}(s_{k}, i_{k}) )$$
만약 representation이 문맥정보를 담지 않았다면 $\text{SelfSim}_{l}(w)=1$일 것이며, 각 문장에서의 문맥정보를 잘 담았다면 값이 낮을 것이다.
2) Intra-Sentence Similarity(IntraSim)
문장 $s=< w_{1}, \cdots, w_{n} >$가 있다고 할 때 IntraSim은 해당 문장에 존재하는 각 단어 $w_{i}$의 representation과 sentence representation의 cosine similarity의 평균으로 정의한다.
$$\begin{array}{rcl}
\text{IntraSim}_{l}(s) & = &\frac{1}{n}\sum_{i}cos(\ \overset{\to }{s_{l}}, \ f_{l}(s,i) ) \\
\overset{\to }{s_{l}} & = & \frac{1}{n}\sum_{i}f_{l}(s,i) \end{array}$$
IntraSim을 통해 context-specificity가 벡터 공간에서 어떻게 나타나는지를 볼 수 있다. 만약 $\text{SelfSim}_{l}(w), \ \text{IntraSim}_{l}(s)$이 모두 낮다면 모델이 각 단어에 대해 context-specific하며 문장 내 다른 단어들과도 구분되는 representation을 계산할 것이다. 만약 $\text{SelfSim}_{l}(w)$이 낮고 $\text{IntraSim}_{l}(s)$이 높다면 해당 문장 내 각 단어가 비슷한 곳에 위치되도록 하는 representation이 계산될 것이다.
3) Maximum Explainable Variance(MEV)
서로 다른 문장에 존재하는 동일 단어의 representation으로 구성된 occurrence matrix $[f_{l}(s_{1},i_{1}), \cdots , f_{l}(s_{n},i_{n})]$이 있고, $\sigma_{1}, \cdots, \sigma_{m}$을 이 행렬에서의 $m$개의 singular values라고 하자. 이때 MEV를 다음과 같이 정의한다.
$$\text{MEV}_{l}(w)=\frac{\sigma_{1}^{2}}{\sum_{i}\sigma_{i}^{2}}$$
MEV는 contextualized representation의 분산에서 1번째 principal component의 비율인데 이를 통해 분산의 정도를 알 수 있다. 만약 $\text{MEV}_{l}(w)$가 1에 가깝다면 context와 관계없는 static embedding이라고 볼 수 있다.
MEV: proportion of variance in its contextualized representations that can be explained by their first principal component.
3. Findings
1) (An)Isotropy
*Contextualized representations are anisotropic in all non-input layers.
단어의 representation이 isotropic(e.g. directionally uniform)하다면 cosine similarity의 평균이 0에 가까울 것이며 anisotropic하다면 1에 가까울 것이다. 기하학적으로 anisotropic한 representaion은 벡터 공간에서 좁은 cone에 분포해있는 모양이다. 위 그래프에서 모든 layer에 대해 0보다 큰 값임을 알 수 있다.
*Contextualized representations are generally more anisotropic in higher layers
위 그래프에서 layer index가 커지면서 랜덤한 두 단어의 cosine similarity가 증가하는 것을 볼 수 있으며 이는 contextualization 과정이 진행되면서 anisotropy가 증가하는 것이라고 볼 수 있다.
2) Context-Specificity
*Contextualized word representations are more context-specific in higher layers
Figure 2.의 각 레이어 별 SelfSim값을 보면 레이어가 커질 수록 값이 작아지는데 이는 상위 레이어가 하위 레이어보다 문맥정보를 더 많이 담고 있다는 것을 의미한다.
*Stopwords (e.g., ‘the’, ‘of’, ‘to’) have among the most context-specific representations
모든 레이어에서 the, of, to와 같은 단어들이 SelfSim값이 제일 낮았다.
3) Static vs Contextualized
*On average, less than 5% of the variance in a word’s contextualized representations can be explained by a static embedding
단어들의 분포가 anisotropic하기 때문에 첫번째 eigen vector의 principal component 값이 클 것이다. 때문에 이를 조정해서 MEV를 측정하면 최대 5%정도가 나오는데, 이는 context가 반영되면서 임베딩에 분포가 생긴다. 이를 통해 contextualizing 모델이 단순히 단어의 의미별로 임베딩을 만드는게 아니라 문맥 정보를 반영하고 있다는 것을 알 수 있다.
This suggests that contextualizing models are not simply assigning one of a finite number of word-sense representations to each word – otherwise, the proportion of variance explained would be much higher