{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":false,"_kg_hide-output":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)\nimport keras\nimport matplotlib.pyplot as plt\nimport os\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nprint(os.listdir(\"../input\"))\n\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"82f506c34671bd5a9992b2d83faf949bfe06eb13"},"cell_type":"code","source":"X_train = train.iloc[:,1:].values\ny_train = train.iloc[:,0].values\nX_test = train.iloc[:,1:].values\ny_test = train.iloc[:,0].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8996e5def4913c76bcd0bd0ca7d78e58852942f8"},"cell_type":"code","source":"# to plot the image from training data\nimg = np.reshape(X_train[101] , (28,28))\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b4d2c06e9ccc54576400482fb39ba0baa23ae12c"},"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\nX_test = X_test.reshape(X_test.shape[0], 28, 28, 1)\n# normalise the data\nX_train = X_train/255\nX_test = X_test/255","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"33d4130b7783978000813c7fab1baf656839950e"},"cell_type":"code","source":"# convert labels into one hot encoded\nfrom keras.utils.np_utils import to_categorical\ny_train = to_categorical(y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d4bb91febd5053266a78d8e7f1e03281f6a2604"},"cell_type":"code","source":"y_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9ca26c2f1ef8aaa756991e85cd59fe703b5517c"},"cell_type":"code","source":"from keras import Sequential\nfrom keras.layers import Dense,Convolution2D , MaxPooling2D, BatchNormalization,Dropout,Flatten\nfrom keras.activations import relu","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"933cf2282b3da06f47be88665b59e28a8340767b"},"cell_type":"code","source":"# build convolutional neural network\ndef cnn():\n    model = Sequential([\n        # 1st conv layer\n        Convolution2D(32 ,kernel_size = (3,3) , input_shape = (28,28 ,1) , strides = (1,1) , padding = \"SAME\",activation = \"relu\"),\n        MaxPooling2D(pool_size = (2,2) , strides = (2,2)),\n        BatchNormalization(),\n        Dropout(0.3),\n        \n        #2nd conv layer\n        Convolution2D(64 ,kernel_size = (3,3) , strides = (1,1) , padding = \"SAME\"),\n        MaxPooling2D(pool_size = (2,2) , strides = (2,2)),\n        BatchNormalization(),\n        Dropout(0.3),\n        # 3rd conv layer\n        Convolution2D(64 , kernel_size = (3,3) , strides = (1,1) , padding = \"SAME\"),\n        MaxPooling2D(pool_size = (2,2) , strides = (2,2)),\n        BatchNormalization(),\n        Dropout(0.3),\n        \n        # Flatten the output\n        Flatten(),\n        \n        # fully connected dense layer\n        Dense(128 , activation = 'relu'),\n        Dense(10 , activation = 'softmax')\n        \n    ])\n    \n    model.compile(optimizer = 'adam' , loss = 'categorical_crossentropy' , metrics = ['accuracy'])\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d13911f7043ba883d700bfc5a1d888e8d26919e8"},"cell_type":"code","source":"classifier = cnn()\nprint(classifier.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7745810a22b112c1ead35fe4c975440ed05a509"},"cell_type":"code","source":"classifier.optimizer.lr = 0.001","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebcf0488889534bdfb0dc91bb315d871c415f306"},"cell_type":"code","source":"history = classifier.fit(X_train , y_train , batch_size = 35 , epochs = 20 , validation_split = 0.2 , verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"992cad4369c3b3e3baabd39be147146a5b0e7572"},"cell_type":"code","source":"plt.plot(history.history['loss'] , 'green')\nplt.plot(history.history['val_loss'] , 'red')\nplt.xlabel('Epochs')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33ea3b0be90608ac47f306945ac2370c422edd82"},"cell_type":"code","source":"plt.plot(history.history['acc'] , 'blue')\nplt.plot(history.history['val_acc'] , 'orange')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e1f9bdf0ebd8bd28f4f813fa44866514745c2a05"},"cell_type":"code","source":"","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}