ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Attention Is All You Need(2)
    NAVER AI TECH 2023. 6. 9. 10:22

    Self-Attention

    입력으로 다음과 같은 문장이 들어왔다고 해 봅시다. "I am a student" Tokenization과 Embedding을 거쳐 해당 문장은 다음과 같은 Embedding으로 변환됩니다. [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] (편의상 one-hot encoding을 적용하였습니다.) 이제 각 Embedding Word는 행렬곱을 통과해 Query, Key, Value를 만들게 됩니다. 즉, 'I'에 대한 Query, Key, Value, 'am'에 대한 Query, Key, Value, 'a'에 대한 Query, Key, Value, 'student'에 대한 Query, Key, Value가 생성되게 됩니다. Query, Key, Value가 동일한 단어에 근간을 두고 있기 때문에 Self-Attention이라는 용어가 사용되었습니다. 이후 Query는 Query끼리, Key는 Key끼리, Value는 Value끼리 이어붙여 줍니다. 마지막으로 아래 연산을 수행합니다.

    한 가지 유의할 점은 $\sqrt(d_k)$로 나누어준다는 점이다. 이는 $softmax$ 연산이 지수함수를 활용하기 때문이다. 아래는 $softmax$ 함수의 수식이다.  

    $\sqrt(d_k)$로 나누어주지 않으면 $\sqrt(d_k)$로 나누어주지 않을 때에 비해 큰 값은 더욱 큰 값으로 작은 값은 더욱 작은 값으로 변환되게 된다. 이로 인해 학습이 방해 받을 수 있다. 또한, $QK^T$의 값이 너무 커져 계산이 불안정해질 수 있다. 다음은 scaled dot product에 관해 설명해주는 논문 원문이다.

     

    While for small values of $d_k$ the two mechanisms perform similarly, additive attention outperforms dot product attention without scaling for larger values of $d_k$ . We suspect that for large values of dk, the dot products grow large in magnitude, pushing the softmax function into regions where it has extremely small gradients. To counteract this effect, we scale the dot products by $\frac{1}{\sqrt(d_k)}$.

     

    Multi-Head Attention

    Multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions. With a single attention head, averaging inhibits this.

     

    Multi-Head를 통해서 다양한 하위공간에 관한 표상을 얻을 수 있고, 이는 더욱 정확한 표상을 얻을 수 있는 근거가 된다. 한편, Multi-Head를 늘릴 수록 얻는 것은 무엇이고 잃는 것은 무엇인지도 고민해 보았다. Multi-Head를 늘릴 경우 더욱 다양한 하위공간에 관한 표상을 얻을 수 있지만 하나의 Head가 공간을 정확히 표상하는 하위공간을 표상할 확률이 줄어들 거라는 생각이 들었다.

     

    Questions

    Transformer 모델의 파라미터 개수 구하기

    참고자료: Estimate the Number of Parameters in Transformer models

    d_model = 512

    d_ff = 2048

     

    1. Multi-Head Attention

    $W^Q$, $W^K$, $W^V$ 각각이 d_model * d_model + d_model

    $W^O$의 경우 d_model * d_model + d_model

    즉 4 * (d_model * d_model + d_model)

     

    2. Feed-forward Network

    MLP --> ReLU --> MLP 순서대로 진행

    d_model * d_ff + d_ff --> ReLU --> d_ff * d_model + d_model

    즉, 2 * (d_model * d_ff) + d_model + d_ff

     

    3. Layer Normalization

    Layer Normalization은 beta와 gamma 두 값을 통해 이루어진다.

    따라서 2 * d_model

     

    A. Encoder

    Encoder는 one attention block + one feed-forward net + two layer normalizations로 이루어져 있다.

    1 * (4 * (d_model * d_model + d_model)) +

    1 * (2 * (d_model * d_ff) + d_model + d_ff) +

    2 * (2 * d_model)

     

    B. Decoder

    Decoder는 two attention blocks + one feed-forward net + three layer normalizations로 이루어져 있다.

    2 * (4 * (d_model * d_model + d_model)) +

    1 * (2 * (d_model * d_ff) + d_model + d_ff) +

    3 * (2 * d_model)

     

    따라서 두 값을 더하면

    3 * (4 * (d_model * d_model + d_model)) +

    2 * (2 * (d_model * d_ff) + d_model + d_ff) +

    5 * (2 * d_model)

     

    Encoder와 Decoder는 6개씩 있으므로

    6 * (3 * (4 * (d_model * d_model + d_model)) +

           2 * (2 * (d_model * d_ff) + d_model + d_ff) +

           5 * (2 * d_model))

     

    여기에 처음 Embedding Layer의 파라미터 개수와 마지막 Linear Layer의 파라미터 개수를 더해주면 된다.

    Embedding Layer: Input Vector * d_model

    Linear Layer: d_model * Ouput Vector

     

    Transformer 모델의 장점을 기술하라

    1. 병렬화가 가능하다. (vs RNN 계열의 모델)

    2. 계산 속도가 빠르다. (vs RNN 계열의 모델)

    3. 정보의 손실이 적다. (vs RNN 계열의 모델)

     

     

     

    'NAVER AI TECH' 카테고리의 다른 글

    Learning Dense Representations of Phrases at Scale  (0) 2023.07.04
    BERT  (0) 2023.06.29
    NextRise 2023, Seoul 회고록  (0) 2023.06.04
    CleanLab  (0) 2023.06.04
    모델 성능 향상을 위한 데이터 처리 방법  (0) 2023.05.25

    댓글

Designed by Tistory.