{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Handwritten Digit classification Using Neural Networks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " First we import tensorflow and load the dataset from tensorflow.examples.tutorial.mnist" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Extracting data/mnist/train-images-idx3-ubyte.gz\n", "Extracting data/mnist/train-labels-idx1-ubyte.gz\n", "Extracting data/mnist/t10k-images-idx3-ubyte.gz\n", "Extracting data/mnist/t10k-labels-idx1-ubyte.gz\n" ] } ], "source": [ "import warnings\n", "warnings.filterwarnings('ignore')\n", "import tensorflow as tf\n", "from tensorflow.examples.tutorials.mnist import input_data\n", "mnist = input_data.read_data_sets(\"data/mnist/\", one_hot=True)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ " Now, we will see what we got in our data," ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No of images in training set (55000, 784)\n", "No of labels in training set (55000, 10)\n", "No of images in test set (10000, 784)\n", "No of labels in test set (10000, 10)\n" ] } ], "source": [ "print(\"No of images in training set {}\".format(mnist.train.images.shape))\n", "print(\"No of labels in training set {}\".format(mnist.train.labels.shape))\n", "\n", "print(\"No of images in test set {}\".format(mnist.test.images.shape))\n", "print(\"No of labels in test set {}\".format(mnist.test.labels.shape))\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "We have 55000 images in the training set and each image is of size 784 and we have 10\n", "labels which are actually 0 to 9. Similarly, we have 10000 images in the test set. \n", "\n", "Now we plot an input image to see how it looks like, " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAA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+L+3crOruldIaSOjU3jq1L4neGlVWb7zt\nB4Ii/EBQZYe/p+T9p3Rqb53al0RvjSqlt1I/8wMoT9lHfgAlKSX8ZjbLzN43s71m9lAZPdRiZr1m\n9q6ZvWNmpU4pnE2DdsjMdg1YdomZbTazPdntoNOkldTbY2b2f9lr946Z/UNJvY03s/8ys91m9jsz\neyBbXuprl+irlNet7W/7zaxL0geSbpa0T9Kbkua5+/+2tZEazKxXUsXdSx8TNrMbJP1Z0ip3n5wt\n+xdJR9z9iewP50h3/6cO6e0xSX8ue+bmbEKZcQNnlpZ0u6R/VImvXaKvO1TC61bGkX+GpL3u/nt3\nPyHpV5Jml9BHx3P31yQdOWvxbEkrs/sr1f8/T9vV6K0juPsBd38ru39M0pmZpUt97RJ9laKM8F8m\n6Y8DHu9TZ0357ZI2mdkOM1tQdjODGJNNm35m+vTRJfdzttyZm9vprJmlO+a1a2TG66KVEf7BZv/p\npCGH69x9qqRbJf0oe3uL+tQ1c3O7DDKzdEdodMbropUR/n2Sxg94/DVJ+0voY1Duvj+7PSRpnTpv\n9uGDZyZJzW4PldzPX3TSzM2DzSytDnjtOmnG6zLC/6akK83s62b2FUlzJW0ooY8vMbOLsi9iZGYX\nSfq2Om/24Q2S5mf350taX2IvX9ApMzfXmllaJb92nTbjdSk/8smGMv5VUpek5e7+z21vYhBmdoX6\nj/ZS/ySmvyyzNzNbI+lG9Z/1dVDSTyT9u6S1kiZI+oOk77l72794q9Hbjep/6/qXmZvPfMZuc29/\nJ+m/Jb0r6XS2eLH6P1+X9tol+pqnEl43fuEHBMUv/ICgCD8QFOEHgiL8QFCEHwiK8ANBEX4gKMIP\nBPX/lUYsit1vv9AAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "img1 = mnist.train.images[41].reshape(28,28)\n", "plt.imshow(img1, cmap='Greys')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we define hyperparameters which is learning rate, batch size and number of epcohs" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "learning_rate = 0.1\n", "batch_size = 100\n", "epochs = 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "Let us start building our network. We build the 2 layer neural network with one input\n", "layer and one hidden layer and one output layer which predicts handwritten digit.\n", "\n", "First, we define the placeholders for our input and output. As our input data shape is 784,\n", "we can define input placeholder as" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = tf.placeholder(tf.float32, [None, 784])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "What does the None implies? None specifies the number of samples (batch size) passed\n", "which will be decided dynamically at run time.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since we have 10 classes as output, we can define output placeholder as," ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y = tf.placeholder(tf.float32, [None, 10])" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ " Now, we define the weight and bias between an input to the hidden layer as w_xh and\n", "b_h respectively. We initialize the weight matrix with values randomly drawing from a\n", "normal distribution with standard deviation of 0.03." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "w_ih = tf.Variable(tf.random_normal([784, 300], stddev=0.03), name='w_ih')\n", "b_h = tf.Variable(tf.random_normal([300]), name='b_h')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Next, we define the weights and bias between our hidden layer to the output layer as w_hy\n", "and b_y respectively." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "w_hy = tf.Variable(tf.random_normal([300, 10], stddev=0.03), name='w_hy')\n", "b_y = tf.Variable(tf.random_normal([10]), name='b_y')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us perform the forward propagation now, recall the steps we peformed in forward\n", "propagation. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "z1 = tf.add(tf.matmul(x, w_ih), b_h)\n", "a1 = tf.nn.relu(z1)\n", "z2 = tf.add(tf.matmul(a1, w_hy), b_y)\n", "yhat = tf.nn.softmax(z2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " We define our cost function cross entropy loss as," ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(yhat), reduction_indices=[1]))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Our objective is to minimize the cost function. We can minimize the cost function by\n", "propagating the network backward and perform gradient descent. With tensorflow we\n", "don't have to manually calculate the gradients, we can use tensorflow's built in gradient\n", "descent optimizer function as," ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "optimiser = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To evaluate our model, we will calculate accuracy as follows," ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(yhat, 1))\n", "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ " As we know that tensorflow runs by building the computation graph, whatever we have\n", "written so far, will actually run only if we start the tensorflow session. So let's do that.\n", "\n", "First, initialize tensorflow variables," ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "init_op = tf.global_variables_initializer()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ " Now start the tensorflow session and start training the model. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Epoch:', 1, 'cost =0.541')\n", "('Epoch:', 2, 'cost =0.292')\n", "('Epoch:', 3, 'cost =0.238')\n", "('Epoch:', 4, 'cost =0.196')\n", "('Epoch:', 5, 'cost =0.165')\n", "('Epoch:', 6, 'cost =0.142')\n", "('Epoch:', 7, 'cost =0.124')\n", "('Epoch:', 8, 'cost =0.111')\n", "('Epoch:', 9, 'cost =0.100')\n", "('Epoch:', 10, 'cost =0.090')\n" ] } ], "source": [ "with tf.Session() as sess:\n", " sess.run(init_op)\n", " total_batch = int(len(mnist.train.labels) / batch_size)\n", " \n", " for epoch in range(epochs):\n", " avg_cost = 0\n", " \n", " for i in range(total_batch):\n", " batch_x, batch_y = mnist.train.next_batch(batch_size=batch_size)\n", " \n", " _, c = sess.run([optimiser, cross_entropy], \n", " feed_dict={x: batch_x, y: batch_y})\n", " \n", " avg_cost += c / total_batch\n", " \n", " print(\"Epoch:\", (epoch + 1), \"cost =\"\"{:.3f}\".format(avg_cost))\n", " \n", " print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 2 }