기존의 mnist 데이터 베이스를 통해 손글씨를 써서 숫자를 인식하는 과정이다.
# Lab 7 Learning rate and Evaluation
import tensorflow as tf
import random
import matplotlib.pyplot as plt
tf.set_random_seed(777) # for reproducibility
from tensorflow.examples.tutorials.mnist import input_data
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
# input data를 읽어오는 과정
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
nb_classes = 10
# MNIST data image of shape 28 * 28 = 784
# 0 - 9 digits recognition = 10 classes
# 28x28픽셀 이므로 784개의 칸이 있다. 그러므로 행은 N개 열은 784개로 정의
# 0~9까지 숫자가 있으므로 Y노드의 행은 N개 열은 10개
X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, nb_classes])
W = tf.Variable(tf.random_normal([784, nb_classes]))
b = tf.Variable(tf.random_normal([nb_classes]))
# Hypothesis (using softmax)
# softmax 함수를 이용한 가설함수
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
#softmax 함수를 이용한 비용함수
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
# Test model
is_correct = tf.equal(tf.arg_max(hypothesis, 1), tf.arg_max(Y, 1))
# Calculate accuracy
#가설함수와 Y노드 값의 arg_max값(one-hot encoding)의 차이를 빼서 평균을 내고 확률을 구함.
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
# parameters
#수많은 mnist 데이터중에 100개씩 끊어서 15번 러닝한다는뜻
training_epochs = 15
batch_size = 100
with tf.Session() as sess:
# Initialize TensorFlow variables
sess.run(tf.global_variables_initializer())
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples / batch_size)
#i = iteration(반복) -> 전체사이즈 / 배치사이즈
#예를들어 전체사이즈가 10000이고 배치사이즈가 100이면 loop는 100번 실행된다.
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
c, _ = sess.run([cost, optimizer], feed_dict={
X: batch_xs, Y: batch_ys})
avg_cost += c / total_batch
#파이썬 문법 %04d = 십진수, 최소4칸확보, 맨왼쪽 0으로 채움, .9f = 소수점이하 9자리까지 표시
print('Epoch:', '%04d' % (epoch + 1),
'cost =', '{:.9f}'.format(avg_cost))
print("Learning finished")
# Test the model using test sets
#X노드에 테스트 이미지를 넣고 Y노드에 라벨을 넣어서 정확도를 판별한다
print("Accuracy: ", accuracy.eval(session=sess, feed_dict={
X: mnist.test.images, Y: mnist.test.labels}))
#mnist 테스트 데이터에서 랜덤으로 이미지를 고른다
#테스트 라벨을 arg_max를 이용하여 나올 수 있는 확률이 가장 큰 숫자를 고른다
#테스트 이미지를 넣은 hypothesis값을 arg_max를 이용하여 나올 수 있는 확률이 가장 큰 숫자를 고른다.
r = random.randint(0, mnist.test.num_examples - 1)
print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))
print("Prediction: ", sess.run(
tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]}))
#테스트 이미지를 28x28 사이즈로 출력한다
plt.imshow(
mnist.test.images[r:r + 1].reshape(28, 28),
cmap='Greys',
interpolation='nearest')
plt.show()
'코딩이것저것' 카테고리의 다른 글
| CNN_Sigmoid & ReLU (0) | 2017.08.05 |
|---|---|
| CNN_Neural Net for XOR (0) | 2017.08.05 |
| CNN_training test dataset, learning rate (0) | 2017.08.05 |
| CNN_Softmax classifier (0) | 2017.08.04 |
| CNN_logistic_regression (0) | 2017.08.02 |