{"cells":[{"metadata":{"_uuid":"8eea19e6c5cd965636867004707d213d39da2b58"},"cell_type":"markdown","source":"# CNN using MNIST dataset"},{"metadata":{"_uuid":"57fe5cad682cbf4bed4c43793bca72eb563e313c"},"cell_type":"markdown","source":"### Loading all the dependency"},{"metadata":{"trusted":true,"_uuid":"091f6f1f43613ebdc8427a5835d493577a36aa16"},"cell_type":"code","source":"import numpy as np \nimport tensorflow as tf\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tensorflow.examples.tutorials.mnist import input_data","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d22c52fbd5b9215d25da58ca035db1af7ed5c69b"},"cell_type":"markdown","source":"### Loading data"},{"metadata":{"trusted":true,"_uuid":"a4b823d82a9287b7044210ee9c199f134873837e"},"cell_type":"code","source":"train_data = pd.read_csv(\"../input/digit-recognizer/train.csv\" , nrows=5000)\n#test_data = pd.read_csv(\"../input/digit-recognizer/test.csv\", nrows=10000)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"19d3ba8ed14a5fc1a6ad81c12ba233fd1dc92bf1"},"cell_type":"markdown","source":"#### Thanks a lot. **@tylerneylon**\nI got the following function to read \"idx\" files from [**here**](https://gist.github.com/tylerneylon/ce60e8a06e7506ac45788443f7269e40))"},{"metadata":{"trusted":true,"_uuid":"f4cc3a67757ca4e28ca73cc8028fc7a937577163"},"cell_type":"code","source":"import struct\ndef read_idx(filename):\n    with open(filename, 'rb') as f:\n        zero, data_type, dims = struct.unpack('>HBB', f.read(4))\n        shape = tuple(struct.unpack('>I', f.read(4))[0] for d in range(dims))\n        return np.fromstring(f.read(), dtype=np.uint8).reshape(shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"181df8a0bd728e26f861886763f7068b9c9e6bbc"},"cell_type":"code","source":"test_data = read_idx(\"../input/mytestdata/Test_data.idx3-ubyte\")[:1000]\ntest_labels = read_idx(\"../input/mytestdata/Test_labels.idx1-ubyte\")[:1000]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7e6dbee8e7574fd6484fd63c32a1c618f63bfb94"},"cell_type":"code","source":"train_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c4df943b38fd62ad951db649f41c87f91ef4870a"},"cell_type":"code","source":"labels = train_data[\"label\"].values\nlabels.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"98d48d518eb2b4abc2ebcace92ea2adf655cbcb8"},"cell_type":"markdown","source":"### Visualizing  the data"},{"metadata":{"trusted":true,"_uuid":"5cd61a2b4df1aceaec527cca55377d9cc593654a"},"cell_type":"code","source":"train_data.drop(\"label\",axis=1, inplace=True)\nplt.figure(figsize=(20,6))\nsns.countplot(labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cda4407576996bdc61d50849ab921ffde191fee5"},"cell_type":"code","source":"one_hot_labels = pd.get_dummies(labels)\none_hot_test_labels = pd.get_dummies(test_labels)\nprint(\"The shape of training labels :{} \\n The shape of test labels {}\".format(one_hot_labels.shape, one_hot_test_labels.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de0120e5bd5927e68b6bed4f6472c77cb93d3c5b"},"cell_type":"code","source":"image_data = train_data.values\nimages = np.array([image_data[i].reshape(28,28) for i in range(image_data.shape[0])])\ntest_images = test_data.shape\ntest_data = np.array([test_data[i].reshape(-1) for i in range(test_data.shape[0])])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"deb48f1674d9af59453330397fca4647b5991dd5"},"cell_type":"code","source":"print(\"shape of image data {} \\n shape of images {} \\n shape of test data{} \\n shape of test images {}\"\n      .format(image_data.shape , images.shape , test_data.shape , test_images))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8232e186f3ac1499e33ad763c41a1f83e81a590"},"cell_type":"code","source":"plt.imshow(images[2])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"964511c3041da85f159a187ce00451d0ac3e1d01"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bcbf2cfb6e59c5e9eae9cd6303433c6eeac53483"},"cell_type":"markdown","source":"### Create the Model"},{"metadata":{"_uuid":"97de1c32526bf2a8108a1a3e6d179340a15ec4e8"},"cell_type":"markdown","source":"### defining place holders"},{"metadata":{"trusted":true,"_uuid":"13029bb97acb44df18bd8b237b63426845d1cee3"},"cell_type":"code","source":"X = tf.placeholder(tf.float32 , shape= [None, 784], name=\"Images\")\ny = tf.placeholder(tf.float32,  shape= [None , 10], name=\"Labels\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1a6a396e66f9dade825d41e7048d6572c3cc3567"},"cell_type":"markdown","source":"### weight initialisation"},{"metadata":{"trusted":true,"_uuid":"464e8c8dadfcb363bad89dda6211bbb8a494f514"},"cell_type":"code","source":"def initialisation_weight(shape):\n    init_weight = tf.truncated_normal(shape , stddev=0.1)\n    return tf.Variable(init_weight)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f7abf0a959b8c3398613d3cc3d940fafcb89da61"},"cell_type":"markdown","source":"### Bias initialisation"},{"metadata":{"trusted":true,"_uuid":"4278c4c185a963659f7fce8ff7da6ca70e55a6bd"},"cell_type":"code","source":"def initialisation_bias(shape):\n    init_bias = tf.constant(0.1 , shape=shape)\n    return tf.Variable(init_bias)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b865cc73288270051f99853aa43693129855f776"},"cell_type":"markdown","source":"### defining convolution2d"},{"metadata":{"trusted":true,"_uuid":"56435c5e677faa2f36af9db1a4a87af51587aa92"},"cell_type":"code","source":"def conv2d(x,W):\n    return tf.nn.conv2d(x,W, strides=[1,1,1,1], padding=\"SAME\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3fe007cfe832ffe88bc3e1473201775e1414ff3c"},"cell_type":"markdown","source":"### defining max pooling layer"},{"metadata":{"trusted":true,"_uuid":"81c5238f14ac8d8edf792aa86dce39230526ce1f"},"cell_type":"code","source":"def maxpool(x):\n    return tf.nn.max_pool(x , ksize = [1,2,2,1] , strides=[1,2,2,1] , padding=\"SAME\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"812c9bb8fd05d26f688e4eb07b6b66aad1141460"},"cell_type":"code","source":"def convolutional_layer(input_x , shape):\n    W= initialisation_weight(shape)\n    b= initialisation_bias([shape[3]])\n    \n    return tf.nn.relu(conv2d(input_x , W) + b)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9f44840a927263bcee37d162760a16d334b7c918"},"cell_type":"code","source":"def normal_layer(input_layer , size):\n    input_size = int(input_layer.get_shape()[1])\n    W = initialisation_weight([input_size , size])\n    b= initialisation_bias([size])\n    \n    return tf.matmul(input_layer , W) +b","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efe3eaf43a63fddf26b4ef70a8b219b7defa1cba"},"cell_type":"code","source":"x_image = tf.reshape(X,[-1,28,28,1])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5c3fd1ae808351def8be585a2ab696d1243b0eb8"},"cell_type":"markdown","source":"### Defining CNN Model"},{"metadata":{"trusted":true,"_uuid":"0922bafc8ba09736e53432a05303b64114505129"},"cell_type":"code","source":"convo_1 = convolutional_layer(x_image,shape=[6,6,1,32])\nconvo_1_pooling = maxpool(convo_1)\nconvo_2 = convolutional_layer(convo_1_pooling,shape=[6,6,32,64])\nconvo_2_pooling = maxpool(convo_2)\nconvo_2_flat = tf.reshape(convo_2_pooling,[-1,7*7*64])\nfull_layer_one = tf.nn.relu(normal_layer(convo_2_flat,256))\nhold_prob = tf.placeholder(tf.float32)\nfull_one_dropout = tf.nn.dropout(full_layer_one,keep_prob=hold_prob)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9760fc795aab56e2435734331d2c760c872957cc"},"cell_type":"markdown","source":"### Output layer ( prediction layer )"},{"metadata":{"trusted":true,"_uuid":"9660006400adf00d93aff14971efa9edd748c007"},"cell_type":"code","source":"y_pred = normal_layer(full_one_dropout,10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fba76114803d98ab84e8b34042397a87b13d8953"},"cell_type":"markdown","source":"### Loss fucntion and Optimizer"},{"metadata":{"trusted":true,"_uuid":"abf941ff0f9e4ec4472b9d3a6440861897a764d2"},"cell_type":"code","source":"cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_pred))\noptimizer = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(cross_entropy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e9b98fda0746aa7a5cf606aa756f4e5be57bf9e","scrolled":false},"cell_type":"code","source":"init = tf.global_variables_initializer()\nsteps = 20\nwith tf.Session() as sess:\n    sess.run(init)\n    for i in range(steps):\n        \n        sess.run(optimizer,feed_dict={X:image_data,y:one_hot_labels,hold_prob:0.5})\n        \n        # PRINT OUT A MESSAGE EVERY 100 STEPS\n        if i%2 == 0:\n            \n            print('Currently on step {}'.format(i))\n            print('Accuracy is:')\n            # Test the Train Model\n            matches = tf.equal(tf.argmax(y_pred,1),tf.argmax(y,1))\n            acc = tf.reduce_mean(tf.cast(matches,tf.float32))\n\n            #print(sess.run(acc,feed_dict={X:test_images,y:mnist.test.labels,hold_prob:1.0}))\n            #print('\\n')\n            print(sess.run(acc , feed_dict={X:test_data, y:one_hot_test_labels , hold_prob:1}))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}