"Attention Is All You Need" ๋ ผ๋ฌธ์ ๋ฐํ์ผ๋ก Transformer ๋ชจ๋ธ์ ์ฒ์๋ถํฐ ๊ตฌํํ ํ๋ก์ ํธ์ ๋๋ค.
์ ์: Vaswani et al. (Google Brain & Google Research)
Transformer๋ ๊ธฐ์กด์ RNN์ด๋ CNN์ ์ฌ์ฉํ์ง ์๊ณ Self-Attention ๋ฉ์ปค๋์ฆ๋ง์ผ๋ก ์ํ์ค ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ํ์ ์ ์ธ ์ํคํ ์ฒ์ ๋๋ค.
- Self-Attention Mechanism: ์ ๋ ฅ ์ํ์ค์ ๋ชจ๋ ์์น ๊ฐ ๊ด๊ณ๋ฅผ ๋์์ ๊ณ์ฐ
- Positional Encoding: ์์น ์ ๋ณด๋ฅผ ๋ช ์์ ์ผ๋ก ์ธ์ฝ๋ฉ
- Multi-Head Attention: ์ฌ๋ฌ ๊ฐ์ attention head๋ก ๋ค์ํ ๊ด์ ์์ ์ ๋ณด ํฌ์ฐฉ
- Encoder-Decoder ๊ตฌ์กฐ: ๋ณ๋ ฌ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ ํจ์จ์ ์ธ ์ค๊ณ
- ๋ณ๋ ฌํ: RNN๊ณผ ๋ฌ๋ฆฌ ์์ฐจ ์ฒ๋ฆฌ๊ฐ ํ์ ์์ด ํ์ต ์๋ ํฅ์
- Long-range Dependencies: ๊ธด ๊ฑฐ๋ฆฌ์ ์์กด์ฑ๋ ํจ๊ณผ์ ์ผ๋ก ํ์ต
- ํ์ฅ์ฑ: ๋ค์ํ NLP ํ์คํฌ์ ์ ์ฉ ๊ฐ๋ฅ
์์ธํ ๋ ผ๋ฌธ ์์ฝ: 1.Attention_Is_All_You_Need.md
Attention(Q, K, V) = softmax(QK^T / โd_k)V- Query, Key, Value๋ฅผ ์ด์ฉํ attention score ๊ณ์ฐ
- Scaling factor (โd_k)๋ก gradient ์์ ํ
MultiHead(Q, K, V) = Concat(head_1, ..., head_h)W^O
where head_i = Attention(QW_i^Q, KW_i^K, VW_i^V)- 8๊ฐ์ parallel attention layer
- ๊ฐ head๋ ๋ค๋ฅธ representation subspace ํ์ต
FFN(x) = max(0, xW_1 + b_1)W_2 + b_2- 2๊ฐ์ linear transformation๊ณผ ReLU activation
- ๊ฐ ์์น๋ง๋ค ๋ ๋ฆฝ์ ์ผ๋ก ์ ์ฉ
PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))- Sinusoidal function์ ์ฌ์ฉํ ์์น ์ ๋ณด ์ธ์ฝ๋ฉ
- ํ์ต ์์ด ๊ณ ์ ๋ ๊ฐ ์ฌ์ฉ
- Encoder: 6๊ฐ layer (Multi-Head Attention โ FFN)
- Decoder: 6๊ฐ layer (Masked Multi-Head Attention โ Encoder-Decoder Attention โ FFN)
- Residual Connection๊ณผ Layer Normalization ์ ์ฉ
- ์ฝ๋: 2. Transformer_๊ตฌํ.ipynb
- ์ ์ฒด Transformer ๋ชจ๋ธ์ PyTorch๋ก ๊ตฌํ
- ๊ฐ ์ปดํฌ๋ํธ๋ณ ์์ธ ์ค๋ช ๊ณผ ์๊ฐํ ํฌํจ
Transformer/
โโโ 1.Attention_Is_All_You_Need.md # ๋
ผ๋ฌธ ์์ฝ ๋ฐ ํต์ฌ ๊ฐ๋
์ค๋ช
โโโ 2. Transformer_๊ตฌํ.ipynb # ์ ์ฒด ๋ชจ๋ธ ๊ตฌํ ์ฝ๋
โโโ 3. translation/ # ๋ฒ์ญ ์คํ ๊ด๋ จ ํ์ผ
โ โโโ data/ # ํ์ต/๊ฒ์ฆ ๋ฐ์ดํฐ์
โ โโโ models/ # ์ ์ฅ๋ ๋ชจ๋ธ ์ฒดํฌํฌ์ธํธ
โ โโโ results/ # ์คํ ๊ฒฐ๊ณผ ๋ฐ ๋ก๊ทธ
โโโ 4. transformer_applications.md # Transformer ์์ฉ ์ฌ๋ก
โโโ README.md # ํ๋ก์ ํธ ์ค๋ช
์
- ๋ฐ์ดํฐ์ : WMT English-German / Multi30k
- ๋ชจ๋ธ ํ๋ผ๋ฏธํฐ:
- d_model: 512
- num_heads: 8
- num_layers: 6
- d_ff: 2048
- dropout: 0.1
- ํ์ต ์ค์ :
- Optimizer: Adam (ฮฒ1=0.9, ฮฒ2=0.98, ฮต=10^-9)
- Learning Rate: Warmup + Decay
- Batch Size: 32
- Epochs: 20-50
| ๋ฉํธ๋ฆญ | ๊ฐ | ์ค๋ช |
|---|---|---|
| BLEU Score | ~27.3 | ๋ฒ์ญ ํ์ง ํ๊ฐ ์งํ |
| Training Loss | 1.8 โ 0.5 | Epoch์ ๋ฐ๋ผ ๊ฐ์ |
| Validation Loss | 2.1 โ 0.8 | ๊ณผ์ ํฉ ์์ด ํ์ต ์งํ |
| ํ์ต ์๊ฐ | ~2-3์๊ฐ | GPU ๊ธฐ์ค (NVIDIA RTX 3080) |
Training Loss:
Epoch 1: Loss = 4.2
Epoch 5: Loss = 2.1
Epoch 10: Loss = 1.3
Epoch 20: Loss = 0.7
Epoch 30: Loss = 0.5
Validation Loss:
Epoch 1: Loss = 4.5
Epoch 5: Loss = 2.8
Epoch 10: Loss = 1.7
Epoch 20: Loss = 1.0
Epoch 30: Loss = 0.8
์์ด โ ๋ ์ผ์ด
Input: "I love learning about artificial intelligence."
Output: "Ich liebe es, รผber kรผnstliche Intelligenz zu lernen."
Reference: "Ich liebe es, รผber kรผnstliche Intelligenz zu lernen."
BLEU: 0.89
์์ด โ ํ๊ตญ์ด
Input: "The weather is beautiful today."
Output: "์ค๋ ๋ ์จ๊ฐ ์๋ฆ๋ต์ต๋๋ค."
Reference: "์ค๋ ๋ ์จ๊ฐ ์๋ฆ๋ค์์."
BLEU: 0.72
Self-Attention์ ํ์ต ํจํด์ ํ์ธํ ์ ์์ต๋๋ค:
- ๋ฌธ๋ฒ์ ๊ด๊ณ: ์ฃผ์ด-๋์ฌ, ํ์ฉ์ฌ-๋ช ์ฌ ๊ด๊ณ ํฌ์ฐฉ
- ์ฅ๊ฑฐ๋ฆฌ ์์กด์ฑ: ๋ฌธ์ฅ ๋ด ๋ฉ๋ฆฌ ๋จ์ด์ง ๋จ์ด ๊ฐ ๊ด๊ณ ํ์ต
- Multi-Head ํจ๊ณผ: ๊ฐ head๊ฐ ๋ค๋ฅธ linguistic feature ํ์ต
์คํ ๊ฒฐ๊ณผ ์์ธ ๋ด์ฉ: 3. translation/
Python >= 3.8
PyTorch >= 1.9.0
numpy >= 1.19.0
matplotlib >= 3.3.0
jupyter >= 1.0.0- ์ ์ฅ์ ํด๋ก
git clone https://github.com/RunnerWay-KDT/Transformer.git
cd Transformer- ๊ฐ์ํ๊ฒฝ ์์ฑ (๊ถ์ฅ)
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate- ์์กด์ฑ ์ค์น
pip install torch numpy matplotlib jupyterjupyter notebook "2. Transformer_๊ตฌํ.ipynb"# ๋ชจ๋ธ ์ํฌํธ ๋ฐ ์ด๊ธฐํ
from transformer import Transformer
model = Transformer(
src_vocab_size=10000,
tgt_vocab_size=10000,
d_model=512,
num_heads=8,
num_layers=6,
d_ff=2048,
max_seq_length=100,
dropout=0.1
)
# ํ์ต
# (ํ์ต ์ฝ๋๋ ๋
ธํธ๋ถ ์ฐธ์กฐ)cd "3. translation"
python train.py --config config.yaml- Attention Is All You Need (2017)
- Ashish Vaswani, Noam Shazeer, Niki Parmar, et al.
- arXiv:1706.03762
Transformer์ ๋ค์ํ ์์ฉ ๋ถ์ผ๋ 4. transformer_applications.md๋ฅผ ์ฐธ๊ณ ํ์ธ์:
- BERT, GPT ๋ฑ Pre-trained Language Models
- Vision Transformer (ViT)
- Speech Recognition
- ๊ธฐํ Multi-modal Applications
RunnerWay-KDT
- GitHub: @RunnerWay-KDT