{"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":"markdown","source":"## Loading the dataset","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nimport tensorflow as tf\nimport pandas as pd\nimport numpy as np","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-08-31T02:11:50.950457Z","iopub.execute_input":"2022-08-31T02:11:50.950980Z","iopub.status.idle":"2022-08-31T02:11:55.564041Z","shell.execute_reply.started":"2022-08-31T02:11:50.950938Z","shell.execute_reply":"2022-08-31T02:11:55.563046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()","metadata":{"collapsed":false,"pycharm":{"name":"#%%\n"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-31T02:11:55.565832Z","iopub.execute_input":"2022-08-31T02:11:55.566502Z","iopub.status.idle":"2022-08-31T02:11:56.956616Z","shell.execute_reply.started":"2022-08-31T02:11:55.566462Z","shell.execute_reply":"2022-08-31T02:11:56.955658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing the dataset","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"train_images = train_images.astype('float32') / 255\ntest_images = test_images.astype('float32') / 255\n\ntrain_images = train_images.reshape((60000, 28, 28, 1))\ntest_images = test_images.reshape((10000, 28, 28, 1))","metadata":{"collapsed":false,"pycharm":{"name":"#%%\n"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-31T02:11:56.957851Z","iopub.execute_input":"2022-08-31T02:11:56.958226Z","iopub.status.idle":"2022-08-31T02:11:57.044100Z","shell.execute_reply.started":"2022-08-31T02:11:56.958190Z","shell.execute_reply":"2022-08-31T02:11:57.043003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Defining the architecture of the model","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1), name='1st_Conv2D'),\n    tf.keras.layers.MaxPooling2D((2, 2), name='1st_MaxPooling2D'),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu', name='2nd_Conv2D'),\n    tf.keras.layers.MaxPooling2D((2, 2), name='2nd_MaxPooling2D'),\n    tf.keras.layers.Conv2D(64, (3, 3), activation='relu', name='3rd_Conv2D'),\n    tf.keras.layers.Flatten(name='Flatten'),\n    tf.keras.layers.Dense(784, activation='relu', name='1st_Dense'),\n    tf.keras.layers.Dropout(rate=0.5),\n    tf.keras.layers.Dense(10, activation='softmax', name='OutputLayer'),\n])\nmodel.summary()","metadata":{"collapsed":false,"pycharm":{"name":"#%%\n"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-31T02:11:57.047570Z","iopub.execute_input":"2022-08-31T02:11:57.048214Z","iopub.status.idle":"2022-08-31T02:12:00.226551Z","shell.execute_reply.started":"2022-08-31T02:11:57.048172Z","shell.execute_reply":"2022-08-31T02:12:00.225552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nhistory = model.fit(train_images, train_labels, epochs=5, batch_size=64, validation_split=0.2)","metadata":{"collapsed":false,"pycharm":{"name":"#%%\n"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-31T02:12:00.227982Z","iopub.execute_input":"2022-08-31T02:12:00.228988Z","iopub.status.idle":"2022-08-31T02:12:21.557530Z","shell.execute_reply.started":"2022-08-31T02:12:00.228947Z","shell.execute_reply":"2022-08-31T02:12:21.556606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'])\nplt.show()","metadata":{"collapsed":false,"pycharm":{"name":"#%%\n"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-31T02:12:21.560450Z","iopub.execute_input":"2022-08-31T02:12:21.560750Z","iopub.status.idle":"2022-08-31T02:12:21.808995Z","shell.execute_reply.started":"2022-08-31T02:12:21.560723Z","shell.execute_reply":"2022-08-31T02:12:21.808080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_images, test_labels)[-1]","metadata":{"collapsed":false,"pycharm":{"name":"#%%\n"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-08-31T02:12:21.810323Z","iopub.execute_input":"2022-08-31T02:12:21.811082Z","iopub.status.idle":"2022-08-31T02:12:22.596437Z","shell.execute_reply.started":"2022-08-31T02:12:21.811020Z","shell.execute_reply":"2022-08-31T02:12:22.595552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making predictions on the test set","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv('../input/digit-recognizer/test.csv')\ntest_data = test_data.to_numpy()\ntest_data = test_data.reshape(28000, 28, 28, 1)\ntest_data = test_data.astype('float32') / 256","metadata":{"execution":{"iopub.status.busy":"2022-08-31T02:12:22.599141Z","iopub.execute_input":"2022-08-31T02:12:22.599442Z","iopub.status.idle":"2022-08-31T02:12:24.477326Z","shell.execute_reply.started":"2022-08-31T02:12:22.599415Z","shell.execute_reply":"2022-08-31T02:12:24.476315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(model.predict(test_data), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T02:12:24.478885Z","iopub.execute_input":"2022-08-31T02:12:24.479288Z","iopub.status.idle":"2022-08-31T02:12:26.053584Z","shell.execute_reply.started":"2022-08-31T02:12:24.479247Z","shell.execute_reply":"2022-08-31T02:12:26.052571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({\n    'ImageId': np.array(range(1, predictions.shape[0] + 1)),\n    'Label': predictions.flatten()\n}).to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T02:12:26.056474Z","iopub.execute_input":"2022-08-31T02:12:26.057519Z","iopub.status.idle":"2022-08-31T02:12:26.094054Z","shell.execute_reply.started":"2022-08-31T02:12:26.057479Z","shell.execute_reply":"2022-08-31T02:12:26.093110Z"},"trusted":true},"execution_count":null,"outputs":[]}]}