티스토리 뷰

 

 

 

 

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]))
 
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
 

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}
        ),
    )
 
Epoch: 0001, Cost: 2.895244860
Epoch: 0002, Cost: 1.110244753
Epoch: 0003, Cost: 0.872411700
Epoch: 0004, Cost: 0.759510901
Epoch: 0005, Cost: 0.690217822
Epoch: 0006, Cost: 0.641041565
Epoch: 0007, Cost: 0.604330420
Epoch: 0008, Cost: 0.575824975
Epoch: 0009, Cost: 0.552191756
Epoch: 0010, Cost: 0.531711015
Epoch: 0011, Cost: 0.514700532
Epoch: 0012, Cost: 0.500018633
Epoch: 0013, Cost: 0.487149436
Epoch: 0014, Cost: 0.475324202
Epoch: 0015, Cost: 0.464626287
Learning finished
Accuracy:  0.8856
 

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()
 
Label: [4]
Prediction: [4]
 
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함