순서:
(1) LSTM
(2) seq2seq, +attention
(3) Show, Attend and Tell
Reference: Visualization of seq2seqmodel
seq2seq
[1] Sutskever, Ilya, Oriol Vinyals, and Quoc V. Le. "Sequence to sequence learning with neural networks." arXiv preprint arXiv:1409.3215 (2014).
[2] Cho, Kyunghyun, et al. "Learning phrase representations using RNN encoder-decoder for statistical machine translation." arXiv preprint arXiv:1406.1078 (2014).
말 그대로 sequence로 입력을 받아 sequence로 출력한다.
input 길이와 output 길이의 제한이 없어 유용하다.
기본적으로 Encoder-Decoder 구조를 가진다.
Encoder이 input으로 context vector을 추출하고,
Decoder은 context를 받아 output을 생성한다.
Encoder과 Decoder은 각각 RNN으로 구성된다.
Encoder의 각 cell들은 input과 이전 cell의 hidden state를 받아 다음 hidden state를 출력하고,
Decoder의 각 cell들은 이전 cell의 hidden state와 output을 받아 output과 다음 hidden state를 출력한다.
Decoder의 첫 cell은 context vector과 <sos> 토큰을 입력으로 받는다.
seq2seq + attention
[3] Bahdanau, Dzmitry, Kyunghyun Cho, and Yoshua Bengio. "Neural machine translation by jointly learning to align and translate." arXiv preprint arXiv:1409.0473 (2014).
[4] Luong, Minh-Thang, Hieu Pham, and Christopher D. Manning. "Effective approaches to attention-based neural machine translation." arXiv preprint arXiv:1508.04025 (2015).
- seq2seq의 context vector이 bottleneck으로 작용한다. 이에 long sentence에 제대로 동작하지 못한다는 단점이 있다.
- seq2seq에 attention 개념을 적용하여 이러한 문제를 해결한다.
- Attention은 다음과 같이 표현된다.
Attention(Q, K, V) : 주어진 Query Q에 대해 모든 Key K와의 Score value V를 구하고, 이를 모두 더해서 return한다.
- 마지막 hidden vector(=context vector)을 decoder에 전달하는 seq2seq와 달리, 모든 cell의 hidden state를 decoder에 전달한다.
Decoder의 각 cell들은 forward 과정 이후 Encoder의 hidden state들을 이용해 다음의 추가적인 작업을 거친다.
1. Encoder에서 모든 hidden state를 받는다.
2. 각 Encoder hidden state에 현재 시점의 Decoder hidden state에 대한 score을 부여하고, softmax를 취한다. => Attention
- Score은 현재 시점의 Decoder hidden state에 대한 각 Encoder hidden state의 중요도를 의미한다.
- 즉, 각 Encoder hidden state의 score은 해당 input에 대한 weight로 작용하는 것.
- 매번 Decoder output을 계산할 때마다 Encoder의 모든 시점의 hidden state를 고려하는 것이다.
- Softmax를 취한 score 값들을 Attention distribution이라고 한다.
* Scoring을 하는 방법은 여러 가지가 있을 수 있는데, 간단한 방법은 decoder hidden state와 encoder hidden state를 dot product 하는 것.
incredible.ai/nlp/2020/02/20/Sequence-To-Sequence-with-Attention/ 에서 보다 자세한 설명과 예시를 확인할 수 있다.
* 혹은, decoder hidden state와 encoder hidden state를 neural network에 넣어 score을 계산할 수도 있다. [4]
* lilianweng.github.io/lil-log/2018/06/24/attention-attention.html#whats-wrong-with-seq2seq-model에서 다양한 attention의 종류를 표로 정리해 놓았다.
3. 위에서 얻은 (softmax) score과 Encoder hidden state를 곱한 후 모든 값을 더해 하나의 vector을 만든다.
이는 중요한 hidden state는 강조하고, 중요하지 않은 hidden state는 줄이는 것이다. 이것이 attention의 개념이다.
이것이 각 decoder cell의 context vector이 되는것.
여기까지의 과정을 수식으로 정리하면 다음과 같다.
4. context vector과 Decoder hidden state를 concat 후 Dense layer에 통과시켜 output을 출력한다.
다시 말해, 각 cell에서는
1) 이전 hidden state와 이전 output을 이용해 새로운 hidden state를 계산하고,
2) Encoder hidden state set을 이용해 context vector을 계산하고,
3) 새로운 hidden state와 context vector을 이용해 output를 구하는 과정을 각각 거치는 것이다.
[참고자료]
구현에 도움이 될 만한 글들.
'🌌 Deep Learning > Overview' 카테고리의 다른 글
[StyleGAN 시리즈] ProGAN/PGGAN, StyleGAN, StyleGAN2 (0) | 2022.08.19 |
---|---|
[GAN Overview] GAN 주요 모델 정리 (GAN survey 논문 리뷰) (0) | 2022.01.23 |
[Overview] Attention 정리 - (1) LSTM (0) | 2021.01.26 |
[Overview] YOLO 계열 Object Detection 정리 - (1) YOLO (0) | 2021.01.19 |
[Overview] R-CNN 계열 Object Detection 정리 (Two-stage detector) (0) | 2021.01.08 |