{"cells":[{"metadata":{"_kg_hide-input":false,"execution":{"iopub.execute_input":"2020-09-17T10:45:13.339291Z","iopub.status.busy":"2020-09-17T10:45:13.338525Z","iopub.status.idle":"2020-09-17T10:45:25.909282Z","shell.execute_reply":"2020-09-17T10:45:25.908505Z"},"papermill":{"duration":12.588943,"end_time":"2020-09-17T10:45:25.909411","exception":false,"start_time":"2020-09-17T10:45:13.320468","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n#tensorflow version 2.0\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\n\n#reading data\nmnist_train = pd.read_csv('../input/digit-recognizer/train.csv')\nmnist_test = pd.read_csv('../input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-17T10:45:25.947992Z","iopub.status.busy":"2020-09-17T10:45:25.947025Z","iopub.status.idle":"2020-09-17T10:45:25.951079Z","shell.execute_reply":"2020-09-17T10:45:25.950442Z"},"papermill":{"duration":0.026727,"end_time":"2020-09-17T10:45:25.951190","exception":false,"start_time":"2020-09-17T10:45:25.924463","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"mnist_train.shape,mnist_test.shape","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2020-09-17T10:45:25.991911Z","iopub.status.busy":"2020-09-17T10:45:25.990897Z","iopub.status.idle":"2020-09-17T10:45:54.389052Z","shell.execute_reply":"2020-09-17T10:45:54.390043Z"},"papermill":{"duration":28.423369,"end_time":"2020-09-17T10:45:54.390339","exception":false,"start_time":"2020-09-17T10:45:25.966970","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"#standardization\nmnist_train.iloc[:,1:] /= 255\n\n#splitting features and target column\nx_train = mnist_train.iloc[:,1:]\ny_train = mnist_train.iloc[:,0]\nx_test= mnist_test/255\n\n#further splitting train set into validation and training set\nx_train,x_validate,y_train,y_validate = train_test_split(x_train,y_train,test_size = 0.3,random_state = 12345)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.015784,"end_time":"2020-09-17T10:45:54.428910","exception":false,"start_time":"2020-09-17T10:45:54.413126","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Let's have a look at the images in our dataset."},{"metadata":{"execution":{"iopub.execute_input":"2020-09-17T10:45:54.465026Z","iopub.status.busy":"2020-09-17T10:45:54.464273Z","iopub.status.idle":"2020-09-17T10:45:56.617852Z","shell.execute_reply":"2020-09-17T10:45:56.617005Z"},"papermill":{"duration":2.173168,"end_time":"2020-09-17T10:45:56.617979","exception":false,"start_time":"2020-09-17T10:45:54.444811","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor i in range(36):\n    plt.subplot(6, 6, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(np.array(x_test.iloc[i]).reshape(28,28))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.025726,"end_time":"2020-09-17T10:45:56.671219","exception":false,"start_time":"2020-09-17T10:45:56.645493","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Let's see if our data is balanced."},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-09-17T10:45:56.733641Z","iopub.status.busy":"2020-09-17T10:45:56.732500Z","iopub.status.idle":"2020-09-17T10:45:57.034008Z","shell.execute_reply":"2020-09-17T10:45:57.032888Z"},"papermill":{"duration":0.336148,"end_time":"2020-09-17T10:45:57.034142","exception":false,"start_time":"2020-09-17T10:45:56.697994","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"sns.countplot(y_train)\nplt.title('Classes distribution in train set');","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-09-17T10:45:57.093637Z","iopub.status.busy":"2020-09-17T10:45:57.091686Z","iopub.status.idle":"2020-09-17T10:45:57.390944Z","shell.execute_reply":"2020-09-17T10:45:57.390187Z"},"papermill":{"duration":0.331411,"end_time":"2020-09-17T10:45:57.391138","exception":false,"start_time":"2020-09-17T10:45:57.059727","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"sns.countplot(y_validate)\nplt.title('Classes distribution in validation set');","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.026432,"end_time":"2020-09-17T10:45:57.792054","exception":false,"start_time":"2020-09-17T10:45:57.765622","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Convolutional Neural Network\n\nThe term deep neural nets refers to any neural network with several hidden layers. Convolutional neural nets are a specific type of deep neural net which are especially useful for image recognition. Specifically, convolutional neural nets use convolutional and pooling layers, which reflect the translation-invariant nature of most images.\n\nFor this, we need to reshape our input data."},{"metadata":{"execution":{"iopub.execute_input":"2020-09-17T10:45:57.855579Z","iopub.status.busy":"2020-09-17T10:45:57.854571Z","iopub.status.idle":"2020-09-17T10:45:58.200076Z","shell.execute_reply":"2020-09-17T10:45:58.199362Z"},"papermill":{"duration":0.381664,"end_time":"2020-09-17T10:45:58.200215","exception":false,"start_time":"2020-09-17T10:45:57.818551","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"image_rows = 28\nimage_cols = 28\nimage_shape = (image_rows,image_cols,1)\nx_train = tf.reshape(x_train,[x_train.shape[0],*image_shape])\nx_test = tf.reshape(x_test,[x_test.shape[0],*image_shape])\nx_validate = tf.reshape(x_validate,[x_validate.shape[0],*image_shape])","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.027627,"end_time":"2020-09-17T10:45:58.257036","exception":false,"start_time":"2020-09-17T10:45:58.229409","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Now we define our model. The layer in model network (keras.layers.Flatten) transforms the format of the images from a two-dimensional array (of 28 by 28 pixels) to a one-dimensional array (of 28 * 28 = 784 pixels). This layer unstacks rows of pixels in the image and lining them up and has no parameters to learn; it only reformats the data. Pooling layers are then added to further reduce the number of parameters.\n\nAfter the pixels are flattened, the network consists of a sequence of two keras.layers.Dense layers. These are densely connected, or fully connected, neural layers.\n\nA problem with training neural networks is in the choice of the number of training epochs to use. Too many epochs can lead to overfitting of the training dataset, whereas too few may result in an underfit model. Early stopping is a method that allows you to specify an arbitrary large number of training epochs and stop training once the model performance stops improving on a hold out validation dataset."},{"metadata":{"_kg_hide-output":true,"execution":{"iopub.execute_input":"2020-09-17T10:45:58.323122Z","iopub.status.busy":"2020-09-17T10:45:58.322385Z","iopub.status.idle":"2020-09-17T10:56:25.594725Z","shell.execute_reply":"2020-09-17T10:56:25.589976Z"},"papermill":{"duration":627.310543,"end_time":"2020-09-17T10:56:25.594936","exception":false,"start_time":"2020-09-17T10:45:58.284393","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"cnn_model = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu',input_shape = image_shape),\n    tf.keras.layers.MaxPooling2D(pool_size=2) ,# down sampling the output instead of 28*28 it is 14*14\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu',input_shape = image_shape),\n    tf.keras.layers.MaxPooling2D(pool_size=2) ,\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Flatten(), # flatten out the layers\n    tf.keras.layers.Dense(200,activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(200,activation='relu'),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(25,activation = 'softmax')\n])\n\ncnn_model.compile(loss ='sparse_categorical_crossentropy',\n                  optimizer='adam',metrics =['accuracy'])\n\nearly_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=8)\n\nhistory = cnn_model.fit(\n    x_train,\n    y_train,\n    batch_size=500,\n    epochs=60,\n    verbose=1,\n    validation_data=(x_validate,y_validate),\n    callbacks=early_stop\n)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2020-09-17T10:56:34.154871Z","iopub.status.busy":"2020-09-17T10:56:34.154101Z","iopub.status.idle":"2020-09-17T10:56:34.539873Z","shell.execute_reply":"2020-09-17T10:56:34.538833Z"},"papermill":{"duration":1.551513,"end_time":"2020-09-17T10:56:34.540004","exception":false,"start_time":"2020-09-17T10:56:32.988491","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(10, 10))\n\nplt.subplot(2, 2, 1)\nplt.plot(history.history['loss'], label='Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.legend()\nplt.title('Training - Loss Function')\n\nplt.subplot(2, 2, 2)\nplt.plot(history.history['accuracy'], label='Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.legend()\nplt.title('Training - Accuracy');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Making submissions\nsubmissions = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsubmissions['Label'] = cnn_model.predict_classes(x_test)\nsubmissions.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":1.17501,"end_time":"2020-09-17T10:56:46.057528","exception":false,"start_time":"2020-09-17T10:56:44.882518","status":"completed"},"tags":[]},"cell_type":"markdown","source":"***Please upvote and provide suggestions.***"}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}