크크루쿠쿠

Transformer: Attention is all you need 본문

DeepLearning/부스트캠프 AI Tech

Transformer: Attention is all you need

JH_KIM 2021. 8. 12. 12:06

Transformer

 

Sequential Model

What makes sequential moedling a hard problem to handle?

이러한 다른 sequence 를 다루기 힘들어짐.

 

Transformer

Transformer is the first sequence transduction model based entirely on attention.

-> recurrent한 구조X, attention 이라는 구조 활용

 

기계어 번역 뿐만 아니라 이미지 분류에도 활용될정도로 많은 활용가치가 있음.

model 자체는 하나의 model이다.

-> 몇개의 단어가 들어가든 한번에 처리함.

 

- Encoder

The Self-Attention in both encoder and decoder is the cornerstone of Transformer

Self-Attention에 넣기 위해서  n개의 단어가 들어오면 n개의 vector를 만들어줌

 

-> 통과하면서 모든 vector를 고려해준다.

feed forward 부분은 dependecies X

 

--> 다른 단어와의 관계를 확인해줌.

 

Example) Thinking machines

단어마다 Q,K,V 라는 3가지 Vector를 생성해준다.

 

Self-Attention 과정

Thinking으로 예시

1. Scor

encoding 하고자 하는 vector의  query vector와 남은 모든 단어들의 key vector를 내적해준다.

-> i번째 단어가 나머지 단어들사이에서 얼만나 interaction 해야하는지 보여줌.

 

2. Normalize

key(query) vector의 dimension의 square 수만큼 나눠줌.

 

3. Attention weight

그 뒤에 Softmax를 취해준다.

 

4. Softmax X values

마지막으로 구한 weighted value vector를 모두 더해줌.

 

이렇게 구한 최종적인 결과인 z1 이 embedding vector의 encoding vector가 나옴

 

위의 과정을 요약하면 이렇게 된다.

 

왜 잘될까?

input이 fix되어있으면 ouput도 fix 되어있다.

transformer 같은 경우에는 하나의 input이 fix되어있더라도 encoding할려는 단어가 다른 단어에 따라 결과가 달라짐.

ouput이 fix가 안된다는것. -> 훨씬 더 많이 표현 할 수있다. -> 많은 computing 필요

computational cost (N^2) 때문에 한번에 처리할 수 있는 길이가 제한되어있지만 flexible 하고 더 많은 표현력 덕분에 좋음.

 

MHA(Multi Head Attention)

->  하나의 입력에 대해서 Q,K,V 를 여러개 만드는것.

차원은 어떻게 맞추지?

 

 

총 과정

 

-> 실제 구현에서는 이렇게 구현 X

실제로는 쪼개서 Attention함

 

Position Encoding

 

Encoding되는 값에서는 순서가 고려가 안됨.

-> 순서를 고려할수 있게끔 position vector를 더해줌.

 

- Decoder

Transformer transfers key(K) and value(V) of the topmost encoder to the decoder

 

In the decoder, the self-attention layer is only allowed to attend to earlier positions in the output sequence which is done by masking future positions before the softmax step.

학습시 input output 출력 정답은 이미 알고 있기 때문에 단어를 만드는 중 모든 단어를 알고있음 학습의 의미X -> masking 사용

이전 단어들만 dependent 하게끔 -> 미래의 정보는 사용하지 않겠다.

 

Encoder-Decoder Attention

지금 decoder에 generation 한것으로 만으로만 query를 만들고 K,V는 encoder에서 나오는 vector를 사용.

-> 방식은 encoder와 똑같다.

The final layer converts the stacck of decoder outputs to the distribution over words.

하나하나 sampling.

 

Vision Transformer

 

Encoder만을 활용해서 이미지 분류

문장과 달리 sequence 대신 이미지를 나눠서 하나의 입력처럼 넣어줌

position embedding도 필요하다.

 

DALL -E

문장 -> 이미지

얘는 decoder만 사용함.

이미지, 문장 둘다 학습후 조합해서 만든것.

'DeepLearning > 부스트캠프 AI Tech' 카테고리의 다른 글

Pytorch Basics  (0) 2021.08.17
Generative Models  (0) 2021.08.13
RNN(Recurrent Neural Network)  (0) 2021.08.12
[Week2] DL CNN(Convolutional Neural Network) [Day3]  (0) 2021.08.11
[Week2] DL Basic Optimization[Day2]  (0) 2021.08.10
Comments