{"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":"# 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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-03T13:09:06.118945Z","iopub.execute_input":"2022-06-03T13:09:06.119508Z","iopub.status.idle":"2022-06-03T13:09:06.147272Z","shell.execute_reply.started":"2022-06-03T13:09:06.119418Z","shell.execute_reply":"2022-06-03T13:09:06.146398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom tensorflow.keras import callbacks, layers","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:09:06.169788Z","iopub.execute_input":"2022-06-03T13:09:06.170015Z","iopub.status.idle":"2022-06-03T13:09:12.919310Z","shell.execute_reply.started":"2022-06-03T13:09:06.169971Z","shell.execute_reply":"2022-06-03T13:09:12.918470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\").sample(frac=1)\ntrain_labels = train_images.pop(\"label\")\n\ntrain_images /= 255\ntrain_images = train_images.to_numpy().astype(np.float32).reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:35:39.319787Z","iopub.execute_input":"2022-06-03T13:35:39.320075Z","iopub.status.idle":"2022-06-03T13:35:42.439765Z","shell.execute_reply.started":"2022-06-03T13:35:39.320044Z","shell.execute_reply":"2022-06-03T13:35:42.438984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val = train_images[:10_000]\ny_val = train_labels[:10_000]\n\npartial_X_train = train_images[10_000:]\npartial_y_train = train_labels[10_000:]","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:35:42.604075Z","iopub.execute_input":"2022-06-03T13:35:42.604616Z","iopub.status.idle":"2022-06-03T13:35:42.611215Z","shell.execute_reply.started":"2022-06-03T13:35:42.604564Z","shell.execute_reply":"2022-06-03T13:35:42.610236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(partial_X_train[10])","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:35:44.833765Z","iopub.execute_input":"2022-06-03T13:35:44.834355Z","iopub.status.idle":"2022-06-03T13:35:45.075057Z","shell.execute_reply.started":"2022-06-03T13:35:44.834311Z","shell.execute_reply":"2022-06-03T13:35:45.072845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    layers.Conv2D(filters=16, kernel_size=3, activation=\"relu\"),\n    layers.MaxPool2D(),\n    layers.Conv2D(filters=32, kernel_size=3, activation=\"relu\"),\n    layers.MaxPool2D(),\n    layers.Flatten(),\n    layers.Dropout(rate=0.3),\n    layers.Dense(128),\n    layers.BatchNormalization(),\n    layers.ReLU(),\n    layers.Dropout(rate=0.5),\n    layers.Dense(10, activation=\"softmax\")\n])\n\nmodel.compile(\n    optimizer=\"adam\",\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=\"accuracy\"\n)\n\nearly_stopping = callbacks.EarlyStopping(\n    min_delta=0.001,\n    patience=20,\n    restore_best_weights=True\n)\n\nhistory = model.fit(\n    partial_X_train,\n    partial_y_train,\n    validation_data=(X_val, y_val),\n    epochs=100,\n    batch_size=512,\n    callbacks=[early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:36:57.734338Z","iopub.execute_input":"2022-06-03T13:36:57.734611Z","iopub.status.idle":"2022-06-03T13:37:28.456395Z","shell.execute_reply.started":"2022-06-03T13:36:57.734582Z","shell.execute_reply":"2022-06-03T13:37:28.455633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_history = pd.DataFrame(history.history)\ndf_history.loc[:, [\"loss\", \"val_loss\"]].plot(xlabel=\"Epochs\", ylabel=\"Loss\", figsize=(10, 6))\ndf_history.loc[:, [\"accuracy\", \"val_accuracy\"]].plot(xlabel=\"Epochs\", ylabel=\"Accuracy\", figsize=(10, 6))","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:37:52.165041Z","iopub.execute_input":"2022-06-03T13:37:52.165319Z","iopub.status.idle":"2022-06-03T13:38:20.998851Z","shell.execute_reply.started":"2022-06-03T13:37:52.165283Z","shell.execute_reply":"2022-06-03T13:38:20.997898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    layers.Conv2D(filters=16, kernel_size=3, activation=\"relu\"),\n    layers.MaxPool2D(),\n    layers.Conv2D(filters=32, kernel_size=3, activation=\"relu\"),\n    layers.MaxPool2D(),\n    layers.Flatten(),\n    layers.Dropout(rate=0.3),\n    layers.Dense(128),\n    layers.BatchNormalization(),\n    layers.ReLU(),\n    layers.Dropout(rate=0.5),\n    layers.Dense(10, activation=\"softmax\")\n])\n\nmodel.compile(\n    optimizer=\"adam\",\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=\"accuracy\"\n)\n\nhistory = model.fit(\n    train_images,\n    train_labels,\n    epochs=48,\n    batch_size=512,\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:38:43.204505Z","iopub.execute_input":"2022-06-03T13:38:43.204790Z","iopub.status.idle":"2022-06-03T13:39:25.408766Z","shell.execute_reply.started":"2022-06-03T13:38:43.204751Z","shell.execute_reply":"2022-06-03T13:39:25.408040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\") / 255\ntest_images = test_images.to_numpy().astype(np.float32).reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:39:25.413655Z","iopub.execute_input":"2022-06-03T13:39:25.414244Z","iopub.status.idle":"2022-06-03T13:39:28.338859Z","shell.execute_reply.started":"2022-06-03T13:39:25.414194Z","shell.execute_reply":"2022-06-03T13:39:28.338038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:39:28.340365Z","iopub.execute_input":"2022-06-03T13:39:28.340617Z","iopub.status.idle":"2022-06-03T13:39:29.923344Z","shell.execute_reply.started":"2022-06-03T13:39:28.340584Z","shell.execute_reply":"2022-06-03T13:39:29.922523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame({\n    \"Label\": predictions.argmax(axis=1)\n}, index=range(1, 28001))\n\nresult.index.name = \"ImageId\"","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:39:29.926158Z","iopub.execute_input":"2022-06-03T13:39:29.926471Z","iopub.status.idle":"2022-06-03T13:39:29.933239Z","shell.execute_reply.started":"2022-06-03T13:39:29.926432Z","shell.execute_reply":"2022-06-03T13:39:29.932429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:39:29.963845Z","iopub.execute_input":"2022-06-03T13:39:29.964403Z","iopub.status.idle":"2022-06-03T13:39:29.977422Z","shell.execute_reply.started":"2022-06-03T13:39:29.964365Z","shell.execute_reply":"2022-06-03T13:39:29.976387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv(\"output.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:39:36.514968Z","iopub.execute_input":"2022-06-03T13:39:36.515745Z","iopub.status.idle":"2022-06-03T13:39:36.572969Z","shell.execute_reply.started":"2022-06-03T13:39:36.515703Z","shell.execute_reply":"2022-06-03T13:39:36.572145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-03T11:07:16.843835Z","iopub.execute_input":"2022-06-03T11:07:16.844282Z","iopub.status.idle":"2022-06-03T11:07:16.857784Z","shell.execute_reply.started":"2022-06-03T11:07:16.844237Z","shell.execute_reply":"2022-06-03T11:07:16.857005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}