쉬엄쉬엄블로그

(NLP) Seq2Seq 본문

부스트캠프 AI Tech 4기

(NLP) Seq2Seq

쉬엄쉬엄블로그 2023. 6. 22. 14:12
728x90

이 색깔은 주석이라 무시하셔도 됩니다.

Encoder-decoder architecture, Attention mechanism

Seq2Seq Model

Sequence to sequence learning with neural networks, ICML’14

  • It takes a sequence of words as input and gives a sequence of words as output
    • Seq2Seq 모델은 단어들의 시퀀스를 입력으로 사용하고 단어들의 시퀀스를 출력으로 제공한다.
  • It composed of an encoder and a decoder
    • Seq2Seq2 모델은 encoder와 decoder로 구성되어 있다.

Seq2Seq Model with Attention

  • Attention provides a solution to the bottleneck problem
    • Attention은 bottleneck 문제에 대한 해결책을 제공한다.
  • Core idea: At each time step of the decoder, focus on a particular part of the source sequence
    • decoder의 각 time step마다 source sequence의 특정 부분에 집중하도록 한다.

https://google.github.io/seq2seq/

  • Use the attention distribution to take a weighted sum of the encoder hidden states
    • encoder hidden states의 가중치 합을 얻기 위해 attention 분포를 사용한다.
  • The attention output mostly contains information the hidden states that received high attention
    • attention 출력에는 대부분 높은 attention을 얻은 hidden states 정보가 포함된다.
  • Concatenate attention output with decoder hidden state, then use to compute $\hat {y_1}$ as before
    • decoder hidden state의 합쳐진 attention 출력은 이전처럼 $\hat {y_1}$을 계산하는데 사용된다.


Different Attention Mechanism

  • Luong attention
    • they get the decoder hidden state at time 𝑡, then calculate attention scores, and from that get the context vector which will be concatenated with hidden state of the decoder and then predict the output.
      • 시간 $t$에서 decoder의 hidden state를 얻은 다음 attention scores를 계산하고, 그로부터 decoder의 hidden state와 concat될 context vector를 얻은 후 output을 예측한다.

Effective Approaches to Attention based Neural Machine Translation, EMNLP 2015

  • Bahdanau attention

    • At time 𝑡, we consider the hidden state of the decoder at time 𝑡 - 1. Then we calculate the alignment, context vectors as above. But then we concatenate this context with hidden state of the decoder at time 𝑡 - 1. So before the softmax, this concatenated vector goes inside a LSTM unit.
      • 시간 $t$에서, 시간 $t-1$에서의 decoder의 hidden state를 고려한다. 그 후 위 그림과 같이 alignment(선형?, 정렬?), context vector를 계산한다. 그런 다음 이 context를 시간 $t-1$에 decoder의 hidden state와 concat한다. softmax를 적용하기 이전에, 이 concat된 vector는 LSTM unit으로 들어가게 된다.
  • Luong has different types of alignments. Bahdanau has only a concat score alignment model.

    • Luong에는 여러 가지 유형의 alignments가 있고 Bahdanau는 concat socre alignment model만 있다.
  • attention은 다양한 방식으로 유사도를 구하는 방법들이 확장됨

  • 이를 필요한 attention 모듈에 적절히 사용함

  • context vector : 입력 문장의 특정 단어에 집중하는 데 사용되는 가중치 정보를 담고 있는 벡터, 각 단어의 임베딩 벡터와 해당 단어에 대한 attention 가중치를 곱하여 가중합을 계산하면 context vector가 생성됨

  • attention 가중치 : encoder의 출력과 decoder의 입력의 유사도를 구하기 위해 내적을 하고 내적한 값들은 정규화하기 위해 softmax를 취한 값

  • 예시

    • encoder 출력 : [$h_1, h_2, h_3, h_4$]
    • decoder 입력 : [$d_1, d_2$]
    • $내적_{11}$ = dot_product($h_1, d_1$) = $h_1 * d_1$
    • $내적_{21}$ = dot_product($h_2, d_1$) = $h_2 * d_1$
    • $내적_{31}$ = dot_product($h_3, d_1$) = $h_3 * d_1$
    • $내적_{41}$ = dot_product($h_4, d_1$) = $h_4 * d_1$
    • $내적_{12}$ = dot_product($h_1, d_2$) = $h_1 * d_2$
    • $내적_{22}$ = dot_product($h_2, d_2$) = $h_2 * d_2$
    • $내적_{32}$ = dot_product($h_3, d_2$) = $h_3 * d_2$
    • $내적_{42}$ = dot_product($h_4, d_2$) = $h_4 * d_2$
    • $attention\ weight_1$ = softmax($내적_{11}, 내적_{21}, 내적_{31}, 내적_{41}$) = [$w_{11}, w_{21}, w_{31}, w_{41}$]
    • $attention\ weight_2$ = softmax($내적_{12}, 내적_{22}, 내적_{32}, 내적_{42}$) = [$w_{12}, w_{22}, w_{32}, w_{42}$]
    • $context\ vector_1$ = ($w_{11} * h_1$) + ($w_{21} * h_2$) + ($w_{31} * h_3$) + ($w_{41} * h_4$)
    • $context\ vector_2$ = ($w_{12} * h_1$) + ($w_{22} * h_2$) + ($w_{32} * h_3$) + ($w_{42} * h_4$)
    • matrix 연산을 하면 쉽게 계산됨

Attention is Great!

  • Attention significantly improves NMT performance
    • Attention은 NMT 성능을 상당히 개선시켰다.
    • It is useful to allow the decoder to focus on particular parts of the source
      • decoder가 특정 source 부분에 집중하도록 하는 것이 매우 유용하다.
  • Attention solves the bottleneck problem
    • Attention은 bottleneck 문제를 해결한다.
    • Attention allows the decoder to look directly at source; bypass the bottleneck
      • Attention은 bottleneck 문제를 우회하여 decoder가 source 데이터를 직접 볼 수 있도록 만들어준다.
  • Attention helps with vanishing gradient problem.
    • Attention은 vanishing gradient 문제에 도움이 된다.
    • Provides a shortcut to far-away states
      • 먼 거리의 states를 위한 지름길을 제공한다.
    • attention을 통해 정보의 지름길을 거쳐서 원하는 encoder의 특정 time step의 hidden state vector까지 gradient를 빠르고 큰 변질없이 전달 가능
  • Attention provides some interpretability
    • Attention은 약간의 해석 가능성을 제공한다.
    • By instpecting attention distribution, we can see what the decoder was focusing on
      • attention 분포를 조사함으로써, decoder가 무엇에 집중하는지 알 수 있다.
    • The network just learned alignment by itself
      • 네트워크가 스스로 alignment을 학습했다.

Attention Examples in Machine Translation

  • It properly learns grammatical orders of words
    • Attention은 문법적인 단어의 순서를 적절하게 배운다.
  • It skips unnecessary words such as an article
    • Attention은 기사와 같은 불필요한 단어를 생략한다.

출처: 부스트캠프 AI Tech 4기(NAVER Connect Foundation)

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

(Data Viz) Seaborn 소개  (0) 2023.06.24
(NLP) Beam Search와 BLEU Score  (0) 2023.06.23
(NLP) LSTM과 GRU  (0) 2023.06.21
(NLP) Basics of Recurrent Neural Network  (0) 2023.06.20
(NLP) Word Embedding  (0) 2023.06.19
Comments