File size: 11,884 Bytes
d731ef0 |
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
import numpy as np
# λ°μ΄ν° λ‘λ©μ μν΄μλ§ tensorflow.keras.datasetsλ₯Ό μ¬μ©ν©λλ€.
# μ€μ λͺ¨λΈ μν€ν
μ²μ νμ΅ λ‘μ§μλ μ ν μ¬μ©λμ§ μμ΅λλ€.
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
# =================================================================
# --- 1. λ°μ΄ν° μ€λΉ ---
# =================================================================
def load_mnist_data():
"""MNIST λ°μ΄ν°μ
μ λΆλ¬μ€κ³ μ μ²λ¦¬ν©λλ€."""
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# ν½μ
κ°μ 0κ³Ό 1 μ¬μ΄λ‘ μ κ·ν
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# μ±λ μ°¨μ μΆκ° (N, 28, 28) -> (N, 28, 28, 1)
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
# λ μ΄λΈμ μ-ν« μΈμ½λ©
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
return x_train, y_train, x_test, y_test
# =================================================================
# --- 2. κ³μΈ΅(Layer) ꡬν ---
# =================================================================
class ConvLayer:
"""ν©μ±κ³± κ³μΈ΅ (λ€μ€ μ±λ μ
λ ₯ μ§μμΌλ‘ μμ λ¨)"""
def __init__(self, num_filters, filter_size, input_channels):
self.num_filters = num_filters
self.filter_size = filter_size
self.input_channels = input_channels
# Xavier/Glorot μ΄κΈ°ν (λ€μ€ μ±λ κ³ λ €)
self.filters = np.random.randn(filter_size, filter_size, input_channels, num_filters) * np.sqrt(2. / (filter_size * filter_size * input_channels))
self.biases = np.zeros(self.num_filters)
self.last_input = None
def forward(self, input_image):
self.last_input = input_image
batch_size, input_height, input_width, _ = input_image.shape
output_height = input_height - self.filter_size + 1
output_width = input_width - self.filter_size + 1
output = np.zeros((batch_size, output_height, output_width, self.num_filters))
for b in range(batch_size):
for i in range(output_height):
for j in range(output_width):
region = input_image[b, i:(i + self.filter_size), j:(j + self.filter_size), :]
for f in range(self.num_filters):
output[b, i, j, f] = np.sum(region * self.filters[:, :, :, f]) + self.biases[f]
return output
def backward(self, d_loss_d_output, learning_rate):
"""μμ ν λ©μλ (μ
λ ₯ κ·ΈλλμΈνΈ κ³μ° μΆκ°)"""
batch_size, _, _, _ = self.last_input.shape
d_loss_d_filters = np.zeros_like(self.filters)
d_loss_d_input = np.zeros_like(self.last_input)
# νν°μ μ
λ ₯μ λν κ·ΈλλμΈνΈ κ³μ°
for b in range(batch_size):
for i in range(d_loss_d_output.shape[1]): # output height
for j in range(d_loss_d_output.shape[2]): # output width
region = self.last_input[b, i:(i + self.filter_size), j:(j + self.filter_size), :]
for f in range(self.num_filters):
# νν° κ·ΈλλμΈνΈ λμ
d_loss_d_filters[:, :, :, f] += d_loss_d_output[b, i, j, f] * region
# μ
λ ₯ κ·ΈλλμΈνΈ λμ (Chain Rule)
d_loss_d_input[b, i:i+self.filter_size, j:j+self.filter_size, :] += d_loss_d_output[b, i, j, f] * self.filters[:, :, :, f]
# νΈν₯μ κ·ΈλλμΈνΈ
d_loss_d_biases = np.sum(d_loss_d_output, axis=(0, 1, 2))
# κ°μ€μΉ λ° νΈν₯ μ
λ°μ΄νΈ
self.filters -= learning_rate * d_loss_d_filters / batch_size
self.biases -= learning_rate * d_loss_d_biases / batch_size
# μ΄μ κ³μΈ΅μΌλ‘ μ λ¬ν κ·ΈλλμΈνΈ λ°ν
return d_loss_d_input
class ReLULayer:
"""ReLU νμ±ν ν¨μ"""
def __init__(self):
self.last_input = None
def forward(self, input_data):
self.last_input = input_data
return np.maximum(0, input_data)
def backward(self, d_loss_d_output):
d_relu = (self.last_input > 0).astype(int)
return d_loss_d_output * d_relu
class MaxPoolingLayer:
"""λ§₯μ€ νλ§ κ³μΈ΅"""
def __init__(self, pool_size):
self.pool_size = pool_size
self.last_input = None
def forward(self, input_image):
self.last_input = input_image
batch_size, input_height, input_width, num_filters = input_image.shape
output_height = input_height // self.pool_size
output_width = input_width // self.pool_size
output = np.zeros((batch_size, output_height, output_width, num_filters))
for b in range(batch_size):
for i in range(output_height):
for j in range(output_width):
for f in range(num_filters):
region = input_image[b, (i*self.pool_size):(i*self.pool_size + self.pool_size),
(j*self.pool_size):(j*self.pool_size + self.pool_size), f]
output[b, i, j, f] = np.max(region)
return output
def backward(self, d_loss_d_output):
d_loss_d_input = np.zeros_like(self.last_input)
for b in range(d_loss_d_output.shape[0]):
for i in range(d_loss_d_output.shape[1]):
for j in range(d_loss_d_output.shape[2]):
for f in range(d_loss_d_output.shape[3]):
region = self.last_input[b, (i*self.pool_size):(i*self.pool_size + self.pool_size),
(j*self.pool_size):(j*self.pool_size + self.pool_size), f]
max_val = np.max(region)
mask = (region == max_val)
d_loss_d_input[b, (i*self.pool_size):(i*self.pool_size + self.pool_size),
(j*self.pool_size):(j*self.pool_size + self.pool_size), f] += \
mask * d_loss_d_output[b, i, j, f]
return d_loss_d_input
class FlattenLayer:
"""ννν κ³μΈ΅"""
def __init__(self):
self.last_input_shape = None
def forward(self, input_data):
self.last_input_shape = input_data.shape
batch_size = input_data.shape[0]
return input_data.reshape(batch_size, -1)
def backward(self, d_loss_d_output):
return d_loss_d_output.reshape(self.last_input_shape)
class DenseLayer:
"""μμ μ°κ²° κ³μΈ΅"""
def __init__(self, input_size, output_size):
self.weights = np.random.randn(input_size, output_size) * np.sqrt(2. / input_size)
self.biases = np.zeros(output_size)
self.last_input = None
self.last_input_shape = None
def forward(self, input_data):
self.last_input_shape = input_data.shape
self.last_input = input_data
return np.dot(input_data, self.weights) + self.biases
def backward(self, d_loss_d_output, learning_rate):
batch_size = self.last_input.shape[0]
d_loss_d_input = np.dot(d_loss_d_output, self.weights.T)
d_loss_d_weights = np.dot(self.last_input.T, d_loss_d_output)
d_loss_d_biases = np.sum(d_loss_d_output, axis=0)
self.weights -= learning_rate * d_loss_d_weights / batch_size
self.biases -= learning_rate * d_loss_d_biases / batch_size
return d_loss_d_input
# =================================================================
# --- 3. Softmax λ° μμ€ ν¨μ ---
# =================================================================
def softmax(logits):
exps = np.exp(logits - np.max(logits, axis=1, keepdims=True))
return exps / np.sum(exps, axis=1, keepdims=True)
def cross_entropy_loss(y_pred, y_true):
samples = y_true.shape[0]
epsilon = 1e-12
y_pred_clipped = np.clip(y_pred, epsilon, 1. - epsilon)
loss = -np.sum(y_true * np.log(y_pred_clipped)) / samples
return loss
# =================================================================
# --- 4. CNN λͺ¨λΈ ν΄λμ€ ---
# =================================================================
class SimpleCNN:
def __init__(self):
# μ
λ ₯: 28x28x1
self.conv1 = ConvLayer(num_filters=8, filter_size=3, input_channels=1) # -> 26x26x8
self.relu1 = ReLULayer()
self.pool1 = MaxPoolingLayer(pool_size=2) # -> 13x13x8
self.conv2 = ConvLayer(num_filters=16, filter_size=3, input_channels=8)# -> 11x11x16
self.relu2 = ReLULayer()
self.pool2 = MaxPoolingLayer(pool_size=2) # -> 5x5x16
self.flatten = FlattenLayer() # -> 400
self.dense = DenseLayer(5 * 5 * 16, 10) # -> 10
def forward(self, image):
out = self.conv1.forward(image)
out = self.relu1.forward(out)
out = self.pool1.forward(out)
out = self.conv2.forward(out)
out = self.relu2.forward(out)
out = self.pool2.forward(out)
out = self.flatten.forward(out)
out = self.dense.forward(out)
return out
def backward(self, d_loss_d_out, learning_rate):
"""μμ ν λ©μλ (κ·ΈλλμΈνΈ νλ¦ μμ )"""
# μμ νλ μμ νμ μμμΌλ‘ μ§ν
gradient = self.dense.backward(d_loss_d_out, learning_rate)
gradient = self.flatten.backward(gradient)
# 2λ¨κ³ μμ ν
gradient = self.pool2.backward(gradient)
gradient = self.relu2.backward(gradient)
gradient = self.conv2.backward(gradient, learning_rate)
# 1λ¨κ³ μμ ν
gradient = self.pool1.backward(gradient)
gradient = self.relu1.backward(gradient)
self.conv1.backward(gradient, learning_rate)
# =================================================================
# --- 5. νμ΅ λ£¨ν ---
# =================================================================
if __name__ == '__main__':
x_train, y_train, x_test, y_test = load_mnist_data()
x_train_small, y_train_small = x_train[:1000], y_train[:1000]
x_test_small, y_test_small = x_test[:500], y_test[:500]
model = SimpleCNN()
learning_rate = 0.01
epochs = 5
batch_size = 32
print("νμ΅ μμ...")
for epoch in range(epochs):
epoch_loss = 0
for i in range(0, x_train_small.shape[0], batch_size):
x_batch = x_train_small[i:i+batch_size]
y_batch = y_train_small[i:i+batch_size]
logits = model.forward(x_batch)
predictions = softmax(logits)
loss = cross_entropy_loss(predictions, y_batch)
epoch_loss += loss
d_loss_d_out = (predictions - y_batch)
model.backward(d_loss_d_out, learning_rate)
avg_loss = epoch_loss / (len(x_train_small) / batch_size)
print(f"Epoch {epoch + 1}/{epochs}, Loss: {avg_loss:.4f}")
print("\nν
μ€νΈ μμ...")
test_logits = model.forward(x_test_small)
test_predictions = softmax(test_logits)
predicted_labels = np.argmax(test_predictions, axis=1)
true_labels = np.argmax(y_test_small, axis=1)
accuracy = np.mean(predicted_labels == true_labels)
print(f"ν
μ€νΈ μ νλ: {accuracy * 100:.2f}%") |