transformer에 사용된 positional encoding에 대하여

1. block based model

 

encoding의 마지막 단계로 residual connection, layer normalization 결과를 feed forward network라는 신경망에 넣어 다시 한번 변환을 거친다.

 

근데 특별히 왜 했다는 이유는 없는듯??

 

 

feed forward network후에도 residual connection을 수행하고 layer normalization을 수행하여 최종적인 encoder의 output인 hidden vector를 얻는다.

 

 

2. positional encoding

 

지금까지 연산의 결과는 사실 sequence의 순서 정보를 전혀 고려하지않았다.

 

직관적으로 당연하다.

 

x1,x2,x3의 q,k,v를 한번에 만들어서 이것으로 attention을 수행하면서 encoding을 하니까

 

i,go,home 순으로 넣으나 home, go, i순으로 넣으나 i에 대응하는 attention output vector는 동일하게 나온다

 

순서를 딱히 고려하지 않고 i와 {i,go,home} or {home,go,i}이라는 set과의 내적을 수행했다.

 

그러니까 입력을 sequence보다는 set으로 취급하였다는 점이다.

 

Since our model contains no recurrence and no convolution, in order for the model to make use of the order of the sequence, we must inject some information about the relative or absolute position of the tokens in the sequence.

 

반영하지 않을수는 없는 것이 sequence의 순서정보는 매우 중요하다.

 

RNN은 입력 sequence의 순서대로 처리한다는 점을 기억하라.

 

이를 위해 sequence의 (pos)번째 input word embedding에 (pos)번째 등장했다는 정보를 나타내는 positional encoding 벡터를 더했다.

 

이들은 pos에 따라 모두 다른 유일한 상수벡터이다.

 

positional encoding은 encoding의 최하단, 첫 입력에서만 수행한다.

 

To this end, we add "positional encodings" to the input embeddings at the bottoms of the encoder and decoder stacks. The positional encodings have the same dimension $d_{model}$ as the embeddings, so that the two can be summed.

 

positional encoding은 첫 입력에서만 수행한다는 그림

 

 

논문의 저자들은 어떤 함수를 사용했을까? sin과 cos의 함수를 사용했다

 

 

pos는 position으로 sequence의 입력 위치겠지? 첫번째 단어는 1이고 두번째 단어는 2이고

 

i는 dimension이다. 이 dimension이 무엇이냐면 embedding vector의 각 성분의 위치를 말한다.

 

무슨 말이냐면 embedding vector의 결과 (1,3,4)가 나왔다면 i=3이 아니라 1의 dimension은 1 3의 dimension은 2 4의 dimension은 3이라는 소리다.

 

$d_{model}$은 input sequence의 총 길이

 

 

position이 10인, 10번째 단어의 positional encoding vector는 dim=1인 곳이 d이고 dim=2인 곳이 e이고 dim=3인 곳이 f니까 (d,e,f)이다

 

position이 20인, 20번째 단어의 positional encoding vector는 dim=1인 곳이 a이고 dim=2인 곳이 b이고 dim=3인 곳이 c니까 (a,b,c)이다.

 

저자들이 이 함수를 쓴 이유는 주기를 잘 표현할 수 있다고 생각했나봄

 

 

 

주기를 잘 표현할 수 있다면 다른 함수를 써도 상관없다. 다른 함수도 실험해봤다는 내용이 있는듯?

 

 

128차원의 word embedding vector의 positional encoding vector를 패턴별로 나타낸 그림

 

빨간색으로 친 가로 각 1줄이 sequence의 position 별 positional encoding vector

 

위에서부터 0번~~~10번~~20번~~~~

 

 

3. transformer encoding 과정

 

i go home 각각을 embedding 하고 positional encoding을 한 뒤에 encoder에 넣는다.

 

encoder는 여러번 쌓을 수 있다.

 

encoder는 동일한 block based model을 가지지만 encoder 각각은 parameter가 모두 서로 다르다.(절대 같지 않다 주의)

 

반복해서 쌓아올린 encoder의 최상단부로 나온 벡터가 바로 i go home의 최종 encoding vector가 된다. 

 

 

TAGS.

Comments