PostIT

[AI/Deep Learing] Deep Learning 준비하기 - MNIST Load Data - 5-1 본문

Deep Learning

[AI/Deep Learing] Deep Learning 준비하기 - MNIST Load Data - 5-1

shun10114 2017. 6. 23. 15:58

# [AI/Deep Learing] Deep Learning 준비하기 - MNIST Load Data - 5-1

import sys, os

sys.path.append(os.pardir) 


import numpy as np

import pickle

from dataset.mnist import load_mnist

from common.functions import sigmoid, softmax



def get_data():

    (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)

    return x_test, t_test



def init_network():

    with open("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 = np.dot(x, W1) + b1

    z1 = sigmoid(a1)

    a2 = np.dot(z1, W2) + b2

    z2 = sigmoid(a2)

    a3 = np.dot(z2, W3) + b3

    y = softmax(a3)


    return y



x, t = get_data()

network = init_network()

accuracy_cnt = 0

for i in range(len(x)):

    y = predict(network, x[i])

    p= np.argmax(y) 

#  확률이 가장 높은 인덱스 값을 가져옴.


    if p == t[i]:

        accuracy_cnt += 1


print("Accuracy:" + str(float(accuracy_cnt) / len(x)))

    Accuracy:0.9352



x, _ = get_data()

network = init_network()

W1, W2, W3 = network['W1'], network['W2'], network['W3']

x.shape

    (10000, 784)



x[0].shape

    (784,)



W1.shape

    (784, 50)



W2.shape

    (50, 100)



W3.shape

    (100, 10)



Comments