Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Handrwritten-Digit-Recognizer

Build a TensorFlow classifier for recognizing handwritten digits

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

out: 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

  1. Check type of Dataset
type(mnist)

out: tensorflow.contrib.learn.python.learn.datasets.base.Datasets

  1. Array of Training images
mnist.train.images
#4. size of training data
mnist.train.num_examples

out:

55000
  1. Visualize the Data
import matplotlib.pyplot as plt
%matplotlib inline
mnist.train.images[1].shape

out: (784,)

plt.imshow(mnist.train.images[15].reshape(28,28))

out: <matplotlib.image.AxesImage at 0x1a547431128>

png

  1. Maximum and minimum value of the pixels in the image
mnist.train.images[1].max()

out:

1.0
  1. Create the Model
x = tf.placeholder(tf.float32,shape = [None,784])
#24*24 = 784 pixel images
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
  1. Create the Graph
y = tf.matmul(x,W) +b
#y = Wi*xi+b
y_true = tf.placeholder(tf.float32,[None,10])
#Cross Entropy
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.5)
train = optimizer.minimize(cross_entropy)
  1. Create the Session
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    #Train the model for 1000 steps on the training set using built in batch feeder from mnist
    for  step in range(1000):
        batch_x,batch_y=mnist.train.next_batch(1000)
        sess.run(train, feed_dict = {x:batch_x,y_true:batch_y})
#9. Evaluate the Trained model on Test Data
    #Test the trained model
    matches = tf.equal(tf.argmax(y,1),tf.argmax(y_true,1))
    acc = tf.reduce_mean(tf.cast(matches,tf.float32))
    print(sess.run(acc,feed_dict={x:mnist.test.images,y_true:mnist.test.labels}))

out: 0.9212 The model can recognize handwritten digits of images (28*28=784 pixels) with accuracy 0.9212 this accuracy can be improved further by training the model.

About

Build a TensorFlow classifier for recognizing handwritten digits example

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages