lab07-02.md

MNIST data

1. MNIST Dataset

1) MNIST Data

  • MNIST(Mixed National Institute of Standards and Technology database)

  • 우체국에서 수기로 작성한 우편번호(숫자: 0~9)를 인식하기 위한 data set

     


 

2) MNIST Data set 가져오기

  • Tensorflow에서는 MNIST에 Data set 가져올 수 있도록 라이브러리화 되어 있음

  • 실행 시 지정한 경로로 data set을 다운로드 함

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    
    mnist = input_data.read_data_sets("Data/MNIST_data/", one_hot=True)
    

 

  • Data set
데이터 종류데이터 셋 명차원(Shape)파일
training set imagesmnist.train.images.shape(55000, 784)train-images-idx3-ubyte.gz (9912422 bytes)
training set labelsmnist.train.labels.shape(55000, 10)train-labels-idx1-ubyte.gz (28881 bytes)
test set imagesmnist.test.images.shape(10000, 784)t10k-images-idx3-ubyte.gz (1648877 bytes)
test set labelsmnist.test.labels.shape(10000, 10)t10k-labels-idx1-ubyte.gz (4542 bytes)


3) 소스코드 구현을 위한 용어정리

  • One epoch: 전체 training data set을 한번 씩 모두 학습 시키는 것

  • Batch size: one epoch가 크고 메모리가 한정적이므로 나눠서 학습하기 위한 단위

    • Batch size가 클수록 더 많은 메모리 공간을 필요로 함
  • Iteration의 수: 전체 training data set / batch size

     

2. Example Code

1) 코드 실행 시 발생한 에러와 해결방법

  • 에러메시지
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>

 

  • 해결방법


2) 소스코드와 실행

  • 소스코드

    • softmax 함수를 이용
    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    import matplotlib.pyplot as plt
    import random
    
    mnist = input_data.read_data_sets("Data/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]))
    
    # 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))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
    
    # Test model
    is_correct = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))
    accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
    
    training_epochs = 15
    batch_size = 100
    
    with tf.Session() as sess:
        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)
    
            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
    
            print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))
    
        # Test the model using test sets
        print("Accuracy: ", accuracy.eval(session=sess,
                                          feed_dict={X: mnist.test.images, Y: mnist.test.labels}))
    
        # sample image show and prediction
        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]}))
    
        print("sample image shape:", mnist.test.images[r:r+1].shape)
        plt.imshow(mnist.test.images[r:r+1].reshape(28, 28), cmap='Greys', interpolation='nearest')
        plt.show()
    

     

  • 결과

    Extracting Data/MNIST_data/train-images-idx3-ubyte.gz
    Extracting Data/MNIST_data/train-labels-idx1-ubyte.gz
    Extracting Data/MNIST_data/t10k-images-idx3-ubyte.gz
    Extracting Data/MNIST_data/t10k-labels-idx1-ubyte.gz
    Epoch: 0001 cost = 2.863736535
    Epoch: 0002 cost = 1.122258349
    Epoch: 0003 cost = 0.890512572
    Epoch: 0004 cost = 0.776820077
    Epoch: 0005 cost = 0.705135050
    Epoch: 0006 cost = 0.655167040
    Epoch: 0007 cost = 0.617489539
    Epoch: 0008 cost = 0.587442237
    Epoch: 0009 cost = 0.562598704
    Epoch: 0010 cost = 0.542466953
    Epoch: 0011 cost = 0.525049096
    Epoch: 0012 cost = 0.509709905
    Epoch: 0013 cost = 0.496286628
    Epoch: 0014 cost = 0.484218249
    Epoch: 0015 cost = 0.473724156
    Accuracy:  0.8853
    Label: [8]
    Prediction: [8]
    sample image shape: (1, 784)
    


'AI&BigData > Deep Learning' 카테고리의 다른 글

Lab10-1. ReLU: Better non-linearity  (0) 2018.05.18
Lab09-3. Sigmoid Backpropagation  (0) 2018.05.18
Lab09-2. TensorBoard  (0) 2018.05.05
Lab09-1. NN for XOR  (0) 2018.04.27
Lab08-2. Deep Learning의 기본 개념  (0) 2018.04.26
Lab08-1. Tensor Manipulation  (0) 2018.04.20
Lab07-1. Application&Tip  (0) 2018.04.18
Lab06. Softmax Classification  (0) 2018.04.18
Lab05. Logistic classification  (0) 2018.04.18