{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# necessary imports \nimport numpy as np\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2022-08-06T05:56:32.789906Z","iopub.execute_input":"2022-08-06T05:56:32.790392Z","iopub.status.idle":"2022-08-06T05:56:38.495061Z","shell.execute_reply.started":"2022-08-06T05:56:32.790291Z","shell.execute_reply":"2022-08-06T05:56:38.493791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model / data parameters\n# Number of classes are 10 from 0-9. Input shape is set to be a 28*28 matrix with just one layer to hold. \nnum_classes = 10\ninput_shape = (28, 28, 1)\n\n# Load the data and split it between train and test sets\n(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n\n# Scale images to the [0, 1] range\nx_train = x_train.astype(\"float32\") / 255\nx_test = x_test.astype(\"float32\") / 255\n# Make sure images have shape (28, 28, 1)\nx_train = np.expand_dims(x_train, -1)\nx_test = np.expand_dims(x_test, -1)\nprint(\"x_train shape:\", x_train.shape)\nprint(x_train.shape[0], \"train samples\")\nprint(x_test.shape[0], \"test samples\")\n\n\n# convert class vectors to binary class matrices\ny_train = keras.utils.to_categorical(y_train, num_classes)\ny_test = keras.utils.to_categorical(y_test, num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T05:59:06.478482Z","iopub.execute_input":"2022-08-06T05:59:06.479340Z","iopub.status.idle":"2022-08-06T05:59:07.273239Z","shell.execute_reply.started":"2022-08-06T05:59:06.479303Z","shell.execute_reply":"2022-08-06T05:59:07.272161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets say if we do not load data directly and plan to load the data as provided to us in columns format then we \n# may need to follow the following approach\nimport pandas as pd \nfrom keras.utils.np_utils import to_categorical\n\ndf_train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ndf_test = pd.read_csv(\"../input/digit-recognizer/test.csv\")\n\nX_train = df_train.drop(\"label\", axis = 1)\nY_train = df_train[\"label\"]\n\nX_test = df_test\n\nX_train = X_train.values.reshape(-1,28,28,1)\nX_test = X_test.values.reshape(-1,28,28,1)\n\nX_train = X_train/255\nX_test = X_test/255\n\nY_train = to_categorical(Y_train, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:14:56.697964Z","iopub.execute_input":"2022-08-06T06:14:56.698388Z","iopub.status.idle":"2022-08-06T06:15:00.594830Z","shell.execute_reply.started":"2022-08-06T06:14:56.698356Z","shell.execute_reply":"2022-08-06T06:15:00.593540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of training dataset{}\".format(X_train.shape))\nprint(\"Shape of test dataset{}\".format(X_test.shape))\nprint(\"Shape of Y train dataset{}\".format(Y_train.shape))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:16:35.723988Z","iopub.execute_input":"2022-08-06T06:16:35.724556Z","iopub.status.idle":"2022-08-06T06:16:35.730294Z","shell.execute_reply.started":"2022-08-06T06:16:35.724517Z","shell.execute_reply":"2022-08-06T06:16:35.729237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of training dataset{}\".format(x_train.shape))\nprint(\"Shape of test dataset{}\".format(x_test.shape))\nprint(\"Shape of Y train dataset{}\".format(y_train.shape))","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:17:00.985909Z","iopub.execute_input":"2022-08-06T06:17:00.986595Z","iopub.status.idle":"2022-08-06T06:17:00.992983Z","shell.execute_reply.started":"2022-08-06T06:17:00.986556Z","shell.execute_reply":"2022-08-06T06:17:00.991633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Max Pooling : It reduces the image size by taking the average across the matrix for 2*2 matrix. Please do refer the link : https://paperswithcode.com/method/max-pooling#:~:text=Max%20Pooling%20is%20a%20pooling,used%20after%20a%20convolutional%20layer.\n\nFlatten : Flatten the entire 2D matrix in a long 1D array. ","metadata":{}},{"cell_type":"code","source":"# Simple Covnet Model \ninput_shape = (28, 28, 1)\nmodel = keras.Sequential(\n    [\n        keras.Input(shape=input_shape),\n        layers.Conv2D(32, kernel_size=(3, 3), activation=\"relu\"),\n        layers.MaxPooling2D(pool_size=(2, 2)),\n        layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\"),\n        layers.MaxPooling2D(pool_size=(2, 2)),\n        layers.Flatten(),\n        layers.Dropout(0.5),\n        layers.Dense(num_classes, activation=\"softmax\"),\n    ]\n)\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:21:08.478383Z","iopub.execute_input":"2022-08-06T06:21:08.478764Z","iopub.status.idle":"2022-08-06T06:21:08.525952Z","shell.execute_reply.started":"2022-08-06T06:21:08.478732Z","shell.execute_reply":"2022-08-06T06:21:08.524823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\nepochs = 15\n\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n\nmodel.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:22:00.864284Z","iopub.execute_input":"2022-08-06T06:22:00.864659Z","iopub.status.idle":"2022-08-06T06:22:33.439791Z","shell.execute_reply.started":"2022-08-06T06:22:00.864624Z","shell.execute_reply":"2022-08-06T06:22:33.438901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(x_test, y_test, verbose=0)\nprint(\"Test loss:\", score[0])\nprint(\"Test accuracy:\", score[1])","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:22:38.103542Z","iopub.execute_input":"2022-08-06T06:22:38.103921Z","iopub.status.idle":"2022-08-06T06:22:38.819766Z","shell.execute_reply.started":"2022-08-06T06:22:38.103865Z","shell.execute_reply":"2022-08-06T06:22:38.818805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:23:37.295801Z","iopub.execute_input":"2022-08-06T06:23:37.296459Z","iopub.status.idle":"2022-08-06T06:23:55.093843Z","shell.execute_reply.started":"2022-08-06T06:23:37.296419Z","shell.execute_reply":"2022-08-06T06:23:55.092928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nplt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:24:43.691644Z","iopub.execute_input":"2022-08-06T06:24:43.692063Z","iopub.status.idle":"2022-08-06T06:24:43.924655Z","shell.execute_reply.started":"2022-08-06T06:24:43.692028Z","shell.execute_reply":"2022-08-06T06:24:43.923774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='loss')\nplt.plot(history.history['val_loss'], label = 'val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:25:01.788955Z","iopub.execute_input":"2022-08-06T06:25:01.789867Z","iopub.status.idle":"2022-08-06T06:25:01.994964Z","shell.execute_reply.started":"2022-08-06T06:25:01.789815Z","shell.execute_reply":"2022-08-06T06:25:01.993865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loss, train_accuracy = model.evaluate(X_train, Y_train)\nprint('Train loss: ', train_loss)\nprint('Train accuracy: ', train_accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:37:47.859790Z","iopub.execute_input":"2022-08-06T06:37:47.860473Z","iopub.status.idle":"2022-08-06T06:37:51.380846Z","shell.execute_reply.started":"2022-08-06T06:37:47.860435Z","shell.execute_reply":"2022-08-06T06:37:51.379750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict results\nresults = model.predict(X_test)\n\n# select the index with the maximum probability\nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:38:25.448582Z","iopub.execute_input":"2022-08-06T06:38:25.448986Z","iopub.status.idle":"2022-08-06T06:38:26.966003Z","shell.execute_reply.started":"2022-08-06T06:38:25.448949Z","shell.execute_reply":"2022-08-06T06:38:26.964937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T06:38:45.026981Z","iopub.execute_input":"2022-08-06T06:38:45.027809Z","iopub.status.idle":"2022-08-06T06:38:45.062817Z","shell.execute_reply.started":"2022-08-06T06:38:45.027766Z","shell.execute_reply":"2022-08-06T06:38:45.061917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}