{"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":"2023-05-26T16:34:58.158446Z","iopub.execute_input":"2023-05-26T16:34:58.159036Z","iopub.status.idle":"2023-05-26T16:34:58.199212Z","shell.execute_reply.started":"2023-05-26T16:34:58.159000Z","shell.execute_reply":"2023-05-26T16:34:58.198227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:34:58.201053Z","iopub.execute_input":"2023-05-26T16:34:58.202318Z","iopub.status.idle":"2023-05-26T16:35:05.593969Z","shell.execute_reply.started":"2023-05-26T16:34:58.202280Z","shell.execute_reply":"2023-05-26T16:35:05.592643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom subprocess import check_output","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:05.596364Z","iopub.execute_input":"2023-05-26T16:35:05.596752Z","iopub.status.idle":"2023-05-26T16:35:17.491189Z","shell.execute_reply.started":"2023-05-26T16:35:05.596720Z","shell.execute_reply":"2023-05-26T16:35:17.489629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:17.492902Z","iopub.execute_input":"2023-05-26T16:35:17.494140Z","iopub.status.idle":"2023-05-26T16:35:17.541672Z","shell.execute_reply.started":"2023-05-26T16:35:17.494096Z","shell.execute_reply":"2023-05-26T16:35:17.540320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_data.drop(['label'], axis=1)\ny_train = train_data['label']\nx_test = test_data","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:17.544378Z","iopub.execute_input":"2023-05-26T16:35:17.544814Z","iopub.status.idle":"2023-05-26T16:35:17.681799Z","shell.execute_reply.started":"2023-05-26T16:35:17.544758Z","shell.execute_reply":"2023-05-26T16:35:17.680470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape, y_train.shape, x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:17.683309Z","iopub.execute_input":"2023-05-26T16:35:17.684597Z","iopub.status.idle":"2023-05-26T16:35:17.693225Z","shell.execute_reply.started":"2023-05-26T16:35:17.684542Z","shell.execute_reply":"2023-05-26T16:35:17.691920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.values.reshape(-1, 28, 28, 1)\nx_train = x_train/255.0\nx_test = x_test.values.reshape(-1, 28, 28, 1)\nx_test = x_test/255.0\nx_train.shape, x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:17.695107Z","iopub.execute_input":"2023-05-26T16:35:17.695563Z","iopub.status.idle":"2023-05-26T16:35:17.933447Z","shell.execute_reply.started":"2023-05-26T16:35:17.695532Z","shell.execute_reply":"2023-05-26T16:35:17.932313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = tf.keras.utils.to_categorical(y_train,10)\ny_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:17.934837Z","iopub.execute_input":"2023-05-26T16:35:17.936150Z","iopub.status.idle":"2023-05-26T16:35:17.946699Z","shell.execute_reply.started":"2023-05-26T16:35:17.936099Z","shell.execute_reply":"2023-05-26T16:35:17.945327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train[0:5,:])","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:17.948590Z","iopub.execute_input":"2023-05-26T16:35:17.948973Z","iopub.status.idle":"2023-05-26T16:35:17.964128Z","shell.execute_reply.started":"2023-05-26T16:35:17.948941Z","shell.execute_reply":"2023-05-26T16:35:17.961863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n  tf.keras.layers.Conv2D(32,(3,3),activation = 'relu', input_shape=(28,28,1)),\n  tf.keras.layers.Conv2D(32,(3,3),activation = 'relu'),\n  tf.keras.layers.MaxPooling2D(2,2),\n  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',padding = 'Same'),\n  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',padding = 'Same'),\n  tf.keras.layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)),\n  tf.keras.layers.Dropout(0.25),\n  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',padding = 'Same'),\n  tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',padding = 'Same'),\n  tf.keras.layers.MaxPooling2D(pool_size = (2,2), strides = (2,2)),\n  tf.keras.layers.Dropout(0.25),\n  tf.keras.layers.Flatten(),\n  tf.keras.layers.Dense(256, activation='relu'),\n  tf.keras.layers.Dense(256, activation='relu'),\n  tf.keras.layers.Dropout(0.50),\n  tf.keras.layers.Dense(10, activation='softmax')\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:17.966257Z","iopub.execute_input":"2023-05-26T16:35:17.966771Z","iopub.status.idle":"2023-05-26T16:35:18.393882Z","shell.execute_reply.started":"2023-05-26T16:35:17.966731Z","shell.execute_reply":"2023-05-26T16:35:18.392609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class myCallback(tf.keras.callbacks.Callback):\n        def on_epoch_end(self, epoch, logs={}):\n            if(logs.get('accuracy') > 0.999):\n                print(\"\\nReached 99.9% accuracy so cancelling training!\")\n                self.model.stop_training = True\n    \ncallbacks = myCallback()","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:18.397718Z","iopub.execute_input":"2023-05-26T16:35:18.398573Z","iopub.status.idle":"2023-05-26T16:35:18.405563Z","shell.execute_reply.started":"2023-05-26T16:35:18.398534Z","shell.execute_reply":"2023-05-26T16:35:18.404202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Optimizer = tf.keras.optimizers.Adam(\n            learning_rate=0.0005, \n            beta_1=0.9, \n            beta_2=0.999, \n            epsilon=1e-07,\n            name='Adam'\n)\nmodel.compile(optimizer=Optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(x_train, y_train, batch_size = 50, epochs = 20, callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2023-05-26T16:35:18.407506Z","iopub.execute_input":"2023-05-26T16:35:18.407911Z","iopub.status.idle":"2023-05-26T17:03:44.935994Z","shell.execute_reply.started":"2023-05-26T16:35:18.407880Z","shell.execute_reply":"2023-05-26T17:03:44.934827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(x_test)\n\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2023-05-26T17:03:44.938038Z","iopub.execute_input":"2023-05-26T17:03:44.938906Z","iopub.status.idle":"2023-05-26T17:04:01.349881Z","shell.execute_reply.started":"2023-05-26T17:03:44.938859Z","shell.execute_reply":"2023-05-26T17:04:01.348835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsubmission = 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":"2023-05-26T17:04:01.351107Z","iopub.execute_input":"2023-05-26T17:04:01.352410Z","iopub.status.idle":"2023-05-26T17:04:01.429415Z","shell.execute_reply.started":"2023-05-26T17:04:01.352374Z","shell.execute_reply":"2023-05-26T17:04:01.428327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}