transformer의 decoder 구조 알아보기

decoder의 기본 구조는 이렇다.

 

 

The decoder is also composed of a stack of N = 6 identical layers.

 

In addition to the two sub-layers in each encoder layer, the decoder inserts a third sub-layer, which performs multi-head attention over the output of the encoder stack.

 

Similar to the encoder, we employ residual connections around each of the sub-layers, followed by layer normalization.

 

We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions. This masking, combined with fact that the output embeddings are offset by one position, ensures that the predictions for position i can depend only on the known outputs at positions less than i.

 

decoder는 처음 decoder input sequence를 받아 positional encoding을 하고 첫 layer에서는 masked multi head attention을 수행한다.

 

논문에서도 나와있지만 기본적으로 masked multi head attention이 있는 이유는 decoder의 예측이 오직 이전 단어에만 영향을 받았으면 해서 그렇다.

 

masked multi head attention이 decoder의 self attention layer이고 이후 residual connection, layer normalization을 거쳐 query를 만들어낸다.

 

이 query는 decoder의 두번째 layer인 encoder-decoder multi head attention으로 들어간다.

 

이름에서도 알수있다시피 key와 value는 encoder의 최종 output vector로부터 변형되어 넘어온다.

 

모든 decoder stack에 동일하게 encoder의 최종 output vector에서 변형된 key,value가 넘어온다.

 

이후 residual connection,layer normalization을 거치고 feed forward neural network를 거친 뒤 다시  residual connection, layer normalization을 거친다.

 

decoder도 당연히 여러개의 stack을 만들 수 있으며 각각은 동일한 구조를 가졌지만 다른 가중치를 가진다.

 

여러 decoder의 stack을 지난 최종 output이 decoding의 hidden state vector

 

이것을 output layer에 넣어 vocab size로 변형하고 그것을 softmax를 취하면 vocab 별 확률분포를 얻으며 이들 중 가장 높은 확률을 보이는 단어를 생성하게 된다.

 

 

TAGS.

Comments