26. Attention Mechanism and Self-Attention
Sequence-to-Sequence Models
Focusing on neural machine translation
Machine Translation
- Machine Translation (MT)
- Source language์ ๋ฌธ์ฅ ๋ฅผ target language์ ๋ฌธ์ฅ ๋ก ๋ฒ์ญํ๋ task
- The early history of MT: 1950s
- "A.I."๋ผ๋ ์ฉ์ด๊ฐ ๋ง๋ค์ด์ง๊ธฐ ์ ์ธ 1950๋ ๋ ์ด๋ฐ์ ์์
- ์ฃผ๋ก ๋จ์ด ์นํ์ ์ํํ๋ ๋จ์ํ ๊ท์น ๊ธฐ๋ฐ ์์คํ
- ์์ฐ์ด syntax, semantics, pragmatics(ํ์ฉ๋ก )์ ๋ํ ์ดํด ๋ถ์กฑ
Neural Machine Translation
- Neural Machine Translation (NMT)
- ํ๋์ end-to-end Neural Network๋ก machine Translation์ ์ํํ๋ ๋ฐฉ๋ฒ
- ํด๋น neural Network ์ํคํ ์ฒ๋ sequence-to-sequence model (a.k.a seq2seq)์ด๋ผ ๋ถ๋ฆฌ๋ฉฐ ๋ ๊ฐ์ RNN์ ํฌํจ

Sequence-to-Sequence is Versatile!
- Encoder-Decoder model์ด๋ผ๋ ์ผ๋ฐ์ ์ธ ๊ฐ๋
- ํ๋์ neural network๋ ์ ๋ ฅ์ ๋ฐ์ neural representation์ ์์ฑ
- ๋ค๋ฅธ network๋ ํด๋น neural representation์ ๊ธฐ๋ฐ์ผ๋ก ์ถ๋ ฅ์ ์์ฑ
- ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ด sequence์ธ ๊ฒฝ์ฐ ์ด๋ฅผ Seq2seq model์ด๋ผ ์นญํจ
- Sequence-to-sequence๋ MT(Machine Translation) ์ธ์๋ ๋ค์ํ nLP(Natural Language Processing) Task์ ์ ์ฉ
- Summarization (Long text โ Short text)
- Dialogue (Previous utterances(๋ฐํ) โ Next utterance)
- Code generation (Natural language โ Python code)
Neural Machine Translation (NMT)
- Sequence-to-sequence model์ Conditional Language Model์ ํ ์์
- Decoder๊ฐ target sentence ์ ๋ค์ ๋จ์ด๋ฅผ ์์ธกํ๋ฏ๋ก language model์ด๋ผ ํ ์ ์์.
- ์์ธก์ด source sentence ์ ์กฐ๊ฑด์ ๋ฐ๊ธฐ(Conditioned) ๋๋ฌธ์ conditional์ด๋ผ ํ ์ ์์.
- NMT๋ ๋ฅผ ์ง์ ๊ณ์ฐ
- MT System ํ์ต ๋ฐฉ๋ฒ
- ๋๊ท๋ชจ parallel corpus ํ๋ณด
- Unsupervised NMT, data augmentation ๋ฑ์ ๋ํ ํฅ๋ฏธ๋ก์ด ์ฐ๊ตฌ ์กด์ฌ
Training a Neural Machine Translation System

Introduction to Attention
The Bottleneck Problem of Seq2Seq

Attention
- Attention (mechanism)์ bottleneck ๋ฌธ์ ์ ๋ํ ํด๊ฒฐ์ฑ
์ ๊ณต
- Core idea: Decoder์ ๊ฐ ๋จ๊ณ์์ encoder์ ์ง์ ์ฐ๊ฒฐํ์ฌ source sequence์ ํน์ ๋ถ๋ถ์ ์ง์ค(Focus)
Sequence-to-Sequence with Attention





Attention: In Equations
- Procedure
- Encoder hidden states ๋ณด์
- Timestep ์์ decoder hidden state ๋ณด์
- ์ด ๋จ๊ณ์ attention scores ๊ณ์ฐ
- Softmax๋ฅผ ์ทจํด ์ด ๋จ๊ณ์ attention distribution ํ๋ (ํ๋ฅ ๋ถํฌ์ด๋ฉฐ ํฉ์ 1)
- ๋ฅผ ์ฌ์ฉํ์ฌ encoder hidden states์ weighted sum์ธ attention output ๊ณ์ฐ
- ๋ง์ง๋ง์ผ๋ก attention output ๋ฅผ decoder hidden state ์ concatenateํ๊ณ non-attention seq2seq model๊ณผ ๊ฐ์ด ์งํ
Attention is Great!
- Attention์ NMT ์ฑ๋ฅ์ ํฌ๊ฒ ํฅ์
- Decoder๊ฐ source์ ํน์ ๋ถ๋ถ์ ์ง์คํ๋๋ก ํ๋ ๊ฒ์ด ๋งค์ฐ ์ ์ฉ
- Attention์ MT ๊ณผ์ ์ ๋ํด ๋ "Human-like"ํ model์ ์ ๊ณต
- ์ ์ฒด๋ฅผ ๊ธฐ์ตํ ํ์ ์์ด ๋ฒ์ญํ๋ฉด์ source sentence๋ฅผ ๋ค์ ๋ณผ ์ ์์
- Attention์ bottleneck ๋ฌธ์ ํด๊ฒฐ
- Decoder๊ฐ source๋ฅผ ์ง์ ๋ณผ ์ ์๊ฒ ํ์ฌ bottleneck ์ฐํ
- Attention์ vanishing gradient ๋ฌธ์ ํด๊ฒฐ์ ๋์
- ๋ฉ๋ฆฌ ๋จ์ด์ง state๋ก์ shortcut ์ ๊ณต

- ๋ฉ๋ฆฌ ๋จ์ด์ง state๋ก์ shortcut ์ ๊ณต
- Attention์ ์ด๋ ์ ๋์ interpretability(ํด์ ๊ฐ๋ฅ์ฑ) ์ ๊ณต
- Attention distribution์ ๊ฒ์ฌํ์ฌ decoder๊ฐ ๋ฌด์์ ์ง์คํ๋์ง ํ์ธ ๊ฐ๋ฅ
- (Soft) Alignment๋ฅผ ๋ฌด๋ฃ๋ก ํ๋
- Alignment system์ ๋ช ์์ ์ผ๋ก ํ์ตํ์ง ์์์์๋ network๊ฐ ์ค์ค๋ก alignment๋ฅผ ํ์ตํ๋ค๋ ์ ์ด ํฅ๋ฏธ๋ก์
Attention is A General Deep Learning Technique
- Attention์ด machine Translation์ ์ํ sequence-to-sequence model์ ๊ฐ์ ํ๋ ํ๋ฅญํ ๋ฐฉ๋ฒ์์ ํ์ธ
- ๊ทธ๋ฌ๋, Seq2seq๋ฟ๋ง ์๋๋ผ ๋ค์ํ ์ํคํ ์ฒ์ MT ์ธ์ ๋ค์ํ ์์ ์์ attention ์ฌ์ฉ ๊ฐ๋ฅ
- Attention์ ๋ ์ผ๋ฐ์ ์ธ ์ ์
- Vector Values ์งํฉ๊ณผ vector Query๊ฐ ์ฃผ์ด์ก์ ๋, Attention์ Query์ ์์กดํ์ฌ Values์ weighted sum์ ๊ณ์ฐํ๋ ๊ธฐ๋ฒ
- ๋๋๋ก Query๊ฐ Values์ Attendํ๋ค๊ณ ํํ
- ์: Seq2seq + Attention model์์ ๊ฐ decoder hidden state (Query)๋ ๋ชจ๋ encoder hidden states (Values)์ attend
- ์ง๊ด
- Weighted sum์ Values์ ํฌํจ๋ ์ ๋ณด์ ์ ํ์ ์์ฝ(Selective summary)์ด๋ฉฐ, Query๊ฐ ์ด๋ค Values์ ์ง์คํ ์ง ๊ฒฐ์
- Attention์ ์์์ representations ์งํฉ(Values)์ผ๋ก๋ถํฐ ๋ค๋ฅธ representation(Query)์ ์์กดํ์ฌ fixed-size representation์ ์ป๋ ๋ฐฉ๋ฒ
- ๊ฒฐ๋ก
- Attention์ ๋ชจ๋ Deep Learning Model์์ pointer ๋ฐ memory ์กฐ์์ ์ํ ๊ฐ๋ ฅํ๊ณ ์ ์ฐํ๋ฉฐ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ด ๋จ.
- 2010๋ ์ดํ NMT๋ก๋ถํฐ ๋์จ ์๋ก์ด ์์ด๋์ด
From RNN to Attention-Based NLP Models Self-Attention
Self-Attention
As of Last Lecture: Recurrent Models for (Most) NLP

- 2016-2018๋
๊ฒฝ, NLP์ ์ฌ์ค์ ํ์ค ์ ๋ต์ ๋ฌธ์ฅ์ Bidirectional LSTM์ผ๋ก ์ธ์ฝ๋ฉํ๋ ๊ฒ
- ์: ๋ฒ์ญ์์์ source sentence
- ๊ทธ ํ, ์ถ๋ ฅ(Translation, Sentence, Summary)์ sequence๋ก ์ ์ํ๊ณ ์์ฑ์ ์ํด LSTM ์ฌ์ฉ
- ์ ์ฐํ memory ์ ๊ทผ์ ์ํด attention ์ฌ์ฉ
Same Goals, Different Building Blocks

- Sequence-to-sequence ๋ฌธ์ ์ Encoder-Decoder model์ ๋ํด ํ์ต
- ํ์ฌ๋ก์๋ ๋ฌธ์ ๋ฅผ ๋ฐ๋ผ๋ณด๋ ์์ ํ ์๋ก์ด ๋ฐฉ์์ ๋๊ธฐ๋ถ์ฌํ๋ ค๋ ๊ฒ์ด ์๋
- ๋์ model์ ์ ์ฉํ์ฌ ํญ๋์ ๋ฐ์ ์ ๊ฐ๋ฅํ๊ฒ ํ ์ต์์ Building Blocks๋ฅผ ์ฐพ์ผ๋ ค๋ ๊ฒ
Issues with Recurrent Models: Linear Interaction Distance
- RNN์ "Left-to-right"๋ก unrolled(์
๋ ฅ์ ๋ฐ์๋ค์ฌ ์ฒ๋ฆฌ)๋จ
- ์ด๋ Linear locality๋ฅผ ์ธ์ฝ๋ฉํ๋ฉฐ ์ ์ฉํ heuristic
- ๊ฐ๊น์ด ๋จ์ด๋ค์ ์ข ์ข ์๋ก์ ์๋ฏธ์ ์ํฅ์ ๋ฏธ์นจ
- Problem
- RNN์ ๋ฉ๋ฆฌ ๋จ์ด์ง ๋จ์ด ์์ด ์ํธ์์ฉํ๊ธฐ ์ํด ๋จ๊ณ๊ฐ ํ์
Issues with Recurrent Models: Linear Interaction Distance

- ๋ฉ๋ฆฌ ๋จ์ด์ง ๋จ์ด ์์ ์ํธ์์ฉ์ ๋จ๊ณ๊ฐ ํ์ํ๋ค๋ ์๋ฏธ
- Long-distance dependencies ํ์ต์ ์ด๋ ค์ (Gradient ๋ฌธ์ ๋๋ฌธ)
- ๋จ์ด์ linear order๊ฐ "Baked in" ๋จ
- ๊ทธ๋ฌ๋ linear order๊ฐ ๋ฌธ์ฅ์ ์๊ฐํ๋ ์ต์ ์ ๋ฐฉ๋ฒ์ด ์๋ ์ ์์

Issues with Recurrent Models: Lack of Parallelizability
- Forward ๋ฐ backward pass๋ ์ unparallelizable(๋ณ๋ ฌํ ๋ถ๊ฐ)ํ ์ฐ์ฐ์ ํฌํจ
- GPU๋ ํ ๋ฒ์ ๋ง์ ๋ ๋ฆฝ์ ์ธ ๊ณ์ฐ์ ์ํ ๊ฐ๋ฅ
- ๊ทธ๋ฌ๋ ๋ฏธ๋์ RNN hidden states๋ ๊ณผ๊ฑฐ์ RNN hidden states๊ฐ ๊ณ์ฐ๋๊ธฐ ์ ๊น์ง๋ ์์ ํ ๊ณ์ฐ๋ ์ ์์
- ์ด๋ ๋งค์ฐ ํฐ dataset์์์ ํ์ต์ ์ ํด


If Not Recurrence, Then What? How About Attention?
- Attention์ ๊ฐ ๋จ์ด์ representations๋ฅผ Query๋ก ์ทจ๊ธํ์ฌ Values ์งํฉ์ ์ ๋ณด์ ์ ๊ทผํ๊ณ ํตํฉ
- Decoder์์ encoder๋ก์ attention์ ๋ณด์์ผ๋, ์ด์ ๋จ์ผ ๋ฌธ์ฅ ๋ด์์์ attention์ ๊ณ ๋ ค โ Self-Attention!
- Attention์ ๊ฒฝ์ฐ, ๋ณ๋ ฌํ ๋ถ๊ฐํ ์ฐ์ฐ์ ์๊ฐ sequence length์ ๋ฐ๋ผ ์ฆ๊ฐํ์ง ์์
- Maximum interaction distance: ๋ชจ๋ ๋จ์ด๊ฐ ๋ชจ๋ layer์์ ์ํธ์์ฉํ๋ฏ๋ก

Attention as a Soft, Averaging Lookup Table
- Attention์ Key-Value store์์์ fuzzy lookup ์ํ์ผ๋ก ์๊ฐํ ์ ์์

Self-Attention Hypothetical Example

Self-Attention: Keys, Queries, Values from the Same Sequence
- ๋จ์ด์ฅ ๋ด์ ๋จ์ด sequence ๊ฐ์
- ์: "Zuko made his uncle tea"
- ๊ฐ ์ ๋ํด (์ฌ๊ธฐ์ ๋ embedding matrix)
- ๊ฐ word embedding์ weight matrices (๊ฐ๊ฐ )๋ก ๋ณํ
- Keys์ queries ๊ฐ์ pairwise similarities ๊ณ์ฐ ๋ฐ softmax๋ก ์ ๊ทํ
- ๊ฐ ๋จ์ด์ ๋ํ ์ถ๋ ฅ์ Values์ weighted sum์ผ๋ก ๊ณ์ฐ
Barriers & Solutions for Self-Attention as A Building Block
- Barrier(์ฅ์ ๋ฌผ): ์์์ ๋ํ ๊ณ ์ ํ ๊ฐ๋
๋ถ์ฌ
- Solution: Input์ position representation ์ถ๊ฐ
- Barrier: Deep Learning์ ์ํ nonlinearity ๋ถ์ฌ (๋จ์ง weighted average์ผ ๋ฟ์)
- Solution: ๊ฐ self-attention output์ ๋์ผํ feedforward network ์ ์ฉ
- Barrier: Sequence ์์ธก ์ ๋ฏธ๋๋ฅผ ๋ณด์ง ์๋๋ก ๋ณด์ฅ ํ์
- Machine translation์ ๊ฒฝ์ฐ
- ํน์ language modeling์ ๊ฒฝ์ฐ
- Solution: Attention weight๋ฅผ ์ธ์์ ์ผ๋ก 0์ผ๋ก ์ค์ ํ์ฌ ๋ฏธ๋๋ฅผ mask out

