{"cells":[{"metadata":{"_uuid":"d9e7ed469e3e32a8918d26c2e9858f8be0bdd8ba"},"cell_type":"markdown","source":"# My first CNN solution\nThis notebook contains my first deep neural network solution to hand written digit recognition problem. I hope it helps out anyone who is seeking."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tensorflow.python import keras\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.optimizers import RMSprop\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Dropout, Flatten, Conv2D\n\nimport os\nprint(os.listdir(\"../input\"))\n\nimg_rows = 28\nimg_cols = 28\nnum_classes = 10\n\ntrainData = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')\n\ny = trainData.label\nx = trainData.iloc[:,1:]\n\n# normalize\nx = x / 255.0\ntest = test / 255.0\n\n# label encoding\ny = to_categorical(y,num_classes)\n\n# reshape imgs\nx = x.values.reshape(-1,img_rows,img_cols,1)\ntest = test.values.reshape(-1,img_rows,img_cols,1)\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"# Build CNN Model"},{"metadata":{"trusted":true,"_uuid":"667c784f41703633ab11c28b83b943e58ee2206d"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(12, kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\n\nmodel.add(Conv2D(12, kernel_size=(3, 3), activation='relu'))\nmodel.add(Conv2D(12, kernel_size=(3, 3), activation='relu'))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(num_classes, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5ec2f75d75571fcac7a56378a57f4a49ece552a7"},"cell_type":"markdown","source":"# Compile Model"},{"metadata":{"trusted":true,"_uuid":"132498cf19565c6a86f21cb5e247600355d52e34"},"cell_type":"code","source":"model.compile(optimizer ='adam',\n              loss=keras.losses.categorical_crossentropy,\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5b0f2a7bb892c56acc547961d52a94673ae8f415"},"cell_type":"markdown","source":"# Fit Model"},{"metadata":{"trusted":true,"_uuid":"cfbda15570a7b629236d5b58d670cd74b2fcabb5"},"cell_type":"code","source":"history = model.fit(x, y,\n                    batch_size=300,\n                    epochs=10,\n                    validation_split = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9b17c1b7f4d8beaffb1b57133fc1ee1a1fff41d"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Plot the loss and accuracy curves for training and validation \nfig, ax = plt.subplots(2,1)\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best')\n\nax[1].plot(history.history['acc'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f241f54665bef95c4084bf53b41a8eea7e48019a"},"cell_type":"markdown","source":"# Get Results for Test file and generate Submission file"},{"metadata":{"trusted":true,"_uuid":"7454cf3dfb9d9619d8bb9a3a1f17325dbf2b56bd"},"cell_type":"code","source":"# predict results\nresults = model.predict(test)\n\n# select the index with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")\n\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"submission.csv\",index=False)","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}