파이썬 딥러닝 ai 스쿨 기초/lecture02

lecture02 2교시 인공지능 텐서플로우 실습 필기체 인식기1

junny1997 2021. 3. 22. 15:07

mnist 데이터 확인

import matplotlib.pyplot as plt
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
#validaiton_size = 2000 으로 2000장으로 제한 가능
#one hot encoding [0.0.0.0.1.0.0.0.0.0.]

print('validation.image: ', np.shape(mnist.validation.images))
print('validation.labels: ', np.shape(mnist.validation.labels))
#validation.image:  (5000, 784) 28x28=784이미지 5000장
#validation.labels:  (5000, 10) 0~9 10개 라벨 5000장
print(np.shape(mnist.train.images))
print(np.shape(mnist.train.labels))
print(np.shape(mnist.test.images))
print(np.shape(mnist.test.labels))
print(mnist.train.labels[101])

plt.imshow(
    mnist.train.images[101].reshape(28, 28),
    cmap="Greys",
    interpolation="nearest"
)
plt.show()

validation.image:  (5000, 784)

validation.labels:  (5000, 10)

(55000, 784)
(55000, 10)
(10000, 784)
(10000, 10)
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]

 

 

 

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("../lecture_03/MNIST_data", one_hot=True)
#model 정의 placeholder 로 그릇 준비
X = tf.placeholder(tf.float32, [None, 784]) #이미지 사이즈 28*28
Y = tf.placeholder(tf.float32, [None, 10]) # 0~9 10개

#layer 정의
W1 = tf.Variable(tf.random_normal([784, 300]))
b1 = tf.Variable(tf.random_normal([300]))
L1 = tf.nn.relu(tf.matmul(X, W1) + b1)
# (batch,784) x (784,300) -> (batch,300) matmul 행렬곱

W2 = tf.Variable(tf.random_normal([300, 200]))
b2 = tf.Variable(tf.random_normal([200]))
L2 = tf.nn.relu(tf.matmul(L1, W2) + b2)

W3 = tf.Variable(tf.random_normal([200, 10]))
b3 = tf.Variable(tf.random_normal([10]))
hypothesis = tf.matmul(L2, W3) +b3
# 0~9

#tf.nn.softmax_cross_entropy_with_ligits() error 자동으로 계산 cost
#tf.reduce_mean 행렬 평균
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=hypothesis, labels=Y))

# hypothesis = tf.nn.softmax(hypothesis) 실수값을 softmax로 확률값으로 변환 [0.6784 0.137 ...] -> [0.09 0.01 ...]
# cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1)) 확률값 x 정답 [0 0 1 ...] error 계산
#처럼도 표현 가능

learning_rate = 0.001

#back propagation 기능
# optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
# optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate).minimize(cost)
# optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate, momentum=0.9).minimize(cost)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

#initialize
sess = tf.Session()
sess.run(tf.global_variables_initializer())
#tf.global_variables_initializer 모든 변수 초기화(미실행시 error)

training_epochs = 30
#학습 횟수 30번
batch_size = 100
#100개씩 55000/100

#train my model
max = 0.0
for epoch in range(training_epochs):
    #30회
    total_batch = int(mnist.train.num_examples / batch_size)
    #55000/100 배치수
    for i in range(total_batch):
        avg_cost = 0
        #배치수
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        # next_batch : 지정한 갯수만큼 image와 label을 순차적으로 반환하는 함수
        # mnist.train의 data set에서 batch_size(100)개의 batch를 랜덤하게 가져와서 batch_xs에 이미지를, batch_ys에 Label을 넣음
        feed_dict = {X: batch_xs, Y: batch_ys}
        #X,Y에 랜덤하게 불러온 batch 이미지, 라벨 입력
        c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)
        #계산하고싶은 데이터 cost(error 누적), _(값 무시)optimizer(학습실행위해 호출)
        avg_cost += c /total_batch
        #error cost 누적 평균

    print('Epoch:', '%04d' % (epoch + 1), 'training cost =', '{:.9f}'.format(avg_cost))
    correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))
    #argmax로 가장 높은 실수 값의 인덱스 return 모델학습(hypothesis) 와 Y(정답)
    #tf.equal 로 같은지 확인 equal = true
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    #tf.cast로 True False를 실수값(float) True = 1.0 Flase = 0.0 으로 변환
    #reduce mean으로 평균 , accuracy(정확도)로 표현


    test_accuracy = sess.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels})
    print('Test Accuracy:', test_accuracy)
    if test_accuracy > max:
        max = test_accuracy
print('='*100)
print('Learning Finished!')
print('Test Max Accuracy:', max)

Epoch: 0030 training cost = 0.000054772
Test Accuracy: 0.956
====================================================================================================
Learning Finished!
Test Max Accuracy: 0.956