Notebook dowloaded from: https://github.com/wagonhelm/NaNmnist


In [5]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import matplotlib.pyplot as plt
import numpy as np
import random as ran
%matplotlib inline


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

In [6]:
import tensorflow as tf

In [7]:
def TRAIN_SIZE(num):
    print ('Total Training Images in Dataset = ' + str(mnist.train.images.shape))
    print ('--------------------------------------------------')
    x_train = mnist.train.images[:num,:]
    print ('x_train Examples Loaded = ' + str(x_train.shape))
    y_train = mnist.train.labels[:num,:]
    print ('y_train Examples Loaded = ' + str(y_train.shape))
    print('')
    return x_train, y_train

def TEST_SIZE(num):
    print ('Total Test Examples in Dataset = ' + str(mnist.test.images.shape))
    print ('--------------------------------------------------')
    x_test = mnist.test.images[:num,:]
    print ('x_test Examples Loaded = ' + str(x_test.shape))
    y_test = mnist.test.labels[:num,:]
    print ('y_test Examples Loaded = ' + str(y_test.shape))
    return x_test, y_test

def display_digit(num):
    print(y_train[num])
    label = y_train[num].argmax(axis=0)
    image = x_train[num].reshape([28,28])
    plt.title('Example: %d  Label: %d' % (num, label))
    plt.imshow(image, cmap=plt.get_cmap('gray_r'))
    plt.show()
        
def display_mult_flat(start, stop):
    images = x_train[start].reshape([1,784])
    for i in range(start+1,stop):
        images = np.concatenate((images, x_train[i].reshape([1,784])))
    plt.imshow(images, cmap=plt.get_cmap('gray_r'))
    plt.show()

In [8]:
x_train, y_train = TRAIN_SIZE(55000)


Total Training Images in Dataset = (55000, 784)
--------------------------------------------------
x_train Examples Loaded = (55000, 784)
y_train Examples Loaded = (55000, 10)


In [9]:
display_digit(0)


[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]

In [10]:
display_mult_flat(0,400)



In [11]:
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

In [12]:
x_train, y_train = TRAIN_SIZE(5500)
x_test, y_test = TEST_SIZE(10000)
LEARNING_RATE = 0.05
TRAIN_STEPS = 4000

sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)


Total Training Images in Dataset = (55000, 784)
--------------------------------------------------
x_train Examples Loaded = (5500, 784)
y_train Examples Loaded = (5500, 10)

Total Test Examples in Dataset = (10000, 784)
--------------------------------------------------
x_test Examples Loaded = (10000, 784)
y_test Examples Loaded = (10000, 10)
WARNING:tensorflow:From /home/pyr0/.virtualenvs/ml-notebook-to-prod/lib/python3.6/site-packages/tensorflow/python/util/tf_should_use.py:189: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.

In [13]:
training = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

In [14]:
for i in range(TRAIN_STEPS+1):
    sess.run(training, feed_dict={x: x_train, y_: y_train})
    if i%100 == 0:
        print('Training Step:' + str(i) + '  Accuracy =  ' + str(sess.run(accuracy, feed_dict={x: x_test, y_: y_test})) + '  Loss = ' + str(sess.run(cross_entropy, {x: x_train, y_: y_train})))


Training Step:0  Accuracy =  0.5988  Loss = 2.2444756
Training Step:100  Accuracy =  0.8408  Loss = 0.76828074
Training Step:200  Accuracy =  0.8645  Loss = 0.5821603
Training Step:300  Accuracy =  0.8738  Loss = 0.50489426
Training Step:400  Accuracy =  0.8789  Loss = 0.46047613
Training Step:500  Accuracy =  0.8827  Loss = 0.43072158
Training Step:600  Accuracy =  0.8863  Loss = 0.40892836
Training Step:700  Accuracy =  0.8883  Loss = 0.39200595
Training Step:800  Accuracy =  0.8905  Loss = 0.37831423
Training Step:900  Accuracy =  0.8924  Loss = 0.36689445
Training Step:1000  Accuracy =  0.8942  Loss = 0.3571444
Training Step:1100  Accuracy =  0.8959  Loss = 0.3486652
Training Step:1200  Accuracy =  0.8975  Loss = 0.34118065
Training Step:1300  Accuracy =  0.8986  Loss = 0.33449262
Training Step:1400  Accuracy =  0.8984  Loss = 0.32845518
Training Step:1500  Accuracy =  0.899  Loss = 0.32295752
Training Step:1600  Accuracy =  0.8999  Loss = 0.3179147
Training Step:1700  Accuracy =  0.9004  Loss = 0.31325907
Training Step:1800  Accuracy =  0.9005  Loss = 0.30893764
Training Step:1900  Accuracy =  0.9009  Loss = 0.30490646
Training Step:2000  Accuracy =  0.9009  Loss = 0.3011301
Training Step:2100  Accuracy =  0.9016  Loss = 0.2975788
Training Step:2200  Accuracy =  0.9023  Loss = 0.29422808
Training Step:2300  Accuracy =  0.9028  Loss = 0.29105675
Training Step:2400  Accuracy =  0.9029  Loss = 0.28804696
Training Step:2500  Accuracy =  0.9034  Loss = 0.2851834
Training Step:2600  Accuracy =  0.9033  Loss = 0.28245285
Training Step:2700  Accuracy =  0.9038  Loss = 0.27984378
Training Step:2800  Accuracy =  0.9039  Loss = 0.27734575
Training Step:2900  Accuracy =  0.9043  Loss = 0.2749502
Training Step:3000  Accuracy =  0.9048  Loss = 0.27264914
Training Step:3100  Accuracy =  0.9055  Loss = 0.2704355
Training Step:3200  Accuracy =  0.9057  Loss = 0.26830304
Training Step:3300  Accuracy =  0.9057  Loss = 0.2662462
Training Step:3400  Accuracy =  0.9062  Loss = 0.26425976
Training Step:3500  Accuracy =  0.9058  Loss = 0.26233932
Training Step:3600  Accuracy =  0.9061  Loss = 0.2604808
Training Step:3700  Accuracy =  0.906  Loss = 0.25868016
Training Step:3800  Accuracy =  0.9063  Loss = 0.25693423
Training Step:3900  Accuracy =  0.9065  Loss = 0.2552398
Training Step:4000  Accuracy =  0.9066  Loss = 0.25359392

In [15]:
for i in range(10):
    plt.subplot(2, 5, i+1)
    weight = sess.run(W)[:,i]
    plt.title(i)
    plt.imshow(weight.reshape([28,28]), cmap=plt.get_cmap('seismic'))
    frame1 = plt.gca()
    frame1.axes.get_xaxis().set_visible(False)
    frame1.axes.get_yaxis().set_visible(False) 
plt.show()



In [16]:
x_train, y_train = TRAIN_SIZE(1) 
display_digit(0)


Total Training Images in Dataset = (55000, 784)
--------------------------------------------------
x_train Examples Loaded = (1, 784)
y_train Examples Loaded = (1, 10)

[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]

In [17]:
answer = sess.run(y, feed_dict={x: x_train})
print(answer)


[[3.4864242e-05 1.5520862e-05 9.0627335e-02 1.8269842e-02 1.1456280e-03
  1.8368168e-05 7.0805370e-05 8.4868628e-01 7.5114537e-03 3.3619948e-02]]

In [18]:
answer.argmax()


Out[18]:
7

In [19]:
def display_compare(num):
    # THIS WILL LOAD ONE TRAINING EXAMPLE
    x_train = mnist.train.images[num,:].reshape(1,784)
    y_train = mnist.train.labels[num,:]
    
    # THIS GETS OUR LABEL AS A INTEGER
    label = y_train.argmax()
    
    # THIS GETS OUR PREDICATION AS A INTEGER
    prediction = sess.run(y, feed_dict={x: x_train}).argmax() 
    
    plt.title('Prediction: %d Label: %d' % (prediction, label))
    plt.imshow(x_train.reshape([28,28]), cmap=plt.get_cmap('gray_r'))
    plt.show()

In [20]:
display_compare(ran.randint(0, 55000))