티스토리 뷰
Meet MNIST Dataset¶
In [1]:
from PIL import Image
Image.open('MNIST.png')
Out[1]:
28x28x1 image¶
In [2]:
Image.open('pic.png')
Out[2]:
In [ ]:
# MNIST data image of shape 28 * 28 = 784
X = tf.placeholder(tf.float32, [None, 784])
# 0 - 9 digits recognition = 10 classes
Y = tf.placeholder(tf.float32, [None, nb_classes])
MNIST Dataset¶
In [ ]:
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
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
...
batch_xs, batch_ys = mnist.train.next_batch(100) # 메모리를 많이 차지하기 때문에 x ,y batch 100개씩 읽어서 올림
...
print("Accuracy: ", accuracy.eval(session=sess,
feed_dict={X: mnist.test.images, Y: mnist.test.labels})) #Test로 평가
Reading data and set variables¶
In [40]:
import tensorflow as tf
import matplotlib.pyplot as plt
import random
tf.set_random_seed(773)# for reproducibility
In [41]:
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
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
nb_classes = 10
# MNIST data image of shape 28 * 28 = 784
X = tf.placeholder(tf.float32, [None, 784])
# 0 - 9 digits recognition = 10 classes
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]))
Softmax!¶
In [42]:
# Hypothesis (using softmax)
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) #optimizer
# Test model
is_correct = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
Training epoch/batch¶
한번에 학습시키기 힘들기 때문에 조금씩 잘라서 batch 시킨다.
전체 데이터를 한 번 학습시키는 것을 1epoch라고 한다.
In [31]:
Image.open('epoch.png')
Out[31]:
In [46]:
# parameters
num_epochs = 15
batch_size = 100
num_iterations = int(mnist.train.num_examples / batch_size)
# general한 학습방법
with tf.Session() as sess:
# Initialize TensorFlow variables
sess.run(tf.global_variables_initializer())
# Training cycle
for epoch in range(num_epochs):
avg_cost = 0
for i in range(num_iterations): # 이 루프가 끝나면 1epoch
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
_, cost_val = sess.run([train, cost], feed_dict={X: batch_xs, Y: batch_ys})
avg_cost += cost_val / num_iterations
print("Epoch: {:04d}, Cost: {:.9f}".format(epoch + 1, avg_cost))
print("Learning finished")
# Report results on test dataset 까지 넣어버림
print(
"Accuracy: ",
accuracy.eval(
session=sess, feed_dict={X: mnist.test.images, Y: mnist.test.labels}
),
)
Report results on test dataset¶
In [ ]:
# 오류때문에 앞에 2줄 써줘야 함. 아니면 앞에 있는 코드에 연결해서 쓰던가.
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Test the model using test sets
# accuracy.eval = sses.num
print(
"Accuracy: ",
accuracy.eval(
session=sess, feed_dict={X: mnist.test.images, Y: mnist.test.labels}
),
)
이렇게 결과 뽑으면 정확도가 왜 스레긴지 모르겠다..
Sample image show and prediction¶
In [49]:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Get one and predict
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]}),
)
plt.imshow(
mnist.test.images[r : r + 1].reshape(28,28),
cmap='Greys',
interpolation='nearest')
plt.show()
'beginner > 파이썬 딥러닝 기초' 카테고리의 다른 글
Tensor Manipulation (0) | 2019.05.06 |
---|---|
딥러닝의 기본 개념: 시작과 XOR 문제~Back-propagation과 2006/2007 '딥'의 출현 (0) | 2019.05.06 |
Training/Test dataset, learning rate, normalization (0) | 2019.05.05 |
Training/Testing 데이터 셋 이론 (0) | 2019.05.05 |
학습 rate, Overfitting, 그리고 일반화(Regularization) 이론 (0) | 2019.05.05 |