Replicated

신경망 손글씨 분류 (MNIST), 순전파(forward propagation) 본문

학부/빅데이터마이닝

신경망 손글씨 분류 (MNIST), 순전파(forward propagation)

라구넹 2025. 6. 8. 02:38
import sys
import os
import pickle
import numpy as np
sys.path.append('/content/gdrive/MyDrive/BigDataMining/MNIST')  # 구글 드라이브 디렉터리의 파일을 가져올 수 있도록 설정
from common.functions import sigmoid, softmax
from keras.datasets import mnist

(x_train, t_train), (x_test, t_test) = mnist.load_data()

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)

print(x_train.shape)
print(t_train.shape)
print(x_test.shape)
print(t_test.shape)

훈련 데이터 60000만개, 테스트 데이터 10000개

784 = 28 * 28,  28*28 이미지들임

 

from matplotlib.pyplot import imshow

img = x_train[0]
label = t_train[0]
print(label)  # 5
print(img.shape)  # (784,)
img = img.reshape(28, 28) 
print(img.shape)  # (28, 28)

imshow(img)

그려보면 5

def softmax_2(x):
  c = np.max(x)
  return np.exp(x - c) / np.sum(np.exp(x - c))

소프트맥스 함수 정의

 

# 3.6.2 신경망의 추론 처리

from keras.datasets import mnist

def get_data():
    (x_train, t_train), (x_test, t_test) = mnist.load_data()
    x_train = x_train.reshape(60000, 784)
    x_test = x_test.reshape(10000, 784)
    return x_test, t_test


def init_network():
    with open("/content/gdrive/MyDrive/BigDataMining/MNIST/pretrained/sample_weight.pkl", 'rb') as f:
        network = pickle.load(f)

    return network


def predict(network, x):
    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']

    a1 = x.dot(W1) + b1
    z1 = sigmoid(a1)

    a2 = z1.dot(W2) + b2
    z2 = sigmoid(a2)

    a3 = z2.dot(W3) + b3
    y = softmax_2(a3)

    return y

predict를 보면 3층 신경망으로 구현

이미 구해진 매개변수로 순전파 진행

활성화 함수는 시그모이드, 출력층은 소프트맥스 사용

'학부 > 빅데이터마이닝' 카테고리의 다른 글

오차 역전파법 (Backward Propagation)  (0) 2025.06.08
신경망 학습 / 수치미분  (0) 2025.06.08
신경망  (0) 2025.06.07
퍼셉트론 (Perceptron)  (0) 2025.06.07
나이브 베이지안 분류기  (0) 2025.04.12