{"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\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\nimport os\nprint(os.listdir(\"../input\"))\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_data = pd.read_csv('../input/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"146f6d36c20ff37b3b03a2033987fc6d9669f4e2"},"cell_type":"code","source":"train = train_data.drop('label',axis=1)\ntarget = train_data.label","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fbcde7f321b490ac57c8bf8aa9b1dd00b5c0c14a"},"cell_type":"markdown","source":"**Preprocessing - Getting data ready**\n* Each digit is of shape 28x28.\n* Convolutional Neural Network takes images in 3D\n* Each data has to be converted to 3D\n* -1 means, number of rows remain the same\n* 1 is added new depth, 3 for RGB"},{"metadata":{"trusted":true,"_uuid":"32a328121abd1ed3bf34f83c89130ca08a505663"},"cell_type":"code","source":"X = train.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"369ec33b85ac238191cee43bb75e3da971f42598"},"cell_type":"markdown","source":"**Importing Keras**"},{"metadata":{"trusted":true,"_uuid":"88a06c742046573ea6088d3f32e318ef9aabb532"},"cell_type":"code","source":"from keras.layers import Dense,Conv2D,MaxPooling2D, Dropout, Flatten\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8507704d33057ce6f779891dee26081ffe9fa582"},"cell_type":"markdown","source":"* Neural networks for classification needs target data in one hot vector format\n* to_categorical is a keras function to convert data to one-hot format"},{"metadata":{"trusted":true,"_uuid":"722211f94c065765ac140a5d60f201ab915b40a2"},"cell_type":"code","source":"Y = to_categorical(target)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a7887f296a5d69cbd252a9208052022ce64d5256"},"cell_type":"markdown","source":"* Using sklearn splitting data into train & test"},{"metadata":{"trusted":true,"_uuid":"16de1f1a1c7a7474596be9439cfd933140e82dec"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7a44a283b42c72a7f414c47bb152fc7d097bca42"},"cell_type":"code","source":"trainX,testX, trainY,testY = train_test_split(X, Y)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0479dd920d5c6c1b2ae581be6bc65a8f08c5622c"},"cell_type":"markdown","source":"* Neural networks works well with data in range of 0 & 1\n* Pixel value max goes to 255"},{"metadata":{"trusted":true,"_uuid":"3e96cf1acc3eb268643065e79eab17e6b1a72d49"},"cell_type":"code","source":"trainX = trainX.astype('float')/255.0\ntestX  = testX.astype('float')/255.0","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"393cbd9c43f459fa028cedbba6e2838ccfdd71bb"},"cell_type":"markdown","source":"**Designing Model**\n* "},{"metadata":{"trusted":true,"_uuid":"3484106132ae81c59e421abb2caedf79128509ba"},"cell_type":"code","source":"model = Sequential()\n#convolutional layer with rectified linear unit activation\nmodel.add(Conv2D(32, kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(28,28,1)))\n#32 convolution filters used each of size 3x3\n#again\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\n#64 convolution filters used each of size 3x3\n#choose the best features via pooling\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n#randomly turn neurons on and off to improve convergence\nmodel.add(Dropout(0.25))\n#flatten since too many dimensions, we only want a classification output\nmodel.add(Flatten())\n#fully connected to get all relevant data\nmodel.add(Dense(128, activation='relu'))\n#one more dropout for convergence' sake :) \nmodel.add(Dropout(0.5))\n#output a softmax to squash the matrix into output probabilities\nmodel.add(Dense(10, activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c8968fe5800c91fb297e0b70c5676ef9f86e30c4"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f74973df68c9207073f5796faf79912de3d60eaf"},"cell_type":"markdown","source":"**Understanding Model Parameters Count**\n1. Layer 1 ( 320)\n  * Filter dim = 3 x 3 x 1 + 1 (bias) = 10\n  * Total filters = 32\n  * Total parameters = 32 x 10 = 320\n  * Activation Maps or Depth of output layer = 32 ( Equal to number of filters )\n   \n2. Layer 2 ( 18496 )\n * Filter dim = 3 x 3 x 32 (depth of filter is equal to previous layers output's depth ) + 1 = 289\n * Total filters = 64\n * Total parameters = 64 x 289 = 18496\n *  Activation Maps or Depth of output layer = 64 ( Equal to number of filters )"},{"metadata":{"trusted":true,"_uuid":"69c0554b56962346ede5c086cd62db200837510d"},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='adam',\n              metrics=['accuracy'])\n\nbatch_size = 128\nnum_epoch = 10\n#model training\nmodel_log = model.fit(trainX, trainY,\n          batch_size=batch_size,\n          epochs=num_epoch,\n          verbose=1,\n          validation_data=(testX, testY))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8c426c1ceb3ff101d0d30adc97f080b77ef1b3f"},"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}