{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"  # This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, Lambda\nfrom keras.optimizers import Adam ,RMSprop\nfrom sklearn.model_selection import train_test_split\nfrom keras import  backend as K\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils.np_utils import to_categorical\n\n#Running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# we import the training set:\ntrain = pd.read_csv(\"../input/train.csv\")\nprint(train.shape)\ntrain.head()\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a48d8f18bad9164edd1d6f282a7e9a2b0af9dcbd"},"cell_type":"code","source":"# we import the test set:\ntest= pd.read_csv(\"../input/test.csv\")\nprint(test.shape)\nprint(test.values)\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e132f77982f0feec399c518a0d2a04055f362c6"},"cell_type":"code","source":"#We create the test and train vectors\nY_train = train[\"label\"] # label digits\nprint (Y_train[0])\nX_train = train.drop(labels = [\"label\"],axis = 1) # pixels values, we just drop the label column\nprint(X_train.values)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d33477b3915cd07283479f98be7682eaf723505"},"cell_type":"code","source":"#normalization of the data set in grayscale\nX_train = X_train / 255.0\ntest = test / 255.0\n\n\n#reshaping in 28x28:\nX_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"654c7bdd924978b5e203690417086b2bbe491fd6"},"cell_type":"code","source":"#We transform the labels into vectors => i.e 5 becomes [0,0,0,0,0,1,0,0,0,0]\n#To do that, we'll use the to_categorical function from keras that does exactly that\nprint(Y_train[0])\nY_train = to_categorical(Y_train, num_classes = 10)\nprint(Y_train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4981ad371e544821ef63a7bb7c56cb31efedec4f"},"cell_type":"code","source":"#Let's set a CNN model. Here we'll start with the following architecture: [Conv2D->Maxpooling->Dropout]*2 -> Flatten -> Dense -> Dropout -> Out\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\n\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d7d772f70890a85e0d56cba2c38b563c84d01de"},"cell_type":"code","source":"#Now that the CNN model layers are set, we'll set up an optimizer. RMSprop seems to have pretty good performances\n#with low learning rates:\nrmsOptimizer = RMSprop(lr=0.001)\n#and we compile our model:\nmodel.compile(loss = \"categorical_crossentropy\", metrics=[\"accuracy\"], optimizer = rmsOptimizer)\nprint(model.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a1d818166bcb7b6bcfe033281da6a89a54f1e358"},"cell_type":"code","source":"#Fit the model. We'll use only 3 epochs for obvious computational reasons \nprint(X_train.shape)\nhistory = model.fit(X_train, Y_train, batch_size = 128, epochs = 3, \n          validation_split=0.1, verbose = 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"352f4fda4725936e650ab528b8196bb3ef0be6e9"},"cell_type":"code","source":"#Let's plot the loss and accuracy:\nimport matplotlib.pyplot as plt\nfig, ax = plt.subplots(2,1)#Separate in two plots\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', shadow=True)\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', shadow=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5aa79a2a40b462c479c79dfe61804c3d76c372d4"},"cell_type":"code","source":"#Eventually, let's predict our 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(\"cnn_mnist_datagen.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}