Attention ๋ ผ๋ฌธ ๋ฆฌ๋ทฐ
1. Abstract
- ๊ธฐ์กด์ Sequence ๋ณํ ๋ชจ๋ธ์ ๋๋ถ๋ถ ์ธ์ฝ๋์ ๋์ฝ๋๋ฅผ ํฌํจํ๋ RNN ๋๋ CNN ์ ๊ฒฝ๋ง์ ๊ธฐ๋ฐ์ ๋๊ณ ์์
- ํด๋น ๋
ผ๋ฌธ์์๋ Attention ๋งค์ปค๋์ฆ๋ง์ ์ด์ฉํ ๋ชจ๋ธ ๊ตฌ์กฐ์ธ Transformer๋ฅผ ์ ์ํจ
โ Attention์ด๋ ๋ฌธ๋งฅ์ ๋ฐ๋ผ ์ง์คํ ๋จ์ด๋ฅผ ๊ฒฐ์ ํ๋ ๋ฐฉ์์ ์๋ฏธ - Transformer์ ํน์ง์ ์๋์ ๊ฐ์
- ๋ ๋์ ๋ณ๋ ฌํ๊ฐ ๊ฐ๋ฅํด ๊ธฐ์กด ๋ชจ๋ธ๋ค ๋ณด๋ค ํ์ต ์๊ฐ์ด ๋ ๊ฑธ๋ฆผ
- WMT 2014 ์์ด-๋ ์ผ์ด ๋ฐ ์์ด-ํ๋์ค์ด ๋ฒ์ญ ์์ ์์ Transformer๋ ์ต์ ์ฑ๋ฅ์ ๊ธฐ๋ก
2. Introduction
- ์์ฐจ์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฃผ๋ก RNN์ด๋ CNN์ด ์ฌ์ฉ๋์ด ์์
โ ์ด๋ฌํ ๋ชจ๋ธ๋ค์ ์ฑ๋ฅ์ ๋ฐ์ด๋์ง๋ง ๊ธด ์ข ์๊ด๊ณ๋ฅผ ํ์ตํ๋ ๋ฐ ์ด๋ ค์์ด ์์ ๋๋ Suquence ๊ธธ์ด๊ฐ ๊ธธ์ด์ง๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ - ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด RNN, CNN ์์ด Attention ๋งค์ปค๋์ฆ๋ง์ ์ฌ์ฉํ ์๋ก์ด ๋ชจ๋ธ ๊ตฌ์กฐ์ธ Transformer๋ฅผ ์ ์
3. Background
- Extended Neural GPU, ByteNet, ConvS2S ์์๋ ์ฐ์์ ์ฐ์ฐ์ ์ค์ด๊ธฐ ์ํ ์ฐ๊ตฌ๊ฐ ์ด๋ฃจ์ด์ก๋๋ฐ, ๋ชจ๋ CNN์ ๊ธฐ๋ณธ ๊ตฌ์ฑ ์์๋ก ์ฌ์ฉ
โ ๋ณ๋ ฌ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ์ง๋ง, ์ ๋ ฅ๊ฐ๊ณผ ์ถ๋ ฅ๊ฐ์ด ๋ฉ์๋ก ์์กด์ฑ์ ์๊ธฐ ์ด๋ ต๊ณ ๊ณ์ฐ๋ ์ฆ๊ฐ - ํ์ง๋ง Transformer๋ฅผ ์ฌ์ฉํ๋ฉด Multi-Head Attention์ ์ด์ฉํด ์์ ์๊ฐ์ ๊ฐ์ผ๋ก ๊ณ์ฐ์ด ๊ฐ๋ฅ
3.1 Self Attention
- ๋ง ๊ทธ๋๋ก Attention์ ์๊ธฐ ์์ ํํ ์ทจํ๋ค๋ ๊ฒ โ ๋ฌธ์ฅ์์์ ๋จ์ด๋ค ๊ฐ์ ์ฐ๊ด์ฑ์ ์๊ธฐ์ํด

์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ๋ฌธ์ฅ ๋ด์์ it ๊ฐ๋ฅดํค๋ ๋จ์ด๊ฐ ๋ฌด์์ธ์ง Self Attention์ ์ด์ฉํด ์ ์ ์์
3.2 End-to-End memory Network
- ์ํ์ค๊ฐ ๋ฐฐ์ด๋ recurrence๋ณด๋ค recurrent attention mechanism ๊ธฐ๋ฐ
4. Model Architecture
- ๊ธฐ์กด์ Sequence ๋ณํ ๋ชจ๋ธ์ ์๋์ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๊ณ ์์
- ์ด๋ฌํ ๊ตฌ์กฐ๋ ํญ์ ์ด์ ๋จ๊ณ๊ฐ ์๋ฃ๋์ด์ผ ๋ค์ ๋จ๊ณ๊ฐ ์งํ ๊ฐ๋ฅ โ ๋ณ๋ ฌ ์ฒ๋ฆฌ ๋ถ๊ฐ๋ฅ

- Transformer๋ ์ธ์ฝ๋์ ๋์ฝ๋ ๋ชจ๋์์ ์คํ๋ Self Attention๊ณผ Point-wise ์์ ์ฐ๊ฒฐ์ธต์ ์ฌ์ฉํ๋ ์๋์ ์ฌ์ง๊ณผ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง

4.1 Encoder and Decoder Stacks
- Encorder : 6๊ฐ(N=6)์ ๋์ผํ ๋ ์ด์ด๋ก ์ด๋ฃจ์ด์ ธ ์์ผ๋ฉฐ ํ๋์ ์ธ์ฝ๋๋ Self-Attention layer์ Feed Forward Neural Network๋ผ๋ ๋ ๊ฐ์ Sub layer๋ก ์ด๋ฃจ์ด์ ธ ์์
- Decorder: 6๊ฐ(N=6)์ ๋์ผํ ๋ ์ด์ด, ๊ฐ ๋ ์ด์ด๋ ์ธ์ฝ๋๊ฐ Sub layer๋ก ๊ฐ์ง Self-Attention layer์ Feed Forward Neural Network ์ธ์ Masked Multi-Head Attention layer๋ฅผ ๊ฐ์ง
4.2 Attention
4.2.1 Scaled Dot-Product Attention

Query : ์ํฅ์ ๋ฐ์ ๋จ์ด (๋ฒกํฐ)
Key : ์ํฅ์ ์ฃผ๋ ๋จ์ด (๋ฒกํฐ)
Value : ์ํฅ์ ๋ํ ๊ฐ์ค์น (๋ฒกํฐ)

์ด ์์ ์ด์ฉํด ๊ณ์ฐ
< ๊ณ์ฐ ๊ณผ์ ์์ฝ >
(1) ์๋ ์๋ฒ ๋ฉ์ ๊ฐ์ค์น(
- Query:
- Key:
- Value:
(2) Query * Key = attention score โ ๊ฐ์ด ๋์ ์๋ก ์ฐ๊ด์ฑ์ด ๋๊ณ , ๋ฎ์ ์๋ก ์ฐ๊ด์ฑ์ด ๋ฎ๋ค.
(3) key ์ฐจ์์๋ก ๋๋๊ณ softmax ์ ์ฉ โ softmax ๊ฒฐ๊ณผ ๊ฐ์ key๊ฐ์ ํด๋นํ๋ ๋จ์ด๊ฐ ํ์ฌ ๋จ์ด์ ์ด๋์ ๋ ์ฐ๊ด์ฑ์ด ์๋์ง ๋ํ๋
(4) ๋ฌธ์ฅ ์์์ ์ง๋ ์ ๋ ฅ ์๋์ ๊ฐ = softmax ๊ฐ๊ณผ value ๊ฐ์ ๊ณฑํ์ฌ ๋ค ๋ํจ
4.2.2 Multi-Head Attention
- ๋ฌธ์ฅ์ ๋ค์ํ ๊ด์ ์์ ๋ชจ๋ธ์ด ํด์ ๊ฐ๋ฅํ๋๋ก ํ๋ ์ญํ
Attention์ parallelํ๊ฒ ๊ตฌ์ฑํด์ ์ฌ๋ฌ๋ฒ ๊ณ์ฐํ๋ ๊ตฌ์กฐ



- ์์ ๊ทธ๋ฆผ์ ๋ณผ ๋ it์ด ๊ฐ๋ฅดํค๋ ๋จ์ด๊ฐ ๋ฌด์์ธ์ง ํ์คํ ์ ์ ์์
- ์ด๋ Multi-Head Attention์ ์ด์ฉํ๋ฉด ์ ์ ์์
โ Head์ ๊ฐฏ์๋ฅผ (๋ ผ๋ฌธ์์) 8๊ฐ๋ฅผ ๋๊ณ ๋ชจ๋ Head๋ฅผ Scaled Dot-Product Attention์ ์งํ
โ ๋ค์ํ ๊ด์ ์์ ๋ฌธ์ฅ์ ๋ณผ ์ ์๊ฒ ๋จ - Single-Head Attention์ ์ถ๋ ฅ ์ฑ๋์ด
์ด๋ผ๊ณ ํ ๋ Multi-Head Attention์์์ ๊ฐ Head์ ์ถ๋ ฅ ์ฑ๋์ ์ฐจ์์ ๊ฐ์ง๊ฒ ๋จ โ ๊ณ์ฐ ๋น์ฉ์ Single๊ณผ Multi๊ฐ ๋น์ทํ์ง๋ง Multi-Head๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ๋ค์ํ ์ ๋ณด๋ฅผ ๋์์ ์ป์ ์ ์๊ฒ๋จ
4.2.3 Application in Attention in our Model
Transformer๋ ์ธ ๊ฐ์ง ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก Multi-Head Attention์ ์ฌ์ฉ

- "self-attention in encoder": encoder์์ ์ฌ์ฉ๋๋ self-attention์ผ๋ก queries, keys, values ๋ชจ๋ encoder๋ก๋ถํฐ ๊ฐ์ ธ์จ๋ค. encoder์ ๊ฐ position์ ๊ทธ ์ layer์ ๋ชจ๋ positions๋ค์ ์ฐธ์กฐํ๊ณ ,ย ์ด๋ ํด๋น position๊ณผ ๋ชจ๋ position๊ฐ์ correlation information์ ๋ํด์ฃผ๊ฒ ๋๋ค. ๊ฐ๋จํ๊ฒ ์ค๋ช ํด์ ์ด๋ค ํ ๋จ์ด์ด ๋ชจ๋ ๋จ์ด๋ค ์ค ์ด๋ค ๋จ์ด๋ค๊ณผ correlation์ด ๋๊ณ , ๋ ์ด๋ค ๋จ์ด์๋ ๋ฎ์์ง๋ฅผ ๋ฐฐ์ฐ๊ฒ ๋๋ค.
- "self-attention in decoder": ์ ์ฒด์ ์ธ ๊ณผ์ ๊ณผ ๋ชฉํ๋ encoder์ self-attention๊ณผ ๊ฐ๋ค. ํ์ง๋ง decoder์ ๊ฒฝ์ฐ, sequence model์ย auto-regressive property๋ฅผ ๋ณด์กดํด์ผํ๊ธฐ ๋๋ฌธ์ masking vector๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น position ์ด์ ์ ๋ฒกํฐ๋ค๋ง์ ์ฐธ์กฐํ๋ค(์ดํ์ ๋์ฌ ๋จ์ด๋ค์ ์ฐธ์กฐํ์ฌ ์์ธกํ๋ ๊ฒ์ ์ผ์ข ์ ์นํ ).
- "encoder-decoder attention": decoder์์ self-attention ๋ค์์ผ๋ก ์ฌ์ฉ๋๋ layer์ด๋ค. queries๋ ์ด์ decoder layer์์ ๊ฐ์ ธ์ค๊ณ , keys์ values๋ encoder์ output์์ ๊ฐ์ ธ์จ๋ค. ์ด๋ decoder์ ๋ชจ๋ position์ vector๋ค๋ก encoder์ ๋ชจ๋ position ๊ฐ๋ค์ ์ฐธ์กฐํจ์ผ๋ก์จย decoder์ sequence vector๋ค์ด encoder์ sequence vector๋ค๊ณผ ์ด๋ ํ correlation์ ๊ฐ์ง๋์ง๋ฅผ ํ์ตํ๋ค.
4.3 Position-wise Feed-Forward Networks

- Transformer์์ fully connected feed-forward network๊ฐ ์ฌ์ฉ๋จ
- 2๊ฐ์ ์ ํ ๋ณํ์ผ๋ก ๊ตฌ์ฑ๋๊ณ ๊ทธ ์ฌ์ด์ ReLU ํ์ฑํ ํจ์๊ฐ ์กด์ฌ
- ์
์ถ๋ ฅ ์ฐจ์
์ด๊ณ hidden ๋ ์ด์ด์ ์ฐจ์์ ๋ฅผ ๊ฐ์ง

4.4 Embeddings and Softmax

- input๊ณผ output ํ ํฐ์ embedding layer๋ฅผ ๊ฑฐ์ณ์ ์ฌ์ฉ
- input embedding ๊ณผ output embedding์์ weight matrix๋ฅผ ์๋ก ๊ณต์
4.5 Positional Encoding
Sequence์ ์์๋ฅผ ํ์ฉํ๊ธฐ ์ํด ํ ํฐ์ ์๋์ ๋๋ ์ ๋์ ์ธ ์์น์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณต
- Transformer์์๋ Attention๋ง ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์์น ์ ๋ณด๋ฅผ ์ป์ ์ ์์
- ๋ฐ๋ผ์ ์ด๋ฌํ ์์น ์ ๋ณด๋ฅผ ์ป๊ธฐ ์ํด โPositional Encodingโ์ ์งํํจ
- ๋ ผ๋ฌธ์์๋ sin ํจ์์ cos ํจ์๋ฅผ ์ด์ฉํด ์งํ

pos๋ position, i๋ dimension
5.Why Self-Attention
์ Self-Attention์ด RNN, CNN ๋ณด๋ค ๋ ์ข์๊ฐ?
- Layer๋น ๊ณ์ฐ ๋ณต์ก์ฑ์ด ์ค์ด๋ฆ
์๋์ ํ๋ฅผ ํ์ธํด๋ณด๋ฉด Self-Attention์ ๊ณ์ฐ ๋ณต์ก๋๊ฐ ๋ค๋ฅธ ๊ฒ์ ๋นํด ๋ฎ์ ๊ฒ์ ์ ์ ์์
๋ณดํต sequence length n์ด vector dimension d๋ณด๋ค ์์ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค. Conv ํฌ๊ธฐ = k,
r์ ๋ชจ๋ ๋จ์ด์ self attention์ด ์๋ ์ผ๋ถ ๋ฒ์์์๋ง ์งํ
-
๋ณ๋ ฌํ ํ ์ ์๋ ๊ณ์ฐ๋
Self-attention layer๋ input์ ๋ชจ๋ position ๊ฐ๋ค์ ์ฐ๊ฒฐํ์ฌ ํ๋ฒ์ ์ฒ๋ฆฌํ ์ ์๋ค. ๋ฐ๋ผ์ Sequential operations์ด O(1)์ ๊ฐ์ง โ parallel system์์ ์ ๋ฆฌํ๊ฒ ์ฌ์ฉ๋จ -
์ฅ๊ธฐ ์์กด์ฑ ๋ฌธ์ ํด๊ฒฐ
์ฅ๊ธฐ ์์กด์ฑ์ ์ ๋ฐฐ์ฐ๊ธฐ ์ํด์๋ Length of paths๊ฐ ํฐ ์ํฅ์ ๋ฏธ์นจ
length of paths ๋ ๋์๋๋ input sequence token๊ณผ output sequence token ๊ฐ์ ๊ฑฐ๋ฆฌ
Maximum path length๋ length of paths์ค์์ ๊ฐ์ฅ ๊ธด ๊ฑฐ๋ฆฌ โ input์ ์ฒซ token๊ณผ output์ ๋ง์ง๋ง token (input sequence length + output sequence length)Self**-**attention์ ๊ฐ token๋ค์ ๋ชจ๋ token๋ค๊ณผ ์ฐธ์กฐํ์ฌ ๊ทธ correlation information์ ๊ตฌํด์ ๋ํด์ฃผ๊ธฐ ๋๋ฌธ์(์ฌ์ง์ด encoder-decoder๋ผ๋ฆฌ๋), maximum path length๋ฅผ O(1)์ด๋ผ๊ณ ๋ณผ ์ ์๋ค. ๋ฐ๋ผ์ long-range dependencies๋ฅผ ๋ ์ฝ๊ฒ ํ์ตํ ์ ์๋ค๋ ์ฅ์ ์ ๊ฐ์ง๋ค.
6. Overall Training
Encoder์์๋ ๋ฌธ์ฅ ๋ด ๋จ์ด์ ๋ฌธ๋งฅ์ ์ดํดํ๊ณ , Decoder์์๋ ์์ฐจ์ ์ผ๋ก ๋ฒ์ญ๋ ๋ฌธ์ฅ์ ๋ด๋๋๋ค.
(1) Encoding ๊ณผ์ :
์๋ฅผ ๋ค์ด, "The cat sits"๋ผ๋ ๋ฌธ์ฅ์ด ์๋ค๊ณ ๊ฐ์ ํด ๋ด ์๋ค. ์ด ๋ฌธ์ฅ์ด Transformer ๋ชจ๋ธ์ ์ ๋ ฅ์ผ๋ก ๋ค์ด๊ฐ๋ฉด, ๊ฐ ๋จ์ด๋ ๋ฒกํฐ๋ก ๋ณํ๋ฉ๋๋ค( โ ์๋ฒ ๋ฉ). ๋์์, ๊ฐ ๋จ์ด์ ์์น ์ ๋ณด๋ ๋ฒกํฐ๋ก ํํ๋ฉ๋๋ค( โํฌ์ง์ ๋ ์๋ฒ ๋ฉ)
- "The" โ [0.1, 0.5, ...] (์๋ฒ ๋ฉ ๋ฒกํฐ)
- "cat" โ [0.2, 0.3, ...] (์๋ฒ ๋ฉ ๋ฒกํฐ)
- "sits" โ [0.4, 0.7, ...] (์๋ฒ ๋ฉ ๋ฒกํฐ)
์ด๋ ๊ฒ ์๋ฒ ๋ฉ๋ ๋จ์ด ์ํ์ค๋ Self-Attention๊ณผ Feed-Forward Network๋ฅผ ํฌํจํ๋ Encoder ๋ธ๋ก์ ํต๊ณผํฉ๋๋ค.
์ด ๊ณผ์ ์ ํตํด ๋ฌธ์ฅ์ ๊ฐ ๋จ์ด๋ ์์ ์ด ์ด๋ค ์๋ฏธ๋ฅผ ๊ฐ์ง๋ฉฐ, ๋ค๋ฅธ ๋จ์ด๋ค๊ณผ ์ด๋ค ๊ด๊ณ๋ฅผ ๋งบ๊ณ ์๋์ง๋ฅผ ํํํ๋ ๋ฒกํฐ๋ก ์ธ์ฝ๋ฉ๋ฉ๋๋ค.
์๋ฅผ ๋ค์ด, "cat"์ด๋ผ๋ ๋จ์ด๊ฐ "sits"์ ๋ฐ์ ํ ๊ด๊ณ๊ฐ ์๋ค๋ฉด, "cat" ๋ฒกํฐ๋ ์ด ์ ๋ณด๋ฅผ ํฌํจํ๊ฒ ๋ฉ๋๋ค.
(2) Decoding ๊ณผ์ :
์ด์ ๋ชจ๋ธ์ "The cat sits"๋ผ๋ ๋ฌธ์ฅ์ ๋ค๋ฅธ ์ธ์ด๋ก ๋ฒ์ญํ๋ ค๊ณ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, "The cat sits"๋ฅผ ํ๊ตญ์ด๋ก ๋ฒ์ญํ๋ค๊ณ ํด๋ด ์๋ค.
Decoder๋ ๋ฒ์ญ๋ ๋ฌธ์ฅ์ ์์ฑํฉ๋๋ค. ์ด ๊ณผ์ ์์ ๋จ์ด๋ค์ ํ๋์ฉ ์์๋๋ก ์ถ๋ ฅ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด, ์ฒ์์๋ ์์ ํ ํฐ(start token)์ด ์ ๋ ฅ์ผ๋ก ๋ค์ด๊ฐ "๊ณ ์์ด"๋ผ๋ ๋จ์ด๊ฐ ๊ฐ์ฅ ๋์ ํ๋ฅ ๋ก ์ถ๋ ฅ๋ฉ๋๋ค.
- ์ฒซ ๋ฒ์งธ ์ ๋ ฅ: ์์ ํ ํฐ โ ์ถ๋ ฅ: "๊ณ ์์ด"
๊ทธ ๋ค์์๋ "๊ณ ์์ด"๋ผ๋ ๋จ์ด๊ฐ ๋ค์ ๋์ฝ๋์ ์ ๋ ฅ์ผ๋ก ์ฌ์ฉ๋์ด, "๋"์ด๋ผ๋ ๋จ์ด๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
- ๋ ๋ฒ์งธ ์ ๋ ฅ: "๊ณ ์์ด" โ ์ถ๋ ฅ: "๋"
๋ง์ง๋ง์ผ๋ก, "๊ณ ์์ด"์ "๋"์ด ํจ๊ป ์ ๋ ฅ๋์ด, "์์์๋ค"๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
- ์ธ ๋ฒ์งธ ์ ๋ ฅ: "๊ณ ์์ด + ๋" โ ์ถ๋ ฅ: "์์์๋ค"
์ด ๊ณผ์ ์์ Self-Attention ๋ ์ด์ด๋ ํด๋น ์์น๋ณด๋ค ์ด์ ์ ์์ฑ๋ ๋ชจ๋ ๋จ์ด๋ฅผ ์ฐธ์กฐํ์ฌ ์ฌ๋ฐ๋ฅธ ๋ฒ์ญ์ ์์ฑํ ์ ์๋๋ก ํฉ๋๋ค. ์ด๋ ํ์ฌ ์์น ์ดํ์ ๋จ์ด๋ ๋งค์ฐ ์์ ๊ฐ(๋ณดํต
์ด๋ ๊ฒ ํ๋ฉด ๋์ฝ๋๋ ์์ง ์์ฑ๋์ง ์์ ๋จ์ด์ ์ํฅ์ ๋ฐ์ง ์๊ณ , ์ด๋ฏธ ์์ฑ๋ ๋จ์ด๋ค๋ง์ ๊ธฐ๋ฐ์ผ๋ก ๋ฒ์ญ์ ์ด์ด๊ฐ๋๋ค.
๋ํ ๋์ฝ๋๋ Encoder-Decoder Attention ๋ ์ด์ด๋ฅผ ํตํด, ์ธ์ฝ๋์์ ๋์จ ์ ๋ณด๋ฅผ ํ์ฉํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ๋์ฝ๋์ ๊ฐ ๋จ์ด๋ ์ธ์ฝ๋์์ ์์ฑ๋ "The cat sits"์ ๋ฒกํฐ๋ค๊ณผ ์ดํ ์ ์ ๊ณ์ฐํ์ฌ, ๋ฒ์ญ ๊ณผ์ ์์ ์ ๋ ฅ ๋ฌธ์ฅ์ ๋ชจ๋ ์์น๋ฅผ ์ฐธ์กฐํ๊ฒ ๋ฉ๋๋ค.