File size: 8,367 Bytes
6140a7a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
import numpy as np
# --- 0. ๊ธฐ๋ณธ ์ค์ (Settings) ---
batch_size = 4 # ๋ฐฐ์น ํฌ๊ธฐ B
d_model = 512 # ๋ชจ๋ธ ์ฐจ์ D
d_k = 64 # ํค๋ ์ฐจ์ (d_model / num_heads)
d_ff = 2048 # FFN ๋ด๋ถ ์ฐจ์
vocab_size = 10000 # ์ดํ ํฌ๊ธฐ V
enc_seq_len = 10 # ์ธ์ฝ๋ ์ํ์ค ๊ธธ์ด S_enc
num_heads = 8
# ์์ ์
๋ ฅ ๋ฐ์ดํฐ: [B, S_enc, D] ํํ
input_data = np.random.randn(batch_size, enc_seq_len, d_model) * 0.1
# --- 1. ํฌํผ ํจ์ ๋ฐ ๊ฐ์ค์น ์ด๊ธฐํ ---
def init_weights(shape):
"""He/Xavier ์ด๊ธฐํ์ ๊ฐ๋ตํ ๋ฒ์ """
if len(shape) == 1:
return np.zeros(shape)
# np.sqrt(2.0 / shape[0]) -> np.sqrt(1.0 / shape[0]) (Xavier)
return np.random.randn(*shape) * np.sqrt(1.0 / shape[0])
# --- 2. ํต์ฌ ๋ ์ด์ด ๊ตฌํ ---
def layer_normalization(x, gamma, beta, epsilon=1e-5):
"""Layer Normalization (๊ณ์ธต ์ ๊ทํ)"""
# x ํํ: [B, S, D]
mean = np.mean(x, axis=-1, keepdims=True)
variance = np.mean((x - mean) ** 2, axis=-1, keepdims=True)
x_normalized = (x - mean) / np.sqrt(variance + epsilon)
output = gamma * x_normalized + beta
return output
def scaled_dot_product_attention(Q, K, V, mask=None):
"""Scaled Dot-Product Attention (๋ฐฐ์น ์ฒ๋ฆฌ ์ง์)"""
# Q: [B, H, S_q, d_k], K: [B, H, S_k, d_k], V: [B, H, S_k, d_k]
scores = np.matmul(Q, K.transpose(0, 1, 3, 2)) # [B, H, S_q, S_k]
scores = scores / np.sqrt(d_k)
if mask is not None:
scores = scores + mask
exp_scores = np.exp(scores - np.max(scores, axis=-1, keepdims=True))
attention_weights = exp_scores / np.sum(exp_scores, axis=-1, keepdims=True)
output = np.matmul(attention_weights, V) # [B, H, S_q, d_k]
return output, attention_weights
def multi_head_attention(Q, K, V, W_Q, W_K, W_V, W_O, mask=None):
"""
Multi-Head Attention (์ค๋ฅ ์์ : ๋์ ์ํ์ค ๊ธธ์ด ์ฒ๋ฆฌ)
Q: [B, S_q, D], K: [B, S_k, D], V: [B, S_k, D]
"""
# ๐๐๐ ํต์ฌ ์์ ๋ถ๋ถ: Q, K, V์์ ๋์ ์ผ๋ก Shape ์ฝ๊ธฐ ๐๐๐
B_q, S_q, D_q = Q.shape
B_k, S_k, D_k = K.shape
B_v, S_v, D_v = V.shape
# (B_q, B_k, B_v๋ ๋ชจ๋ batch_size๋ก ๋์ผํด์ผ ํจ)
# (S_k์ S_v๋ ๋์ผํด์ผ ํจ)
# 1. ์ ํ ๋ณํ (Projection)
Q_proj = np.matmul(Q, W_Q) # [B_q, S_q, D]
K_proj = np.matmul(K, W_K) # [B_k, S_k, D]
V_proj = np.matmul(V, W_V) # [B_v, S_v, D]
# 2. Multi-Head ๋ถํ ๋ฐ ์ฐจ์ ๋ณ๊ฒฝ
# Q: [B_q, num_heads, S_q, d_k]
Q_multi = Q_proj.reshape(B_q, S_q, num_heads, d_k).transpose(0, 2, 1, 3)
# K: [B_k, num_heads, S_k, d_k]
K_multi = K_proj.reshape(B_k, S_k, num_heads, d_k).transpose(0, 2, 1, 3)
# V: [B_v, num_heads, S_v, d_k]
V_multi = V_proj.reshape(B_v, S_v, num_heads, d_k).transpose(0, 2, 1, 3)
# 3. ์ดํ
์
๊ณ์ฐ
attended_output, _ = scaled_dot_product_attention(Q_multi, K_multi, V_multi, mask)
# 4. ๊ฒฐ๊ณผ ๊ฒฐํฉ (Concatenate): [B_q, S_q, D]
attended_output = attended_output.transpose(0, 2, 1, 3).reshape(B_q, S_q, d_model)
# 5. ์ต์ข
์ถ๋ ฅ ์ ํ ๋ณํ
output = np.matmul(attended_output, W_O)
return output
def feed_forward_network(x, W1, b1, W2, b2):
"""Feed-Forward Network (FFN)"""
hidden = np.matmul(x, W1) + b1
hidden = np.maximum(0, hidden) # ReLU
output = np.matmul(hidden, W2) + b2
return output
# --- 3. ๊ฐ์ค์น ์ค์ (ํ๋์ ์ธต์ ์ํ ๋ชจ๋ ๊ฐ์ค์น) ---
# Encoder ๊ฐ์ค์น
W_Q_enc, W_K_enc, W_V_enc, W_O_enc = init_weights((d_model, d_model)), init_weights((d_model, d_model)), init_weights((d_model, d_model)), init_weights((d_model, d_model))
W1_enc, W2_enc = init_weights((d_model, d_ff)), init_weights((d_ff, d_model))
b1_enc, b2_enc = init_weights((1, d_ff)), init_weights((1, d_model))
gamma_enc1, beta_enc1 = np.ones((1, 1, d_model)), np.zeros((1, 1, d_model))
gamma_enc2, beta_enc2 = np.ones((1, 1, d_model)), np.zeros((1, 1, d_model))
# Decoder ๊ฐ์ค์น
W_Q_dec_self, W_K_dec_self, W_V_dec_self, W_O_dec_self = init_weights((d_model, d_model)), init_weights((d_model, d_model)), init_weights((d_model, d_model)), init_weights((d_model, d_model))
W_Q_dec_cross, W_K_dec_cross, W_V_dec_cross, W_O_dec_cross = init_weights((d_model, d_model)), init_weights((d_model, d_model)), init_weights((d_model, d_model)), init_weights((d_model, d_model))
W1_dec, W2_dec = init_weights((d_model, d_ff)), init_weights((d_ff, d_model))
b1_dec, b2_dec = init_weights((1, d_ff)), init_weights((1, d_model))
gamma_dec1, beta_dec1 = np.ones((1, 1, d_model)), np.zeros((1, 1, d_model))
gamma_dec2, beta_dec2 = np.ones((1, 1, d_model)), np.zeros((1, 1, d_model))
gamma_dec3, beta_dec3 = np.ones((1, 1, d_model)), np.zeros((1, 1, d_model))
# --- 4. ์ธ์ฝ๋ ๋ธ๋ก (Add & Norm ์ ์ฉ) ---
def encoder_block(x):
# x ํํ: [B, S_enc, D]
# Sub-layer 1: Multi-Head Self-Attention
attn_output = multi_head_attention(x, x, x, W_Q_enc, W_K_enc, W_V_enc, W_O_enc)
# 1. Add & Norm
x_1 = layer_normalization(attn_output + x, gamma_enc1, beta_enc1)
# Sub-layer 2: Feed-Forward Network
ffn_output = feed_forward_network(x_1, W1_enc, b1_enc, W2_enc, b2_enc)
# 2. Add & Norm
output = layer_normalization(ffn_output + x_1, gamma_enc2, beta_enc2)
return output
# --- 5. ๋์ฝ๋ ๋ธ๋ก (Add & Norm ์ ์ฉ) ---
def create_look_ahead_mask(size):
"""Look-ahead Mask ์์ฑ (๋ฏธ๋ ๋จ์ด ๋ง์คํน)"""
mask = np.triu(np.ones((size, size)), k=1)
return (mask * -1e9)[np.newaxis, np.newaxis, :, :] # [1, 1, S, S]
def decoder_block(x, enc_output, look_ahead_mask):
# x ํํ: [B, S_target, D], enc_output ํํ: [B, S_source, D]
# Sub-layer 1: Masked Multi-Head Self-Attention
self_attn_output = multi_head_attention(
x, x, x, W_Q_dec_self, W_K_dec_self, W_V_dec_self, W_O_dec_self, mask=look_ahead_mask
)
# 1. Add & Norm
x_1 = layer_normalization(self_attn_output + x, gamma_dec1, beta_dec1)
# Sub-layer 2: Multi-Head Encoder-Decoder Attention (Cross-Attention)
# Q: ๋์ฝ๋ ์ถ๋ ฅ(x_1), K, V: ์ธ์ฝ๋ ์ถ๋ ฅ(enc_output)
cross_attn_output = multi_head_attention(
x_1, enc_output, enc_output, W_Q_dec_cross, W_K_dec_cross, W_V_dec_cross, W_O_dec_cross, mask=None
)
# 2. Add & Norm (์์ฐจ ์ฐ๊ฒฐ์ x_1๊ณผ ์ฐ๊ฒฐ)
x_2 = layer_normalization(cross_attn_output + x_1, gamma_dec2, beta_dec2)
# Sub-layer 3: FFN
ffn_output = feed_forward_network(x_2, W1_dec, b1_dec, W2_dec, b2_dec)
# 3. Add & Norm
output = layer_normalization(ffn_output + x_2, gamma_dec3, beta_dec3)
return output
# --- 6. ์ต์ข
Output (Linear + Softmax) ---
W_linear = init_weights((d_model, vocab_size))
b_linear = init_weights((1, vocab_size))
def final_output_layer(x):
# x: [B, S, D]
logits = np.matmul(x, W_linear) + b_linear # [B, S, V]
exp_logits = np.exp(logits - np.max(logits, axis=-1, keepdims=True))
probabilities = exp_logits / np.sum(exp_logits, axis=-1, keepdims=True)
return probabilities
# --- 7. ์ ์ฒด ํธ๋์คํฌ๋จธ ํ๋ฆ ์๋ฎฌ๋ ์ด์
---
print("--- Add & Norm ์ ์ฉ๋ ํธ๋์คํฌ๋จธ ์๋ฎฌ๋ ์ด์
์์ ---")
# 1. ์ธ์ฝ๋ ์คํ
# input_data: (4, 10, 512)
enc_output_final = encoder_block(input_data)
print(f"์ธ์ฝ๋ ์ต์ข
์ถ๋ ฅ ํํ (K, V ์์ค): {enc_output_final.shape}")
# 2. ๋์ฝ๋ ์
๋ ฅ ์ค๋น
dec_seq_len = 5 # ๋์ฝ๋ ์ํ์ค ๊ธธ์ด
decoder_input_data = np.random.randn(batch_size, dec_seq_len, d_model) * 0.1
look_ahead_mask = create_look_ahead_mask(dec_seq_len) # [1, 1, 5, 5]
# 3. ๋์ฝ๋ ์คํ
# decoder_input_data (Q): (4, 5, 512)
# enc_output_final (K, V): (4, 10, 512)
# Cross-Attention์์ Q(S=5)์ K/V(S=10)์ ๊ธธ์ด๊ฐ ๋ฌ๋ผ๋ ์ ์ ์๋
dec_output_final = decoder_block(decoder_input_data, enc_output_final, look_ahead_mask)
print(f"๋์ฝ๋ ์ต์ข
์ถ๋ ฅ ํํ: {dec_output_final.shape}")
# 4. ์ต์ข
์ถ๋ ฅ
probabilities = final_output_layer(dec_output_final)
print(f"์ต์ข
ํ๋ฅ ๋ถํฌ ํํ (B x S_target x V): {probabilities.shape}")
print("\n**์๋ฃ**") |