์ด๋ฒ ๊ธ์์๋ Transformer ๋ชจ๋ธ์ ์ ๋ฐ์ ์ธ Architecture ๋ฐ ๊ตฌ์ฑ์ ๋ฐํ์ฌ ์์๋ณด๊ฒ ์ต๋๋ค.
Transformer: Attention is All You Need
- Transformer ๋ชจ๋ธ์ 2017๋ ์ "Attention is All You Need"๋ผ๋ ๋ ผ๋ฌธ์ ํตํด์ ์๊ฐ๋์์ต๋๋ค.
- ์ฃผ์ํ ํต์ฌ ์์ด๋์ด๋ "Self-Attention" ์ด๋ผ๋ ๋งค์ปค๋์ฆ์ ๊ธฐ๋ฐํ์ฌ, ๋ฌธ์ฅ ๋ด์ ๋ชจ๋ ๋จ์ด๋ค ์ฌ์ด์ ๊ด๊ณ๋ฅผ ํ ๋ฒ์ ํ์ ํ ์ ์๋ค๋ ์ ์ ์์ต๋๋ค.
- ์ด์ ์ ์ค๋ช ํ๋ RNN(Recurrent Neural Network), LSTM(Long Short-Term Memory)๊ณผ ๊ฐ์ ์์ฐจ์ ์ธ Model์ด ๊ฐ์ง ์์ฐจ์ ์ฒ๋ฆฌ์ ํ๊ณ๋ฅผ ๊ทน๋ณตํ๋ค๋ ํน์ง์ด ์์ต๋๋ค.
- ๊ทธ๋ฆฌ๊ณ ํ์ฌ Transformer ๋ชจ๋ธ์ Sequence - Sequence๊ฐ์ ์์ ๋ฟ๋ง ์๋๋ผ, Language Modeling, Pre-Training ์ค์ ์์๋ ์ฌ์ค์์ ํ์ค ๋ชจ๋ธ์ ๋๋ค.
- ๊ทธ๋ฆฌ๊ณ Transformer Model์ ์๋ก์ด ๋ชจ๋ธ๋ง ํจ๋ฌ๋ค์์ ๋์ ํ์ต๋๋ค.
Attention ๋ฐ Self-Attention ๊ฐ๋ ์ ๊ดํ ๋ด์ฉ์ ์๋์ ๊ธ์ ์์ธํ ์์ฑํด ๋์์ผ๋ ํ๋ฒ ์ฝ์ด๋ณด์ธ์!
[NLP] Attention - ์ดํ ์
1. Attention Attention์ CS ๋ฐ ML์์ ์ค์ํ ๊ฐ๋ ์ค ํ๋๋ก ์ฌ๊ฒจ์ง๋๋ค. Attention์ ๋งค์ปค๋์ฆ์ ์ฃผ๋ก Sequence Data๋ฅผ ์ฒ๋ฆฌํ๊ฑฐ๋ ์์ฑํ๋ ๋ชจ๋ธ์์ ์ฌ์ฉ๋ฉ๋๋ค. -> Sequence ์ ๋ ฅ์ ์ํํ๋ ๋จธ์ ๋ฌ๋ ํ์ต
daehyun-bigbread.tistory.com
Transformer Model๊ณผ RNN Model์ ์ฐจ์ด์
Transformer ๋ชจ๋ธ๊ณผ RNN ๋ชจ๋ธ๊ฐ์ ์ฐจ์ด์ ์ ํ๋ฒ ์๊ธฐํด ๋ณด๊ฒ ์ต๋๋ค.
"๋๋ ์ํ์ ๋์ฐฉ ํ์ด... ๊ฑฐ๋ฆฌ๋ฅผ ๊ฑด๋์? ๊ฐ์ ๊ฑด๋์?"
- ์ด ๋ฌธ์ฅ์์ ์ํ์ด ์๋ฏธํ๋๊ฒ ๋ญ๊น์?
- ๋ฌธ์ฅ์ Encoding ํ ๋ RNN์ ์ ์ฒด ๋ฌธ์ฅ์ ์ฝ์๋ ๊น์ง ์ํ์ด ๋ฌด์์ ์๋ฏธํ๋์ง ์ดํดํ์ง ๋ชปํ๋ฉฐ ๊ธด Sequence์ ๊ฒฝ์ฐ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆด์๋ ์์ต๋๋ค.
- ์ด์ ๋์กฐ์ ์ผ๋ก Transformer์ Encoder Token์ ์๋ก ๋์์ ์ํธ์์ฉํฉ๋๋ค.
- ์ง๊ด์ ์ผ๋ก Transformer์ Encoder๋ ์ผ๋ จ์ ์ถ๋ก ํ๋ ๋จ๊ณ(Layer-์ ๋ ฅ ๋ฐ์ดํฐ์ ๋ณํ & ์ ๋ณด ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ ๋จ์)๋ก ์๊ฐํ ์ ์์ต๋๋ค.
- ๊ฐ ๋จ๊ณ(๊ฐ Layer)์์์ Token์ ์๋ก๋ฅผ ๋ฐ๋ผ๋ณด๊ณ (Attention - Self Attention), ์ ๋ณด๋ฅผ ๊ตํํ๋ฉด์ ์ ์ฒด ๋ฌธ์ฅ์ ๋งฅ๋ต์์ ์๋ก๋ฅผ ๋ ์ ์ดํดํ๋ ค๊ณ ๋ ธ๋ ฅํฉ๋๋ค.
- ๊ฐ Decoder Layer์์์ Prefix Token์ Self-Attention ๋งค์ปค๋์ฆ์ ํตํด์ ์๋ก ์ํธ ์์ฉ ํ๋ฉด์ Encoder ์ํ๋ฅผ ํ์ธํฉ๋๋ค.
- Attention ๋ฐ Self-Attention์ ๊ดํ ๊ฐ๋ ์ ์์ ๋งํฌ๋ฅผ ๋ฌ์๋์์ผ๋ ํ๋ฒ ์ฝ์ด๋ณด์ธ์!
Transformer: Encoder & Decoder
Transformer ๋ชจ๋ธ์ ํฌ๊ฒ 2๊ฐ์ ๋ถ๋ถ์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค. Encoder์ Decoder๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
์๋์ Encoder & Decoder์ ๊ดํ ์ ์๊ธ์ ์ฐธ๊ณ ํด์ฃผ์ธ์!
- ์๋์ ๊ธ์ seq2seq์ ๋ฐํ Encoder & Decoder์ ๊ดํ์ฌ ์ด ๊ธ์ด์ง๋ง, ๊ธฐ๋ณธ์ ์ธ ๊ฐ๋ ๊ณผ ๊ฐ๋ ์ ์ ์ฌํฉ๋๋ค.
- ๋ค๋ง ๊ตฌ์ฒด์ ์ธ ๊ตฌํ๊ณผ ์๋ ๋ฐฉ์์์์ ์ฐจ์ด๋ ์์ต๋๋ค. Transformer Model์์ Encoder & Decoder ๋ถ๋ถ์ ์ค์ ์ ๋๊ณ ์ค๋ช ํด๋ณด๊ฒ ์ต๋๋ค.
[NLP] Seq2Seq, Encoder & Decoder
1..sequence-to-sequence ๐ก ํธ๋์คํฌ๋จธ(Transformer) ๋ชจ๋ธ์ ๊ธฐ๊ณ ๋ฒ์ญ(machine translation) ๋ฑ ์ํ์ค-ํฌ-์ํ์ค(sequence-to-sequence) ๊ณผ์ ๋ฅผ ์ํํ๊ธฐ ์ํ ๋ชจ๋ธ์ ๋๋ค. sequence: ๋จ์ด ๊ฐ์ ๋ฌด์ธ๊ฐ์ ๋์ด์ ์๋ฏธํฉ
daehyun-bigbread.tistory.com
Transformer ๋ชจ๋ธ์ Encoder
- Encoder: Input Sequence(์ ๋ ฅ ์ํ์ค)๋ฅผ ์ฒ๋ฆฌํ๋ ๊ตฌ์ฑ์์๋ก, ์ฌ๋ฌ๊ฐ์ Encoder Layer๋ฅผ ์์ฐจ์ ์ผ๋ก ์์์ ๊ตฌ์ฑํฉ๋๋ค.
- ๊ฐ Layer๋ Multi-Head Attention ๋งค์ปค๋์ฆ, Position-wise Feed-Forward Network๋ฅผ ํฌํจํ๋ฉฐ, input ๋ฐ์ดํฐ์ ์ ์ฒด์ ์ธ Context๋ฅผ ํจ๊ณผ์ ์ผ๋ก ํฌ์ฐฉํ์ฌ ์ฒ๋ฆฌํฉ๋๋ค.
- Transformer Model์ Encoder๋ ์์ฐจ์ ์ฒ๋ฆฌ๊ฐ ์๋, ์ ์ฒด Input Sequence๋ฅผ ํ๋ฒ์ ์ฒ๋ฆฌํ๋ ์ ์์ RNN ๊ธฐ๋ฐ์ Encoder์ ์ฐจ์ด๊ฐ ์์ต๋๋ค.
Transformer ๋ชจ๋ธ์ Encoder Example Code
์ด ์ฝ๋๋ ์์ ์ฝ๋์ ๋๋ค. ์ฌ์ฉ์์ ๋ง์ถฐ์ ์์ ์ ํด์ผ ํฉ๋๋ค.
- # seq_len: ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋ฐ์๋ ๊ทธ Sequence length๊น์ง๋ง ๋ฐ๊ฒ ๋ค.
class Encoder(tf.keras.layers.Layer):
def __init__(self, **kargs):
super(Encoder, self).__init__()
self.d_model = kargs['d_model']
self.num_layers = kargs['num_layers']
self.embedding = tf.keras.layers.Embedding(kargs['input_vocab_size'], self.d_model)
self.pos_encoding = positional_encoding(kargs['maximum_position_encoding'],
self.d_model)
self.enc_layers = [EncoderLayer(**kargs)
for _ in range(self.num_layers)]
self.dropout = tf.keras.layers.Dropout(kargs['rate'])
def call(self, x, mask):
attn = None
seq_len = tf.shape(x)[1]
# adding embedding and position encoding.
x = self.embedding(x) # (batch_size, input_seq_len, d_model)
x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
x += self.pos_encoding[:, :seq_len, :] # seq_len
x = self.dropout(x)
for i in range(self.num_layers):
x, attn = self.enc_layers[i](x, mask)
return x, attn # (batch_size, input_seq_len, d_model)
Transformer ๋ชจ๋ธ์ Decoder
- Decoder: Target Sequence๋ฅผ ์ฒ๋ฆฌํ๋ ๊ตฌ์ฑ ์์๋ก, ์ฌ๋ฌ๊ฐ์ Decoder Layer๋ฅผ ์์ฐจ์ ์ผ๋ก ์์์ ๊ตฌ์ฑํฉ๋๋ค.
- Decoder Layer๋ Encoder์ ์ ์ฌํ๊ฒ Multi-Head Attention๊ณผ Feed-Forward Networks๋ฅผ ์ฌ์ฉํ์ง๋ง, ์ถ๊ฐ์ ์ผ๋ก Encoder์ ์ถ๋ ฅ & ์ํธ์์ฉํ๋ Encoder-Decoder Attention ๋งค์ปค๋์ฆ์ ํฌํจํฉ๋๋ค.
- ์ด๋ฅผ ํตํด์ Decoder๋ Encoder๊ฐ ์ฒ๋ฆฌํ ์ ๋ณด์ ์์ ์ด ์ง๊ธ๊น์ง ์์ฑํ ์ถ๋ ฅ์ ๊ธฐ๋ฐ์ผ๋ก ๋ค์ ํ ํฐ์ ์์ธกํฉ๋๋ค.
Transformer ๋ชจ๋ธ์ Decoder Example Code
์ด ์ฝ๋๋ ์์ ์ฝ๋์ ๋๋ค. ์ฌ์ฉ์์ ๋ง์ถฐ์ ์์ ์ ํด์ผ ํฉ๋๋ค.
class Decoder(tf.keras.layers.Layer):
def __init__(self, **kargs):
super(Decoder, self).__init__()
self.d_model = kargs['d_model']
self.num_layers = kargs['num_layers']
self.embedding = tf.keras.layers.Embedding(kargs['target_vocab_size'], self.d_model)
self.pos_encoding = positional_encoding(kargs['maximum_position_encoding'], self.d_model)
self.dec_layers = [DecoderLayer(**kargs)
for _ in range(self.num_layers)]
self.dropout = tf.keras.layers.Dropout(kargs['rate'])
def call(self, x, enc_output, look_ahead_mask, padding_mask):
seq_len = tf.shape(x)[1]
attention_weights = {}
x = self.embedding(x) # (batch_size, target_seq_len, d_model)
x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
x += self.pos_encoding[:, :seq_len, :]
x = self.dropout(x)
for i in range(self.num_layers):
x, block1, block2 = self.dec_layers[i](x, enc_output, look_ahead_mask, padding_mask)
attention_weights['decoder_layer{}_block1'.format(i+1)] = block1
attention_weights['decoder_layer{}_block2'.format(i+1)] = block2
# x.shape == (batch_size, target_seq_len, d_model)
return x, attention_weights
Transformer: Layer
Layer์ ๋ฐํ์ฌ ์ค๋ช ์ ํด๋ณด๊ฒ ์ต๋๋ค.
- Transformer ๋ชจ๋ธ์์์ "Layer"๋ Model์ ํต์ฌ ๊ตฌ์ฑ ์์์ค ํ๋๋ก, ์
๋ ฅ ๋ฐ์ดํฐ์ ๋ณํ & ์ ๋ณด ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ ๋จ์์
๋๋ค.
- ๋ชจ๋ธ์ ์ ๋ ฅ์ ๋ง๋๋ ๊ณ์ธต์ Input Layer, ์ถ๋ ฅ์ Output Layer๋ผ๊ณ ํฉ๋๋ค.
- Transformer Model์ ์ผ๋ฐ์ ์ผ๋ก ์ฌ๋ฌ ๊ฐ์ Layer๋ฅผ ์์ฐจ์ ์ผ๋ก ์์์ ๊ตฌ์ฑ๋๋ฉฐ, ๊ฐ Layer๋ ์ ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ ์ฒ๋ฆฌํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ Layer๋ก ์ ๋ฌํฉ๋๋ค.
- ์ด๋ฌํ ๊ตฌ์กฐ๋ฅผ ํตํด์ Model์ Data์ ๋ณต์กํ ํน์ฑ & ํจํด์ ํ์ตํ ์ ์์ต๋๋ค.
- ํฌ๊ฒ Transformer ๋ชจ๋ธ์ Layer๋ 2๊ฐ์ง ์ฃผ์ ๊ตฌ์ฑ ์์๋ก ์ด๋ฃจ์ด์ ธ ์๋๋ฐ, ํ๋๋ Multi-Head Attention, ๋ค๋ฅธ ํ๋๋ Position-wise Feed-Forward Network ์ ๋๋ค.
- ๊ทธ๋ฆฌ๊ณ ๊ฐ Layer๋ ์ ๊ทํ(Normalization)๊ณผ Residual Connection๊ณผ ๊ฐ์ ๊ธฐ๋ฒ์ ์ฌ์ฉํ์ฌ ํ์ต์ ์์ ์ฑ, ํจ์จ์ฑ์ ๋์
๋๋ค.
- Normalization์ Layer์ Input(์ ๋ ฅ), Output(์ถ๋ ฅ)์ ์ ๊ทํ ํ์ฌ ํ์ต๊ณผ์ ์ ์์ ํ ์ํค๋ ์ญํ ์ ํฉ๋๋ค.
- Residual Connection์ Input(์ ๋ ฅ)์ Layer์ Output(์ถ๋ ฅ)์ ์ง์ ๋ํ๋ ๋ฐฉ์์ผ๋ก, ๊น์ ๋คํธ์ํฌ์์ ๋ฐ์ํ ์ ์๋ ํ์ต ๋ฌธ์ ๋ฅผ ์ํํฉ๋๋ค.
Transformer: Input Layer
- Encoder Input์ Source Sequence & Input Embedding ๊ฐ์ ์์น์ ๋ณด๊น์ง ๋ํด์ ๋ง๋ญ๋๋ค.
- ์ด๋ Encoder Input์ Source ์ธ์ด ๋ฌธ์ฅ์ Token Index Sequence๊ฐ ๋ฉ๋๋ค. ํ๋ฒ ์๋ฅผ ๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
Self-Attention์ Input Layer์ ๋์๋ฐฉ์
- ์๋ฅผ ๋ค์ด ์์ค ์ธ์ด์ Token Sequence๊ฐ "์ด์ , ์นดํ, ๊ฐ์์ด"๋ผ๋ฉด Encoder Input Layer(์ธ์ฝ๋ ์ ๋ ฅ์ธต)์ ์ง์ ์ ์ธ ์ ๋ ฅ๊ฐ์ ์ด๋ค Token๋ค์ ๋์ํ๋ ์ธ๋ฑ์ค ์ํ์ค๊ฐ ๋๋ฉฐ Encoder Input(์ธ์ฝ๋ ์ ๋ ฅ)์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ง๋ค์ด์ง๋๋ค.
- ๊ทธ๋ฆฌ๊ณ Input Embedding์ ๋ํ๋ ์์น ์ ๋ณด๋ ํด๋น Token์ด ๋ฌธ์ฅ ๋ด์์ ๋ช๋ฒ์งธ์ ์์น์ธ์ง ์ ๋ณด๋ฅผ ๋ํ๋ ๋๋ค.
"์ด์ " ๊ฐ ์ฒซ๋ฒ์งธ, "์นดํ" ๊ฐ ๋๋ฒ์งธ, "๊ฐ์์ด" ๊ฐ ์ธ๋ฒ์งธ๋ฉด → Transformer Model์ ์ด๊ฐ์ ๋ฐฉ์์ผ๋ก ์์ค ์ธ์ด์ ํ ํฐ ์ํ์ค๋ฅผ ์ด์ ๋์ํ๋ Vector Sequence(๋ฒกํฐ ์ํ์ค)๋ก ๋ณํํด Incoder input์ ๋ง๋ญ๋๋ค.
- ๊ทธ๋ฌ๋ฉด Encoder Input Layer์์ ๋ง๋ค์ด์ง Vector Sequence๊ฐ ์ต์ด์ Encoder Block์ Input์ด ๋ฉ๋๋ค.
- ๊ทธ ๋ค์์๋ Output Vector Sequence๊ฐ ๋๋ฒ์งธ Encoder Block์ Input์ด ๋ฉ๋๋ค.
- ๊ทธ๋ฌ๋ฉด ๋ค์ Encoder ๋ธ๋ญ์ Input(์ ๋ ฅ)์ ์ด์ Block์ Output(์ถ๋ ฅ)์ ๋๋ค. ์ด ๊ณผ์ ์ N๋ฒ ๋ฐ๋ณตํฉ๋๋ค.
Transformer: Encoder Layer Example Code
์ด ์ฝ๋๋ ์์ ์ฝ๋์ ๋๋ค. ์ฌ์ฉ์์ ๋ง์ถฐ์ ์์ ์ ํด์ผ ํฉ๋๋ค.
class EncoderLayer(tf.keras.layers.Layer):
def __init__(self, **kargs):
super(EncoderLayer, self).__init__()
self.mha = MultiHeadAttention(**kargs)
self.ffn = point_wise_feed_forward_network(**kargs)
self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.dropout1 = tf.keras.layers.Dropout(kargs['rate'])
self.dropout2 = tf.keras.layers.Dropout(kargs['rate'])
def call(self, x, mask):
attn_output, _ = self.mha(x, x, x, mask) # (batch_size, input_seq_len, d_model)
attn_output = self.dropout1(attn_output)
out1 = self.layernorm1(x + attn_output) # (batch_size, input_seq_len, d_model)
ffn_output = self.ffn(out1) # (batch_size, input_seq_len, d_model)
ffn_output = self.dropout2(ffn_output)
out2 = self.layernorm2(out1 + ffn_output) # (batch_size, input_seq_len, d_model)
return out2, attn_output
Transformer: Output Layer
Output Layer๋ Transformer ๋ชจ๋ธ์ ์ถ๋ ฅ์ธต์ ๋๋ค.
- Output Layer์ ์ถ๋ ฅ์ Target ์ธ์ด์ ์ดํ์๋งํผ ์ฐจ์์ ๊ฐ์ง๋ ํ๋ฅ ๋ฒกํฐ์ ๋๋ค.
- Multi-Head Attention์ Attention ๊ด๋ จ ๊ธ์์ ๋ค๋ฃจ์์ผ๋ฏ๋ก, ์ฒ์ ๋ณด๋ Position-wise Feed-Forward Network์ ๋ฐํ์ฌ ์ค๋ช ํด ๋ณด๊ฒ ์ต๋๋ค.
Transformer: Decoder Layer Example Code
์ด ์ฝ๋๋ ์์ ์ฝ๋์ ๋๋ค. ์ฌ์ฉ์์ ๋ง์ถฐ์ ์์ ์ ํด์ผ ํฉ๋๋ค.
class DecoderLayer(tf.keras.layers.Layer):
def __init__(self, **kargs):
super(DecoderLayer, self).__init__()
self.mha1 = MultiHeadAttention(**kargs)
self.mha2 = MultiHeadAttention(**kargs)
self.ffn = point_wise_feed_forward_network(**kargs)
self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.layernorm3 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.dropout1 = tf.keras.layers.Dropout(kargs['rate'])
self.dropout2 = tf.keras.layers.Dropout(kargs['rate'])
self.dropout3 = tf.keras.layers.Dropout(kargs['rate'])
def call(self, x, enc_output, look_ahead_mask, padding_mask):
# enc_output.shape == (batch_size, input_seq_len, d_model)
attn1, attn_weights_block1 = self.mha1(x, x, x, look_ahead_mask) # (batch_size, target_seq_len, d_model)
attn1 = self.dropout1(attn1)
out1 = self.layernorm1(attn1 + x)
attn2, attn_weights_block2 = self.mha2(
enc_output, enc_output, out1, padding_mask) # (batch_size, target_seq_len, d_model)
attn2 = self.dropout2(attn2)
out2 = self.layernorm2(attn2 + out1) # (batch_size, target_seq_len, d_model)
ffn_output = self.ffn(out2) # (batch_size, target_seq_len, d_model)
ffn_output = self.dropout3(ffn_output)
out3 = self.layernorm3(ffn_output + out2) # (batch_size, target_seq_len, d_model)
return out3, attn_weights_block1, attn_weights_block2
Feed-Forward Networks
Position-wise Feed-Forward Network์ ๋ฐํ์ฌ ์ค๋ช ์ ๋๋ฆด๋ ค๊ณ ํ๋๋ฐ, ์ด๊ฑด Transformer ๋ชจ๋ธ์ ํน์ Context(๋ฌธ๋งฅ)์์๋ง ์ฌ์ฉ๋๋ FFN(Feed-Forward Network)์ด๋ผ๊ณ ํฉ๋๋ค. ๊ทธ๋์ ์ผ๋จ Feed-Forward Network์ ๋ฐํ ์ค๋ช ์ ๋จผ์ ํ ๋ค์์ Position-wise Feed-Forward Network์ ๋ฐํ ์ค๋ช ์ ์ด์ด์ ํ๋๋ก ํ๊ฒ ์ต๋๋ค.
Feed-Forward Networks (FFN)
- Feed-Forward Networks๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ธ๊ณต ์ ๊ฒฝ๋ง ๊ตฌ์กฐ์ค ํ๋๋ก, Input Layer(์ ๋ ฅ์ธต)์์ Output Layer(์ถ๋ ฅ์ธต)์ผ๋ก ๋ฐ์ดํฐ๊ฐ ์๋ฐฉํฅ์ผ๋ก ํ๋ฅด๋ ๊ตฌ์กฐ๋ฅผ ์๋ฏธํฉ๋๋ค.
- ์ฌ๊ธฐ์ Data๋ ๊ฐ Layer(์ธต)์ ์ง๋ ๋๋ง๋ค ๊ฐ์ค์น์ ์ํด ๋ณํ๋๊ณ , Activation Function(ํ์ฑํ ํจ์)๋ฅผ ํตํด ๋ค์ Layer(์ธต)์ผ๋ก ์ ๋ฌ๋ฉ๋๋ค.
- ์ด๋ฌํ ๋คํธ์ํฌ๋ ์ํ ์ฐ๊ฒฐ์ด๋ ๋ณต์กํ Feedback ๋ฃจํ๊ฐ ์์ด์ ๊ณ์ฐ์ด ๋น๊ต์ ๊ฐ๋จํ๊ณ , ๋ค์ํ ๋ฌธ์ ์ ์ ์ฉ๋ ์ ์์ต๋๋ค.
- ์ ๋ฆฌํ์๋ฉด, ๋ฐ์ดํฐ๊ฐ ๋คํธ์ํฌ๋ฅผ ํตํด ํ ๋ฐฉํฅ์ผ๋ก๋ง ํ๋ฅธ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ์ ๋ ฅ ๋ฐ์ดํฐ๋ Input Layer(์ ๋ ฅ์ธต)์์ ์์ํ์ฌ Hidden Layer(์๋์ธต)์ ๊ฑฐ์ณ Output Layer(์ถ๋ ฅ์ธต)์ผ๋ก ์ ๋ฌ๋๋ฉฐ, ๊ฐ ์ธต์์๋ Activation Function(ํ์ฑํ ํจ์)๋ฅผ ํตํด ์ฒ๋ฆฌ๋ฉ๋๋ค. ์ด ๊ณผ์ ์์ ์ํ(loop)์ด๋ ๋๋์๊ฐ๋(feedback) ์ฐ๊ฒฐ์ ์์ผ๋ฉฐ, ๊ฐ ์ธต์ ์ด์ ์ธต์ ์ถ๋ ฅ์ ๋ค์ ์ธต์ Input(์ ๋ ฅ)์ผ๋ก๋ง ์ฌ์ฉํฉ๋๋ค.
Feed-Forward Networks (FFN)์ ๊ธฐ๋ณธ์ ์ธ ํํ
- FNN(Feed-Forward Networks)์ ์ธ๊ณต์ ๊ฒฝ๋ง์ ๊ธฐ๋ณธ์ ์ธ ํํ์ ๋๋ค.
- ๋ค์์ Input(์ ๋ ฅ) Node, Weight(๊ฐ์ค์น), Activation Function(ํ์ฑํ ํจ์)๋ฅผ ํตํด ์ถ๋ ฅ ๋ ธ๋๋ก ์ ๋ณด๋ฅผ ์ ๋ฌํฉ๋๋ค.
- ์ด๋ Weight(๊ฐ์ค์น)๋ ํ์ต ๊ณผ์ ์์ ์ ๋ฐ์ดํธ ๋๋ฉฐ, ์ด๊ธฐ Weight(๊ฐ์ค์น)๋ ๋ณดํต ๋ฌด์์๋ก ๊ฒฐ์ ๋ฉ๋๋ค.
- FNN(Feed-Forward Networks)๋ MLP, Multi-Layer Perceptron(๋ค์ค ํผ์ ํธ๋ก )์ด๋ผ๊ณ ๋ ๋ถ๋ฆฌ๋ฉฐ, Hidden Layer(์๋์ธต)์ด ํ๋ ์ด์์ธ ์ธ๊ณต์ ๊ฒธ๋ง์ ์๋ฏธํฉ๋๋ค.
Feed-Forward Networks (FFN)์ ๋คํธ์ํฌ ๊ตฌ์กฐ
- ์ด ๊ธ์์๋ Transformer ๋ชจ๋ธ์ ๋ค๋ฃจ๊ธฐ ์ํด์ ์์ธํ ์ค๋ช ์ ๋์ค์ Deep_Learning ํํธ์์ ์ค๋ช ํ๋๋ก ํ๊ฒ ์ต๋๋ค.
Summary: "Feed-Forward Networks"๋ ๋์ ์๋ฏธ์์๋ ๋ฐ์ดํฐ๊ฐ ์๋ฐฉํฅ์ผ๋ก ์ ๋ฌ๋๋ ์ ๊ฒฝ๋ง ๊ตฌ์กฐ๋ฅผ ์๋ฏธํ๋ฉฐ, ๋ค์ํ Context ๋ฐ ๊ตฌ์กฐ์์ ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
Position-wise Feed-Forward Networks(PFFN)
Position-wise Feed-Forward Networks ์ ๋ํ ์ ์๋ฅผ ํ๋ฒ ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
Transformer๋ด์์ Position-wise Feed-Forward Networks์ ์ ์
- Transformer ๋ชจ๋ธ ๋ด์์ "Position-wise Feed-Forward Networks"๋ ํน๋ณํ ์ ํ์ FFN(Feed-Forward Networks) - *Fully Connected Feed-Forward Network ์ ์๋ฏธํฉ๋๋ค.
- ์ด ๋คํธ์ํฌ๋ Transformer ๋ชจ๋ธ ๋ด์์ ๊ฐ Encoder, Decoder ๋ด์ ์กด์ฌํ๋ฉฐ, Sequence์ ๊ฐ ์์น(Position)์ ์๋ Word Vector(๋จ์ด ๋ฒกํฐ)์ ๋ ๋ฆฝ์ ์ผ๋ก ๋์ผํ ๋คํธ์ํฌ๋ฅผ ์ ์ฉํฉ๋๋ค.
- ์ฆ, Sequence ๋ด ๋ชจ๋ ๋จ์ด(or Token)๋ค์ ๊ฐ์์ ์์น์์ FFN(Feed-Forward Networks)๋ฅผ ๊ฑฐ์น๊ฒ ๋๋๋ฐ, ๋ณดํต 2๊ฐ์ Linear Transformation(์ ํ ๋ณํ) & ํ๋์ NonLinear Function (๋น์ ํ ํจ์ - Ex. ReLUํจ์)๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
- Position-wise๋ผ๋ ์ฉ์ด๋ ์ด ๋คํธ์ํฌ์ Sequence๊ฐ ๊ฐ ์์น๋ง๋ค ๋ ๋ฆฝ์ ์ผ๋ก ์ ์ฉ๋๋ค๋ ์ ์ ๊ฐ์กฐํฉ๋๋ค.
* Fully Connected Feed-Forward Network: ๋คํธ์ํฌ์ ํ์ธต์ ์๋ ๋ชจ๋ Neuron์ด ์ด์ ์ธต(Layer)์ ๋ชจ๋ Neuron๊ณผ ์ฐ๊ฒฐ๋์ด ์์์ ์๋ฏธํฉ๋๋ค. ์ฆ, Fully Connected ๊ตฌ์กฐ๋ ๋คํธ์ํฌ์ ๊ฐ Layer๊ฐ ์ ์ฒด์ ์ผ๋ก ์ฐ๊ฒฐ๋ ๊ตฌ์กฐ๋ฅผ ์๋ฏธํฉ๋๋ค.
Position-wise Feed-Forward Networks
- x์ Linear Transformation(์ ํ ๋ณํ)์ ์ ์ฉํ ๋ค, NonLinear Function(๋น์ ํํจ์)์ธ ReLUํจ์(max(0, z)๋ฅผ ๊ฑฐ์ณ ๋ค์ํ๋ฒ Linear Transformation(์ ํ ๋ณํ)์ ์ ์ฉํฉ๋๋ค.
- ์ด๋ ๊ฐ๊ฐ์ Position๋ง๋ค ๊ฐ์ Parameter(๋งค๊ฐ๋ณ์)์ธ W, b๋ฅผ ์ฌ์ฉํ์ง๋ง, ๋ง์ฝ Layer๊ฐ ๋ฌ๋ผ์ง๋ฉด ๋ค๋ฅธ Parameter๋ฅผ ์ฌ์ฉํฉ๋๋ค.
Point-Wise Feed Forward Network Example Code
์ด ์ฝ๋๋ ์์ ์ฝ๋์ ๋๋ค. ์ฌ์ฉ์์ ๋ง์ถฐ์ ์์ ์ ํด์ผ ํฉ๋๋ค.
def point_wise_feed_forward_network(**kargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(kargs['dff'], activation='relu'), # (batch_size, seq_len, dff)
tf.keras.layers.Dense(kargs['d_model']) # (batch_size, seq_len, d_model)
])
Self-Attention
Self-Attention์ ๊ดํ ๋ด์ฉ์ Attention์ ๊ดํ ๊ธ์ ์ค๋ช ์ ํด๋์์ผ๋, ์ด ๊ธ์์๋ ๊ฐ๋ ๋ง ์ค๋ช ํ๊ฒ ์ต๋๋ค. ์์ ์ฝ๋๋ ์๋์ ๊ธ์ ์ฐธ๊ณ ํด์ฃผ์ธ์!
[NLP] Attention - ์ดํ ์
1. Attention Attention์ CS ๋ฐ ML์์ ์ค์ํ ๊ฐ๋ ์ค ํ๋๋ก ์ฌ๊ฒจ์ง๋๋ค. Attention์ ๋งค์ปค๋์ฆ์ ์ฃผ๋ก Sequence Data๋ฅผ ์ฒ๋ฆฌํ๊ฑฐ๋ ์์ฑํ๋ ๋ชจ๋ธ์์ ์ฌ์ฉ๋ฉ๋๋ค. -> Sequence ์ ๋ ฅ์ ์ํํ๋ ๋จธ์ ๋ฌ๋ ํ์ต
daehyun-bigbread.tistory.com
Self-Attention ๊ธฐ๋ฒ์ Attention ๊ธฐ๋ฒ์ ๋ง ๊ทธ๋๋ก ์๊ธฐ ์์ ์๊ฒ ์ํํ๋ Attention ๊ธฐ๋ฒ์ ๋๋ค.
- Input Sequence ๋ด์ ๊ฐ ์์๊ฐ์ ์๋์ ์ธ ์ค์๋๋ฅผ ๊ณ์ฐํ๋ ๋งค์ปค๋์ฆ์ด๋ฉฐ, Sequence์ ๋ค์ํ ์์น๊ฐ์ ์๋ก ๋ค๋ฅธ ๊ด๊ณ๋ฅผ ํ์ตํ ์ ์์ต๋๋ค.
- Sequence ์์ ๊ฐ์ด๋ฐ Task ์ํ์ ์ค์ํ Element(์์)์ ์ง์คํ๊ณ ๊ทธ๋ ์ง ์์ Element(์์)๋ ๋ฌด์ํฉ๋๋ค.
- ์ด๋ฌ๋ฉด Task๊ฐ ์ํํ๋ ์ฑ๋ฅ์ด ์์นํ ๋ฟ๋๋ฌ Decoding ํ ๋ Source Sequence ๊ฐ์ด๋ฐ ์ค์ํ Element(์์)๋ค๋ง ์ถ๋ฆฝ๋๋ค.
- ๋ฌธ๋งฅ์ ๋ฐ๋ผ ์ง์คํ ๋จ์ด๋ฅผ ๊ฒฐ์ ํ๋ ๋ฐฉ์์ ์๋ฏธ -> ์ค์ํ ๋จ์ด์๋ง ์ง์ค์ ํ๊ณ ๋๋จธ์ง๋ ๊ทธ๋ฅ ์ฝ์ต๋๋ค.
- ์ด ๋ฐฉ๋ฒ์ด ๋ฌธ๋งฅ์ ํ์ ํ๋ ํต์ฌ์ด๋ฉฐ, ์ด๋ฌํ ๋ฐฉ์์ Deep Learning ๋ชจ๋ธ์ ์ ์ฉํ๊ฒ์ด 'Attention' ๋งค์ปค๋์ฆ์ด๋ฉฐ, ์ด ๋งค์ปค๋์ฆ์ ์๊ธฐ ์์ ์๊ฒ ์ ์ฉํ๊ฒ์ด 'Self-Attention' ์ ๋๋ค. ํ๋ฒ ๋งค์ปค๋์ฆ์ ์์๋ฅผ ๋ค์ด์ ์ค๋ช ํด ๋ณด๊ฒ ์ต๋๋ค.
Self-Attention์ ๊ณ์ฐ ์์
Self-Attention์ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ) 3๊ฐ์ง ์์๊ฐ ์๋ก ์ํฅ์ ์ฃผ๊ณ ๋ฐ๋ ๊ตฌ์กฐ์ ๋๋ค.
- ๋ฌธ์ฅ๋ด ๊ฐ ๋จ์ด๊ฐ Vector(๋ฒกํฐ) ํํ๋ก input(์ ๋ ฅ)์ ๋ฐ์ต๋๋ค.
* Vector: ์ซ์์ ๋์ด ์ ๋
- ๊ฐ ๋จ์ด์ Vector๋ 3๊ฐ์ง ๊ณผ์ ์ ๊ฑฐ์ณ์ ๋ฐํ์ด ๋ฉ๋๋ค.
- Query(์ฟผ๋ฆฌ) - ๋ด๊ฐ ์ฐพ๊ณ ์ ๋ณด๋ฅผ ์์ฒญํ๋๊ฒ ์ ๋๋ค.
- Key(ํค) - ๋ด๊ฐ ์ฐพ๋ ์ ๋ณด๊ฐ ์๋ ์ฐพ์๋ณด๋ ๊ณผ์ ์ ๋๋ค.
- Value(๋ฐธ๋ฅ) - ์ฐพ์์ ์ ๊ณต๋ ์ ๋ณด๊ฐ ๊ฐ์น ์๋์ง ํ๋จํ๋ ๊ณผ์ ์ ๋๋ค.
- ์์ ๊ทธ๋ฆผ์ ๋ณด๋ฉด ์ ๋ ฅ๋๋ ๋ฌธ์ฅ "์ด์ ์นดํ ๊ฐ์์ด ๊ฑฐ๊ธฐ ์ฌ๋ ๋ง๋๋ผ" ์ด 6๊ฐ ๋จ์ด๋ก ๊ตฌ์ฑ๋์ด ์๋ค๋ฉด?
- ์ฌ๊ธฐ์์ Self-Attention ๊ณ์ฐ ๋์์ Query(์ฟผ๋ฆฌ) Vector 6๊ฐ, Key(ํค) Vector 6๊ฐ, Value(๋ฐธ๋ฅ) Vector 6๊ฐ๋ฑ ๋ชจ๋ 18๊ฐ๊ฐ ๋ฉ๋๋ค.
- ์์ ํ๋ ๋ ์ธ๋ถ์ ์ผ๋ก ๋ํ๋ธ๊ฒ์ ๋๋ค. Self-Attention์ Query ๋จ์ด ๊ฐ๊ฐ์ ๋ํด ๋ชจ๋ Key ๋จ์ด์ ์ผ๋ง๋ ์ ๊ธฐ์ ์ธ ๊ด๊ณ๋ฅผ ๋งบ๊ณ ์๋์ง์ ํ๋ฅ ๊ฐ๋ค์ ํฉ์ด 1์ธ ํ๋ฅ ๊ฐ์ผ๋ก ๋ํ๋ ๋๋ค.
- ์ด๊ฒ์ ๋ณด๋ฉด Self-Attention ๋ชจ๋์ Value(๋ฐธ๋ฅ) Vector๋ค์ Weighted Sum(๊ฐ์คํฉ)ํ๋ ๋ฐฉ์์ผ๋ก ๊ณ์ฐ์ ๋ง๋ฌด๋ฆฌ ํฉ๋๋ค.
- ํ๋ฅ ๊ฐ์ด ๊ฐ์ฅ ๋์ ํค ๋จ์ด๊ฐ ์ฟผ๋ฆฌ ๋จ์ด์ ๊ฐ์ฅ ๊ด๋ จ์ด ๋์ ๋จ์ด๋ผ๊ณ ํ ์ ์์ต๋๋ค.
- ์ฌ๊ธฐ์๋ '์นดํ'์ ๋ํด์๋ง ๊ณ์ฐ ์์๋ฅผ ๋ค์์ง๋ง, ์ด๋ฌํ ๋ฐฉ์์ผ๋ก ๋๋จธ์ง ๋จ์ด๋ค์ค Self-Attention์ ๊ฐ๊ฐ ์ํํฉ๋๋ค.
Self-Attention์ ๋์ ๋ฐฉ์
Self-Attention์์ ๊ฐ์ฅ ์ค์ํ ๊ฐ๋ ์ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ)์ ์์๊ฐ์ด ๋์ผํ๋ค๋ ์ ์ ๋๋ค.
- ๊ทธ๋ ๋ค๊ณ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ)๊ฐ ๋์ผ ํ๋ค๋ ๋ง์ด ์ด๋๋๋ค.
- ๊ทธ๋ฆผ์ ๋ณด๋ฉด ๊ฐ์ค์น Weight W๊ฐ์ ์ํด์ ์ต์ข ์ ์ธ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ)๊ฐ์ ์๋ก ๋ฌ๋ผ์ง๋๋ค.
Attention์ ๊ตฌํ๋ ๊ณต์์ ๋ณด๊ฒ ์ต๋๋ค.
- ์ผ๋จ Query(์ฟผ๋ฆฌ)๋ Key(ํค)๋ฅผ ๋ด์ ํด์ค๋๋ค. ์ด๋ ๊ฒ ๋ด์ ์ ํด์ฃผ๋ ์ด์ ๋ ๋ ์ฌ์ด์ ์ฐ๊ด์ฑ์ ๊ณ์ฐํ๊ธฐ ์ํด์์ ๋๋ค.
- ์ด ๋ด์ ๋ ๊ฐ์ "Attention Score"๋ผ๊ณ ํฉ๋๋ค. Dot-Product Attention ๋ถ๋ถ์์ ์์ธํ ์ค๋ช ํ์ง๋ง ์ด๋ฒ์๋ ๊ฐ๋จํ๊ฒ ์ค๋ช ํด ๋ณด๊ฒ ์ต๋๋ค.
- ๋ง์ฝ, Query(์ฟผ๋ฆฌ)๋ Key(ํค)์ Dimension(์ฐจ์)์ด ์ปค์ง๋ฉด, ๋ด์ ๊ฐ์ Attention Score๊ฐ ์ปค์ง๊ฒ ๋์ด์ ๋ชจ๋ธ์ด ํ์ตํ๋๋ฐ ์ด๋ ค์์ด ์๊น๋๋ค.
- ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ ์ฐจ์ d_k์ ๋ฃจํธ๋งํผ ๋๋์ด์ฃผ๋ Scaling ์์ ์ ์งํํฉ๋๋ค. ์ด๊ณผ์ ์ "Scaled Dot-Product Attention" ์ด๋ผ๊ณ ํฉ๋๋ค.
- ๊ทธ๋ฆฌ๊ณ Scaled Dot-Product Attention"์ ์งํํ ๊ฐ์ ์ ๊ทํ๋ฅผ ์งํํด์ฃผ๊ธฐ ์ํด์ Softmax ํจ์๋ฅผ ๊ฑฐ์ณ์ ๋ณด์ ์ ์ํด ๊ณ์ฐ๋ scoreํ๋ ฌ, valueํ๋ ฌ์ ๋ด์ ํฉ๋๋ค. ๊ทธ๋ฌ๋ฉด ์ต์ข ์ ์ผ๋ก Attention ํ๋ ฌ์ ์ป์ ์ ์๊ฒ ๋ฉ๋๋ค. ์์ ๋ฌธ์ฅ์ ๋ค์ด์ ์ค๋ช ํด ๋ณด๊ฒ ์ต๋๋ค.
"I am a student"๋ผ๋ ๋ฌธ์ฅ์ผ๋ก ์์๋ฅผ ๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
- Self-Attention์ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ) 3๊ฐ ์์ ์ฌ์ด์ ๋ฌธ๋งฅ์ ๊ด๊ณ์ฑ์ ์ถ์ถํฉ๋๋ค.
Q = X * Wq, K = X * Wk, W = X * Wv
- ์์ ์์์ฒ๋ผ Input Vector Sequence(์ ๋ ฅ ๋ฒกํฐ ์ํ์ค) X์ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ)๋ฅผ ๋ง๋ค์ด์ฃผ๋ ํ๋ ฌ(W)๋ฅผ ๊ฐ๊ฐ ๊ณฑํด์ค๋๋ค.
- Input Vector Sequence(์ ๋ ฅ ๋ฒกํฐ ์ํ์ค)๊ฐ 4๊ฐ์ด๋ฉด ์ผ์ชฝ์ ์๋ ํ๋ ฌ์ ์ ์ฉํ๋ฉด Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ) ๊ฐ๊ฐ 4๊ฐ์ฉ, ์ด 12๊ฐ๊ฐ ๋์ต๋๋ค.
* Word Embedding: ๋จ์ด๋ฅผ Vector๋ก ๋ณํํด์ Dense(๋ฐ์ง)ํ Vector๊ณต๊ฐ์ Mapping ํ์ฌ ์ค์ Vector๋ก ํํํฉ๋๋ค.
- ๊ฐ ๋จ์ด์ ๋ฐํ์ฌ Word Embedding(๋จ์ด ์๋ฒ ๋ฉ)์ ํฉ๋๋ค. ๋จ์ด 'i'์ Embedding์ด [1,1,1,1]์ด๋ผ๊ณ ํ์ ๋, ์ฒ์ 'i'์ ์ฒ์ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ)๋ฅผ ๊ฐ๊ฐ 'Q_i, original', 'K_i, original', 'V_i, original', ๋ผ๊ณ ํฉ๋๋ค.
- Embedding์ ๊ฐ์ด ๋ค [1,1,1,1]๋ก ๊ฐ์ ์ด์ ๋ Self-Attention ๋งค์ปค๋์ฆ์์๋ ๊ฐ์์ผ ํ๊ธฐ ๋๋ฌธ์ ๋ชจ๋ [1,1,1,1]์ด๋ผ๊ณ ๋์ผํฉ๋๋ค.
- ํ์ต๋ Weight(๊ฐ์ค์น)๊ฐ์ด 'WQ', 'WK', 'WV'๋ผ๊ณ ํ ๋ Original ๊ฐ๋ค๊ณผ ์ ๊ณฑ์ ํด์ฃผ๋ฉด ์ต์ข
์ ์ผ๋ก 'Q', 'K', 'V'๊ฐ์ด ๋์ถ๋ฉ๋๋ค.
- 'Q', 'K', 'V'๊ฐ์ ์ด์ฉํด์ ์์์ ์ค์ ํ ๋ณด์ ๋ 'Attention Score'๋ฅผ ๊ณฑํด์ฃผ๋ฉด ์๋์ ์ผ์ชฝ์๊ณผ ๊ฐ์ด 1.5๋ผ๋ ๊ฐ์ด ๋์ต๋๋ค.
- ํ๋ ฌ 'Q', 'K'๋ ์๋ก ์ ๊ณฑ ๊ณ์ฐํด์ฃผ๊ณ , ์ฌ๊ธฐ์ ํ๋ ฌ 'Q', 'K', 'V'์ Dimension(์ฐจ์)์ 4์ด๋ฏ๋ก ๋ฃจํธ 4๋ก ๋๋์ด์ค๋๋ค.
- 'i' ๋ฟ๋ง ์๋๋ผ ๋ชจ๋ ๋จ์ด๊ฐ์ 'Self-Attention'์ ํด์ฃผ๋ฉด ์์ ์ค๋ฅธ์ชฝ ํ๋ ฌ๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ต๋๋ค.
- ๊ฐ์ด๋ฐ ๋ ธ๋ฝ์ ๋ถ๋ถ์ ์๊ธฐ ์์ ์ ๋ํ 'Attention'์ด๋ฏ๋ก ๋น์ฐํ ๊ฐ์ด ์ ์ผ ํฌ๊ณ , ์์ชฝ ์ด๋ก์ ๋ถ๋ถ์ ๋ณด๋ฉด ์ ์๊ฐ ๋์ต๋๋ค.
- ์์ ๊ทธ๋ฆผ์ ๋จ์ด ํ๋ํ๋์ 'Attention'์ ๊ตฌํ๋ ๊ณผ์ ์ ๋์ํ ํ ๊ทธ๋ฆผ์ ๋๋ค.
- ์ค์ ๋ก๋ ์ฌ๋ฌ ๋จ์ด๋ฅผ ์์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ฅผ ํด์ ๊ณ์ฐ์ ํฉ๋๋ค.
- ๋ณ๋ ฌ์ฒ๋ฆฌ๋ฅผ ํ๋ฉด ์ฐ์ฐ์๋๊ฐ ๋นจ๋ฆฌ์ง๋ ์ด์ ์ด ์๊ธฐ ๋๋ฌธ์ ๋๋ค. ๊ฐ๋จํ 'Self-Attention' ๊ณผ์ ์ ์์ฝํด์ ์ค๋ช ํด๋ณด๊ฒ ์ต๋๋ค.
Self-Attention ๊ณผ์ Summary
1. ์ํ๋ ๋ฌธ์ฅ์ ์๋ฒ ๋ฉํ๊ณ ํ์ต์ ํตํด ๊ฐ Query, Key, Value์ ๋ง๋ weight๋ค์ ๊ตฌํด์ค.
2. ๊ฐ ๋จ์ด์ ์๋ฒ ๋ฉ์ Query, Key, Value(Query = Key = Value)์ weight๋ฅผ ์ ๊ณฑ(๋ด์ )ํด ์ต์ข Q, K, V๋ฅผ ๊ตฌํจ.
3. Attention score ๊ณต์์ ํตํด ๊ฐ ๋จ์ด๋ณ Self Attention value๋ฅผ ๋์ถ
4. Self Attention value์ ๋ด๋ถ๋ฅผ ๋น๊ตํ๋ฉด์ ์๊ด๊ด๊ณ๊ฐ ๋์ ๋จ์ด๋ค์ ๋์ถ
Multi-Head Attention
Multi-Head Attention๋ ๊ฐ๋ ๋ง ์ค๋ช ํ๊ฒ ์ต๋๋ค. ์์ ์ฝ๋๋ ์์ Attention ๊ด๋ จํ ๊ธ์ ์์ ์ฝ๋๋ฅผ ๋ง๋ค์ด ๋์์ผ๋ ํ์ํ์ ๋ถ์ ํ์ธํด์ฃผ์ธ์!
Multi-Head Attention์ ์ฌ๋ฌ๊ฐ์ 'Attention Head'๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ์ฌ๊ธฐ์ ๊ฐ 'Head Attention'์์ ๋์จ ๊ฐ์ ์ฐ๊ฒฐํด ์ฌ์ฉํ๋ ๋ฐฉ์์ ๋๋ค.
- ํ๋ฒ์ 'Attention'์ ์ด์ฉํ์ฌ ํ์ต์ ์ํค๋๊ฒ ๋ณด๋ค๋ 'Attention'์ ๋ณ๋ ฌ๋ก ์ฌ๋ฌ๊ฐ ์ฌ์ฉํ๋ ๋ฐฉ์์ ๋๋ค.
- ์์๋ ์๋์ Query(์ฟผ๋ฆฌ), Key(ํค) Value(๋ฐธ๋ฅ) ํ๋ ฌ ๊ฐ์ Head์ ๋งํผ ๋ถํ ํฉ๋๋ค.
- ๋ถํ ํ ํ๋ ฌ ๊ฐ์ ํตํด, ๊ฐ 'Attention' Value๊ฐ๋ค์ ๋์ถํฉ๋๋ค.
- ๋์ถ๋ 'Attention value'๊ฐ๋ค์ concatenate(์์ ํฉ์น๊ธฐ)ํ์ฌ์ ์ต์ข 'Attention Value'๋ฅผ ๋์ถํฉ๋๋ค.
- [4x4] ํฌ๊ธฐ์ ๋ฌธ์ฅ Embedding Vector์ [4x8]์ Query(์ฟผ๋ฆฌ), Key(ํค) Value(๋ฐธ๋ฅ)๊ฐ ์์ ๋, ์ผ๋ฐ์ ์ธ ํ ๋ฒ์ ๊ณ์ฐํ๋ Attention ๋ฉ์ปค๋์ฆ์ [4x4]*[4x8]=[4x8]์ 'Attention Value'๊ฐ ํ ๋ฒ์ ๋์ถ๋ฉ๋๋ค.
- 'Multi-Head Attention' ๋งค์ปค๋์ฆ์ผ๋ก ๋ณด๋ฉด ์ฌ๊ธฐ์ Head๋ 4๊ฐ ์ ๋๋ค. 'I, am, a, student'
- Head๊ฐ 4๊ฐ ์ด๋ฏ๋ก ๊ฐ ์ฐ์ฐ๊ณผ์ ์ด 1/4๋งํผ ํ์ํฉ๋๋ค.
- ์์ ๊ทธ๋ฆผ์ผ๋ก ๋ณด๋ฉด ํฌ๊ธฐ๊ฐ [4x8]์ด์๋, Query(์ฟผ๋ฆฌ), Key(ํค) Value(๋ฐธ๋ฅ)๋ฅผ 4๋ฑ๋ถ ํ์ฌ [4x2]๋ก ๋ง๋ญ๋๋ค. ๊ทธ๋ฌ๋ฉด ์ฌ๊ธฐ์์ 'Attention Value'๋ [4x2]๊ฐ ๋ฉ๋๋ค.
- ์ด 'Attention Value'๋ค์ ๋ง์ง๋ง์ผ๋ก Concatenate(ํฉ์ณ์ค๋ค)ํฉ์ณ์ฃผ๋ฉด, ํฌ๊ธฐ๊ฐ [4x8]๊ฐ ๋์ด ์ผ๋ฐ์ ์ธ Attention ๋งค์ปค๋์ฆ์ ๊ฒฐ๊ณผ๊ฐ๊ณผ ๋์ผํ๊ฒ ๋ฉ๋๋ค. ์์๋ฅผ ํ๋ฒ ๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
Summary: Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ)๊ฐ์ ํ ๋ฒ์ ๊ณ์ฐํ์ง ์๊ณ head ์๋งํผ ๋๋ ๊ณ์ฐ ํ ๋์ค์ Attention Value๋ค์ ํฉ์น๋ ๋ฉ์ปค๋์ฆ. ํ๋ง๋๋ก ๋ถํ ๊ณ์ฐ ํ ํฉ์ฐํ๋ ๋ฐฉ์.
Multi-Head Attention Example
- ์ ๋ ฅ ๋จ์ด ์๋ 2๊ฐ, ๋ฐธ๋ฅ์ ์ฐจ์์๋ 3, ํค๋๋ 8๊ฐ์ธ ๋ฉํฐ-ํค๋ ์ดํ ์ ์ ๋ํ๋ธ ๊ทธ๋ฆผ ์ ๋๋ค.
- ๊ฐ๋ณ ํค๋์ ์ ํ ์ดํ ์ ์ํ ๊ฒฐ๊ณผ๋ ‘์ ๋ ฅ ๋จ์ด ์ ×, ๋ฐธ๋ฅ ์ฐจ์์’, ์ฆ 2×3 ํฌ๊ธฐ๋ฅผ ๊ฐ๋ ํ๋ ฌ์ ๋๋ค.
- 8๊ฐ ํค๋์ ์ ํ ์ดํ ์ ์ํ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ๊ทธ๋ฆผ์ โ ์ฒ๋ผ ์ด์ด ๋ถ์ด๋ฉด 2×24 ์ ํ๋ ฌ์ด ๋ฉ๋๋ค.
- Multi-Head Attention์ ์ต์ข ์ํ ๊ฒฐ๊ณผ๋ '์ ๋ ฅ ๋จ์ด ์' x '๋ชฉํ ์ฐจ์ ์' ์ด๋ฉฐ, Encoder, Decoder Block ๋ชจ๋์ ์ ์ฉ๋ฉ๋๋ค.
Multi-Head Attention์ ๊ฐ๋ณ Head์ Self-Attention ์ํ ๊ฒฐ๊ณผ๋ฅผ ์ด์ด ๋ถ์ธ ํ๋ ฌ (โ )์ W0๋ฅผ ํ๋ ฌ๊ณฑํด์ ๋ง๋ฌด๋ฆฌ ๋๋ค.
→ ์ ํ ์ดํ ์ ์ํ ๊ฒฐ๊ณผ ํ๋ ฌ์ ์ด(column)์ ์ × ๋ชฉํ ์ฐจ์์
Multi-Head Attention ๋ถ๊ฐ ์ค๋ช
- ์์ ์ฌ์ง์์๋ ํ๋์ Head-Attention์ ์ํด ๊ณ์ฐํ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ) ๊ฐ์ ์ฌ๋ฌ ๋ถ๋ถ์ผ๋ก ๋ถํ ํฉ๋๋ค.
- ์ด๋ฌํ ๋ฐฉ์์ผ๋ก ํ๋ ๋๋ ์ฌ๋ฌ๊ฐ์ Attention Head๊ฐ ์๋ ๋ชจ๋ธ์ ๋์ผํ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๋๋ค.
- ์ด๋ง์ ์ ๋ฆฌํ๋ฉด, Multi-Head Attention์ Model์ ํฌ๊ธฐ๋ฅผ ๋๋ฆฌ์ง ์๊ณ ๋์ผํ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง์ง๋ง, ๊ณ์ฐํ Query(์ฟผ๋ฆฌ), Key(ํค), Value(๋ฐธ๋ฅ) ๊ฐ์ ์ฌ๋ฌ ๋ถ๋ถ์ผ๋ก ๋ถํ ํฉ๋๋ค.
Multi-Head Attention Example Code
์ด ์ฝ๋๋ ์์ ์ฝ๋์ ๋๋ค. ์ฌ์ฉ์์ ๋ง์ถฐ์ ์์ ์ ํด์ผ ํฉ๋๋ค.
- Multi-Head Attention Input = Output
class MultiHeadAttention(tf.keras.layers.Layer):
def __init__(self, **kargs):
super(MultiHeadAttention, self).__init__()
self.num_heads = kargs['num_heads']
self.d_model = kargs['d_model']
assert self.d_model % self.num_heads == 0
self.depth = self.d_model // self.num_heads
self.wq = tf.keras.layers.Dense(kargs['d_model']) # Multi-Head Attetion Input = Output
self.wk = tf.keras.layers.Dense(kargs['d_model'])
self.wv = tf.keras.layers.Dense(kargs['d_model'])
self.dense = tf.keras.layers.Dense(kargs['d_model'])
def split_heads(self, x, batch_size):
"""Split the last dimension into (num_heads, depth).
Transpose the result such that the shape is (batch_size, num_heads, seq_len, depth)
"""
x = tf.reshape(x, (batch_size, -1, self.num_heads, self.depth))
return tf.transpose(x, perm=[0, 2, 1, 3])
def call(self, v, k, q, mask):
batch_size = tf.shape(q)[0]
q = self.wq(q) # (batch_size, seq_len, d_model)
k = self.wk(k) # (batch_size, seq_len, d_model)
v = self.wv(v) # (batch_size, seq_len, d_model)
q = self.split_heads(q, batch_size) # (batch_size, num_heads, seq_len_q, depth)
k = self.split_heads(k, batch_size) # (batch_size, num_heads, seq_len_k, depth)
v = self.split_heads(v, batch_size) # (batch_size, num_heads, seq_len_v, depth)
# scaled_attention.shape == (batch_size, num_heads, seq_len_q, depth)
# attention_weights.shape == (batch_size, num_heads, seq_len_q, seq_len_k)
scaled_attention, attention_weights = scaled_dot_product_attention(
q, k, v, mask)
scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3]) # (batch_size, seq_len_q, num_heads, depth)
concat_attention = tf.reshape(scaled_attention,
(batch_size, -1, self.d_model)) # (batch_size, seq_len_q, d_model)
output = self.dense(concat_attention) # (batch_size, seq_len_q, d_model)
return output, attention_weights
Positional Encoding
Transformer ๋ชจ๋ธ์์์ Positinal encoding์ Sequence ๋ด ๋จ์ด๋ค์ ์๋์ ์ธ ์์น ์ ๋ณด๋ฅผ ์ฃผ์ ํ๋๋ฐ ์ฌ์ฉํฉ๋๋ค.
- ๋ค์ ์ค๋ช ํ๋ฉด, Model์ด ๋จ์ด์ ์์๋ฅผ ์ดํดํ๊ณ , ํจ๊ณผ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ฒ ๋์์ค๋๋ค.
- Transformer Model์์ ์ฃผ๋ก ์ฌ์ฉ๋๋ Positional encoding ๋ฐฉ๋ฒ์ Sin(์ฌ์ธ)ํจ์, Cos(์ฝ์ฌ์ธ)ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ํ๋ฒ ์์์ ๋ณด๋ฉด PE๋ Positional Encoding Matrix์ ์์์
๋๋ค.
- ์ด๋ง์ Input Sequence(์ ๋ ฅ ์ํ์ค)์ ๊ฐ ์์น(๋๋ ๋จ์ด)์ ๋ํ ๊ณ ์ ํ ์์น ์ ๋ณด๋ฅผ ์ ๊ณตํ๋๋ฐ ์ฌ์ฉ๋๋ ํ๋ ฌ(matirx)์ ๊ฐ ์์(element)๋ฅผ ์ง์นญํฉ๋๋ค.
- Transformer ๋ชจ๋ธ์ Sequence์ ์์ ์ ๋ณด๋ฅผ ๋ด์ฌ์ ์ผ๋ก ์ฒ๋ฆฌ๋ฅผ ๋ชปํ๊ธฐ ๋๋ฌธ์, ์์น ์ ๋ณด๋ฅผ ๋ชจ๋ธ์ ์ฃผ์ ํด์ผํฉ๋๋ค.
- pos๋ Sequence์์ ๋จ์ด ๋๋ Token์ ์์น(index)๋ฅผ ์๋ฏธํฉ๋๋ค.
- i๋ Positional Encoding Vector ๋ด์ ์ฐจ์์ Index. ์ฆ, Encoding ์ฐจ์(d_model)๋ด์ Index๋ฅผ ๋ํ๋ ๋๋ค.
- d_model์ ๋ชจ๋ธ์ Embedding Dimension(์๋ฒ ๋ฉ ์ฐจ์), ๋ชจ๋ Input Token(์ ๋ ฅ ํ ํฐ)์ด ๋ณํ๋๋ Vector์ ํฌ๊ธฐ๋ฅผ ์๋ฏธํฉ๋๋ค.
ํ๋ฒ ์์๋ฅผ "I am a Robot"์ผ๋ก ๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
- 'Pos'๋ Input Sequence(์
๋ ฅ ์ํ์ค)์์์ ๊ฐ์ฒด์ ์์น์
๋๋ค.
- ์ ๋ ฅ์ํ์ค์ ๊ธธ์ด๋ 0 <= pos < L / 2 ์ ๋๋ค, L์ Input Sequence(์ ๋ ฅ ์ํ์ค-Token)์ ๊ธธ์ด์ ๋๋ค.
- 'd_model' ์ Output Embedding ๊ณต๊ฐ์ Dimension(์ฐจ์)์ ๋๋ค.
- 'PE(pos, 2i)' ๋ ์์น๋ฅผ Mapping ํ๋ ํจ์์ด๋ฉฐ, Indexingํ Input Sequence์ ์์น๋ฅผ ๋ํ๋ ๋๋ค.
- '10000' ์ Transformer ๋ ผ๋ฌธ์์ Positional Encoding ์์์์ ์ค์ ๋ ์ฌ์ฉ์์ ์ ์ Scaler(์ค์นผ๋ผ)์ ๋๋ค. ๋ค๋ฅธ๊ณณ์์ ์ค๋ช ํ ์์์๋ 10000 ๋์ 'n' ์ด๋ผ๊ณ ์ง์นญํ๊ธฐ๋ ํฉ๋๋ค.
- 'i' ๋ ์ด index์ ๋งค์นญํ๋๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. 0< <= i < d_model / 2 ์ ๋๋ค.
์์ Matrixํ๋ฅผ ๋ณด์๋ฉด ์ง์ ์์น๋ Sin ํจ์์ ํด๋นํ๊ณ , ํ์ ์์น๋ Cos ํจ์์ ํด๋นํ๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
- ํ๋ฒ '10000' ์ '100' ์ผ๋ก ๋ฎ์ถ๊ณ , 'd_model' - Output Embedding ๊ณต๊ฐ์ Dimension(์ฐจ์)์ 4๋ก ํ๊ณ ๊ณ์ฐํด๋ณด๋ฉด ์๋์ ๊ฐ์ Encoding Matrix๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
Positional Encoding์ ์ฃผ๊ธฐ์ ์ธ ํจํด์ ์์ฑํฉ๋๋ค.
- Positional Encoding ์์์์์ ์ฌ์ฉ๋๋ Sin, Cos ํจ์์์ ๋ค์ด๊ฐ๋ ๊ณต์์ ์ฃผ๊ธฐ์ ์ธ ํจํด์ ์์ฑํฉ๋๋ค.
- ์ฃผ๊ธฐ์ ์ธ ๊ฐ์ ์์ฑํ๋ฉด์ ์๋ก ๋ค๋ฅธ ์์น์ ๋ฐํ์ฌ ๊ณ ์ ํ ๊ฐ์ ์์ฑํ๋ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
- Positional Encoding์์ ์๋ก ๋ค๋ฅธ ์์น์ ๋ฐํ์ฌ ์์ฑํ ๊ณ ์ ํ ๊ฐ์ ๋จ์ด์ ์๋์ ์ธ ์์น๋ฅผ Embedding ๊ณต๊ฐ์ ๋ฐ์ํ์ฌ Transformer ๋ชจ๋ธ์ด ๋ฌธ์ฅ์ ์์๋ฅผ ์ธ์ํ๋๋ก ๋์์ค๋๋ค.
Scaling
- Scaling์ Positional Encoding์์์ ์์น 'pos' ์ ๋ํ Scaling ์ญํ ์ ํ๋ฉฐ, ๋ค์ํ ์์น์ ๊ฐ๋ค์ ์์ฑํ๋ ์ญํ ์ ํฉ๋๋ค.
'Pos'๋ Input Sequence(์ ๋ ฅ ์ํ์ค)์์์ ๊ฐ์ฒด์ ์์น์ ๋๋ค.
์ ๋ ฅ์ํ์ค์ ๊ธธ์ด๋ 0 <= pos < L / 2 ์ ๋๋ค, L์ Input Sequence(์ ๋ ฅ ์ํ์ค-Token)์ ๊ธธ์ด์ ๋๋ค.
ํ์/์ง์ Dimension ๊ตฌ๋ถ
- ์์์ Positional Encoding Matrix ํ๋ฅผ ๋ณด์๋ฉด ์ผ์ชฝ์์ 2๋ฒ์งธ ํ๋ฅผ ๋ณด๋ฉด "0, 1, 2, 3"์ด๋ผ๊ณ ์์ต๋๋ค.
- ์ด๊ฑด ์ฐจ์์ ๋ํ๋ด๋ ์ซ์์ธ๋ฐ, ์ฌ๊ธฐ์ ํ์ ์ซ์๋ Cos(์ฝ์ฌ์ธ)ํจ์๊ฐ ํ์ Dimension(์ฐจ์), ์ง์ ์ซ์๋ SIn(์ฌ์ธ)ํจ์๋ ์ง์ Dimension(์ฐจ์)์ ์ฒ๋ฆฌํ์ฌ Dimension(์ฐจ์)๊ฐ ๋ค์์ฑ์ ํ๋ณดํฉ๋๋ค.
- ์ด๋ ๊ฒ ํ๋ฉด ๊ฐ Dimension(์ฐจ์)์ด ์๋ก ๋ค๋ฅธ ์ ๋ณด๋ฅผ ๋ด๋นํ๊ฒ ๋๋ฉฐ, Model์ด ๋จ์ด์ ์๋์ ์ธ ์์น๋ฅผ ํจ๊ณผ์ ์ผ๋ก ํ์ตํ ์ ์์ต๋๋ค.
Positional Encoding Example Code
์ด ์ฝ๋๋ ์์ ์ฝ๋์ ๋๋ค. ์ฌ์ฉ์์ ๋ง์ถฐ์ ์์ ์ ํด์ผ ํฉ๋๋ค.
- Sin, Cos ํจ์ ๋ง๋ค๊ณ ๊ฐ๋ค์ ๊ฒน์น์ง ์๊ฒ ํด์ผ ํฉ๋๋ค.
- "Embedding"์ ์ ๋ณด๋ฅผ ํจ๊ณผ์ ์ผ๋ก ํํํ๊ณ ์ฒ๋ฆฌํ ์ ์๋๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฐจ์ ๊ณต๊ฐ์ ๋งคํํ๋ ๊ธฐ์ - "Mapping"์ ์ผ๋ฐ์ ์ผ๋ก ํ ์ธํธ์ ๊ฐ์์ ๋ค๋ฅธ ์ธํธ์ ๊ฐ์ผ๋ก ๋์์ํค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
- embedding dimension ์ด ๋ฌ๋ผ์ ธ๋ positional encoding์ผ๋ก ๊ณ ์ ๋ ๊ฐ์ผ๋ก ๋ค์ด์ต๋๋ค.
- ์ ๊ฒฝ๋ง ๋ชจ๋ธ์์ ์ ๋ ฅ ๋ฐ์ดํฐ๋ฅผ ์ ์ฐจ์์ ๊ณต๊ฐ์ผ๋ก ๋ณํํฅ๋๋ค.
def get_angles(pos, i, d_model):
angle_rates = 1 / np.power(10000, (2 * i//2) / np.float32(d_model))
return pos * angle_rates
def positional_encoding(position, d_model):
angle_rads = get_angles(np.arange(position)[:, np.newaxis],
np.arange(d_model)[np.newaxis, :],
d_model)
# apply sin to even indices in the array; 2i
angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
# apply cos to odd indices in the array; 2i+1
angle_rads[:, 1::2] = np.cos(angle_rads[:, 1::2])
pos_encoding = angle_rads[np.newaxis, ...]
return tf.cast(pos_encoding, dtype=tf.float32)
์ด๋ ๊ฒ Embedding Layer๋ก ์ฌ์ฉํฉ๋๋ค.
pos_encoding = positional_encoding(50, 512) #512 - embedding dimension์ dimension
print (pos_encoding.shape)
plt.pcolormesh(pos_encoding[0], cmap='RdBu')
plt.xlabel('Depth')
plt.xlim((0, 512))
plt.ylabel('Position')
plt.colorbar()
plt.show()
- Result: (1, 50, 512)
Transformer: Model Architecture
Transformer ๋ชจ๋ธ์ ์ ๋ฐ์ ์ธ ๊ตฌ์กฐ๋ฅผ ํ๋ฒ ๋ณด๊ฒ ์ต๋๋ค.
- ํฌ๊ฒ๋ Encoder, Decoder ๋ถ๋ถ์ผ๋ก ๊ตฌ์ฑ์ด ๋์ด ์์ผ๋ฉฐ, ์ธ๋ถ์ ์ผ๋ก๋ Feed-Forward Block, Residual Connection & Layer Normalization, Positional Encoding, Multi-Head Attention์ผ๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
- ์์์ ์์ธํ ์ค๋ช ์ ํ์ผ๋๊น ์ฌ๊ธฐ์๋ ๋๋ต์ ์ธ ์ค๋ช ๋ง ํ๊ฒ ์ต๋๋ค.
Feed-forward Block
- ๊ฐ Layer์๋ Feed-Forward Network Block์ด ์์ต๋๋ค. 2๊ฐ์ Linear Layer(์ ํ ๋ ์ด์ด)์ฌ์ด์๋ ReLU๊ฐ ์์ต๋๋ค.
- Attention ๋งค์ปค๋์ฆ์ ํตํด์ ๋ค๋ฅธ Token์ ์ดํด๋ณธ ํ ๋ชจ๋ธ์ Feed-Forward Block์ ์ฌ์ฉํ์ฌ ์๋ก์ด ์ ๋ณด๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
Residual Connection (์์ฌ ์ฐ๊ฒฐ)
- Residual Connection์ ๋งค์ฐ ๊ฐ๋จํ๋ฉด์ ๋งค์ฐ ์ ์ฉํฉ๋๋ค.
- Block์ Input(์ ๋ ฅ)์ Output(์ถ๋ ฅ)์ ์ถ๊ฐ ํ๋ ๊ตฌ์กฐ์ ๋๋ค.
- Network๋ฅผ ํตํด์ Gradient Flow(ํ๋ฆ)์ ์ํํ๊ณ ๋ง์ Layer(๋ ์ด์ด)๋ฅผ ์์ ์ ์์ต๋๋ค.
- Transformer์์๋ ๊ฐ Attention ๋ฐ Feed-Forward Block ํ์ Residual Connection(์์ฌ ์ฐ๊ฒฐ)์ด ์ฌ์ฉ๋ฉ๋๋ค.
Layer Normalization (๋ ์ด์ด ์ ๊ทํ)
- "Add & Norm" Layer ์ "Norm" ๋ถ๋ถ์ Layer Normalization(๋ ์ด์ด ์ ๊ทํ)๋ฅผ ๋ํ๋ ๋๋ค.
- ๊ฐ ์์ ์ Vector ํํ์ ๋ ๋ฆฝ์ ์ผ๋ก ์ ๊ทํ ํฉ๋๋ค.
- ์ด๋ ๋ค์ Layer๋ก "Flow"๋ฅผ ์ ์ดํ๊ธฐ ์ํด ์ํ๋ฉ๋๋ค.
- Layer Normalization(๋ ์ด์ด ์ ๊ทํ)๋ ์์ ์ฑ์ ํฅ์์ํค๊ณ ํ์ง๊น์ง ํฅ์์ํต๋๋ค.
- Transformer์์๋ ๊ฐ Token์ Vector ํํ์ Normalization(์ ๊ทํ)ํฉ๋๋ค.
- ๋ํ Layer Norm์๋ ํ๋ จ ๊ฐ๋ฅํ ๋งค๊ฐ๋ณ์์ธ 'scale', 'bias' ๊ทธ๋ฆฌ๊ณ Normalization(์ ๊ทํ)ํ์ Layer์ ์ถ๋ ฅ(or Next Layer์ Input)์ ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ๋๋ฐ ์ด์ฉํฉ๋๋ค.
Positional Encoding
- Transformer Model์ Recurrence(๋ฐ๋ณต) & Convolution์ด ํฌํจ๋์ด ์์ง ์์ต๋๋ค
- ๊ทธ๋ฌ๋ฉด Input(์ ๋ ฅ)์ผ๋ก ๋ค์ด์ค๋ Token์ ์์๋ฅผ ์ ์ ์์ต๋๋ค.
- ๋ฐ๋ผ์ Model์ Token์ Position(์์น)๋ฅผ ์๋ ค์ค์ผ ํฉ๋๋ค.
- ์ด๋ฅผ ์ํด์๋ ๋๊ฐ์ง์ Embedding ์ธํธ๊ฐ ์๋๋ฐ, Token, Position์ด ์์ต๋๋ค.
- ๊ทธ๋ฆฌ๊ณ Token์ ์ ๋ ฅ ํํ ํํ๋ Token + Position(์์น)์ Embedding์ ํฉ์ ๋๋ค.
Transformer Model Example Code
์ด ์ฝ๋๋ ์์ ์ฝ๋์ ๋๋ค. ์ฌ์ฉ์์ ๋ง์ถฐ์ ์์ ์ ํด์ผ ํฉ๋๋ค.
Parameter
char2idx = prepro_configs['char2idx']
end_index = prepro_configs['end_symbol']
model_name = 'transformer'
vocab_size = prepro_configs['vocab_size']
BATCH_SIZE = 2
MAX_SEQUENCE = 25
EPOCHS = 30
VALID_SPLIT = 0.1
kargs = {'model_name': model_name,
'num_layers': 2,
'd_model': 512,
'num_heads': 8,
'dff': 2048,
'input_vocab_size': vocab_size,
'target_vocab_size': vocab_size,
'maximum_position_encoding': MAX_SEQUENCE,
'end_token_idx': char2idx[end_index],
'rate': 0.1
}
Transformer Model Code
class Transformer(tf.keras.Model):
def __init__(self, **kargs):
super(Transformer, self).__init__(name=kargs['model_name'])
self.end_token_idx = kargs['end_token_idx']
self.encoder = Encoder(**kargs)
self.decoder = Decoder(**kargs)
self.final_layer = tf.keras.layers.Dense(kargs['target_vocab_size'])
def call(self, x):
inp, tar = x
enc_padding_mask, look_ahead_mask, dec_padding_mask = create_masks(inp, tar)
enc_output, attn = self.encoder(inp, enc_padding_mask) # (batch_size, inp_seq_len, d_model)
# dec_output.shape == (batch_size, tar_seq_len, d_model)
dec_output, attn = self.decoder(
tar, enc_output, look_ahead_mask, dec_padding_mask)
final_output = self.final_layer(dec_output) # (batch_size, tar_seq_len, target_vocab_size)
return final_output, attn
def inference(self, x):
inp = x
tar = tf.expand_dims([STD_INDEX], 0)
enc_padding_mask, look_ahead_mask, dec_padding_mask = create_masks(inp, tar)
enc_output = self.encoder(inp, enc_padding_mask)
predict_tokens = list()
for t in range(0, MAX_SEQUENCE):
dec_output, _ = self.decoder(tar, enc_output, look_ahead_mask, dec_padding_mask)
final_output = self.final_layer(dec_output)
outputs = tf.argmax(final_output, -1).numpy()
pred_token = outputs[0][-1]
if pred_token == self.end_token_idx:
break
predict_tokens.append(pred_token)
tar = tf.expand_dims([STD_INDEX] + predict_tokens, 0)
_, look_ahead_mask, dec_padding_mask = create_masks(inp, tar)
return predict_tokens
'๐ NLP (์์ฐ์ด์ฒ๋ฆฌ) > ๐ Natural Language Processing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[NLP] ํต๊ณ ๊ธฐ๋ฐ ๊ธฐ๋ฒ ๊ฐ์ ํ๊ธฐ (0) | 2024.05.20 |
---|---|
[NLP] Thesaurus(์์๋ฌ์ค), Co-occurence Matrix(๋์๋ฐ์ ํ๋ ฌ) (0) | 2024.05.18 |
[NLP] ํฉ์ฑ๊ณฑ, ์ํ์ ๊ฒฝ๋ง, Encoder, Decoder์์ ์ํํ๋ Self-Attention (0) | 2024.03.01 |
[NLP] Attention - ์ดํ ์ (0) | 2024.02.17 |
[NLP] Word Embedding - ์๋ ์๋ฒ ๋ฉ (0) | 2024.02.12 |