๐ Attention Is All You Need
๐ reference link
๐ญ
Self-Attention
์ ์๊ธฐ์์ ๋ฌธ์ฅ์ ๋ํ ๋จ์ด๋ผ๋ฆฌ์ ์ ์ฌ๋๋ฅผ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ด๋ฉฐ,
Attention ํจ์์ ์ข ๋ฅ๋ ๋ค์ํ๋ค.ํด๋น ๋ ผ๋ฌธ์์๋
Scaled-dot product
ํจ์๋ฅผ ์ฌ์ฉํ๋ฉฐ,
์ด๋, N์ฐจ์์ผ๋ก ์ถ์ํ Q, K, V ๋ฒกํฐ๋ฅผ ์ด์ฉ,
N๊ฒน์ Attention์ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌํ๊ณ concatenation์ ํตํด ํฉ์น๋
Multi-head Attention
๋ฐฉ๋ฒ์ ์ ์ฉํ๋ค.
๐ก ์ ๋ ฅ ๋ฌธ์ฅ ๋ด์ ๋จ์ด๋ค๋ผ๋ฆฌ, ์ฆ ์๊ธฐ์์ ๋ฌธ์ฅ์ ๋ํ ์ ์ฌ๋๋ฅผ ๊ตฌํ์ฌ ๋ฌธ๋งฅ์ ํ์
Self Attention์ `Q, K, V` = ์ ๋ ฅ ๋ฌธ์ฅ์ ๋ชจ๋ ๋จ์ด ๋ฒกํฐ๋ค
- Query์ ๋ํ ๋ชจ๋ Key์์
์ ์ฌ๋
๋ฅผ ๊ณ์ฐ !
Query์ ๋ํด์ ๋ชจ๋ Key์์ ์ ์ฌ๋
๋ฅผ ๊ณ์ฐ
์ ์ฌ๋ = ๊ฐ์ค์น, ๊ฐ Key์ ๋์๋๋ Value ๊ฐ์ ๋ฐ์
Weighted Sum of Value
๋จ์ด ๋ฒกํฐ
๋ค์ ๊ฐ์ง๊ณ ์ํQ, K, V
๋ฒกํฐ๋ก ๋ณํํ์ฌ ์ด์ฉnum_heads = 8
๐ก Attention mechanism
๊ฐ Q ๋ฒกํฐ๋ ๋ชจ๋ K ๋ฒกํฐ์ ๋ํ์ฌ
Attention score
๋ฅผ ๊ณ์ฐ ; ์ ์ฌ๋Attention score๋ฅผ ์ด์ฉํ์ฌ ๋ชจ๋ V ๋ฒกํฐ๋ฅผ
๊ฐ์คํฉ
Return `Context vector`
- ์ดํ ์ ํจ์์ ์ข ๋ฅ๋ ๋ค์ํ๋ค
์ฐ์ฐ ๊ฐ๋
Input
[ I am a student ]
๋ฒกํฐ๋ผ๋ฆฌ Dot-product
Q, K, V ๋ฒกํฐ
๋ฅผ ์ด์ฉ์ค์ผ์ผ๋ง
Attention score
Attention value
Context vector
๊ฐ ๋จ์ด์ ๋ํด ๋ฒกํฐ ์ฐ์ฐ์ ํ๋ ๋์ , ๋ฌธ์ฅ ๋จ์
์ ํ๋ ฌ์ ์ด์ฉํ์ฌ ์ฐ์ฐ
ํ๋ ฌ
์ ๋์
Context matrix
์์์ ์ฌ์ฉ๋ ํ๋ ฌ ํฌ๊ธฐ ์ ๋ฆฌ
์ ๋ ฅ ๋ฌธ์ฅ์ ๊ธธ์ด | |
---|---|
๋ฌธ์ฅ ํ๋ ฌ์ ํฌ๊ธฐ | |
Q, K ๋ฒกํฐ์ ์ฐจ์ | |
V ๋ฒกํฐ์ ์ฐจ์ | |
Q, K ํ๋ ฌ์ ํฌ๊ธฐ | |
V ํ๋ ฌ์ ํฌ๊ธฐ | |
์์ ๊ฐ์ ์ ๋ฐ๋ฅด๋ฉด, | ๊ฐ์ค์น ํ๋ ฌ์ ํฌ๊ธฐ ์ถ์ ๊ฐ๋ฅ |
์ด๋, ๋ ผ๋ฌธ์ ๋ฐ๋ฅด๋ฉด, | ์ด๋ฏ๋ก, |
Attention Value Matrix |
๐ก Why Multi-head?
์ฌ๋ฌ ๊ฐ์ Attention head = `์ฌ๋ฌ ๊ฐ์ ์๊ฐ`
์ฆ, ๋ค์ํ ์๊ฐ์์ ์ ์ฌ๋ ์ ๋ณด๋ฅผ ์์งํ๊ธฐ ์ํจ
์ดํ ์ ํค๋์ ๊ฐ์๋งํผ ๋ณ๋ ฌ ์ดํ ์ ์ฐ์ฐ
Attention head
์ฐ์ฐ ์ ์ฐจ
๊ฐ๊ธฐ ๋ค๋ฅธ ๊ฐ์ค์น ํ๋ ฌ์ ์ด์ฉํ ๋ณ๋ ฌ ์ดํ ์ ์ํ
Attention head
๋ชจ๋ Attention head ์ฐ๊ฒฐ
concatenated matrix
๊ฐ์ค์น ํ๋ ฌ ๊ณฑํ๊ธฐ
Return Multi-head Attention Matrix
๋์ผ ํฌ๊ธฐ ์ ์ง
๐ญ Matrix ํฌ๊ธฐ๊ฐ ์ ์ง๋์ด์ผ ํ๋ ์ด์ ?
- Transformer - ๋์ผํ ๊ตฌ์กฐ์ `encoder`๋ฅผ 6 layer ์์ ๊ตฌ์กฐ โ ๋ค์ `encoder`์ ๋ค์ ์ ๋ ฅ๋๊ธฐ ์ํจ