{"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-03-18T21:35:44.704477Z","iopub.execute_input":"2022-03-18T21:35:44.704736Z","iopub.status.idle":"2022-03-18T21:35:44.714336Z","shell.execute_reply.started":"2022-03-18T21:35:44.704707Z","shell.execute_reply":"2022-03-18T21:35:44.713608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:35:45.711104Z","iopub.execute_input":"2022-03-18T21:35:45.711850Z","iopub.status.idle":"2022-03-18T21:35:45.716220Z","shell.execute_reply.started":"2022-03-18T21:35:45.711786Z","shell.execute_reply":"2022-03-18T21:35:45.714939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntrain_x=train.drop(\"label\",axis=1)\ntrain_y=train[\"label\"]\ntrain_x.shape,test.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:38:11.851062Z","iopub.execute_input":"2022-03-18T21:38:11.851331Z","iopub.status.idle":"2022-03-18T21:38:15.482561Z","shell.execute_reply.started":"2022-03-18T21:38:11.851301Z","shell.execute_reply":"2022-03-18T21:38:15.481694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x=tf.reshape(train_x,(42000,28,28,1))\ntest=tf.reshape(test,(28000,28,28,1))\ntrain_x.shape,test.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:40:41.139781Z","iopub.execute_input":"2022-03-18T21:40:41.140107Z","iopub.status.idle":"2022-03-18T21:40:41.147180Z","shell.execute_reply.started":"2022-03-18T21:40:41.140076Z","shell.execute_reply":"2022-03-18T21:40:41.146439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"#normalize data\nnorm_train_x=train_x/255\nnorm_test=test/255\n","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:40:51.498606Z","iopub.execute_input":"2022-03-18T21:40:51.498908Z","iopub.status.idle":"2022-03-18T21:40:51.503773Z","shell.execute_reply.started":"2022-03-18T21:40:51.498876Z","shell.execute_reply":"2022-03-18T21:40:51.503107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_y=pd.get_dummies(train_y)\nnew_train_y.head,new_train_y.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:35:59.303453Z","iopub.execute_input":"2022-03-18T21:35:59.303764Z","iopub.status.idle":"2022-03-18T21:35:59.328475Z","shell.execute_reply.started":"2022-03-18T21:35:59.303729Z","shell.execute_reply":"2022-03-18T21:35:59.327853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class mycallback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self,epochs,logs={}):\n        if(logs.get('loss')<0.006):\n            print(\"\\n reached loss=0.006, hence stopped training!!\")\n            self.model.stop_training=True\n            \n            \ncallbacks=mycallback()\n            \n        \n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:36:02.089862Z","iopub.execute_input":"2022-03-18T21:36:02.090651Z","iopub.status.idle":"2022-03-18T21:36:02.098112Z","shell.execute_reply.started":"2022-03-18T21:36:02.090614Z","shell.execute_reply":"2022-03-18T21:36:02.097116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.random.set_seed(42)\n\nmodel_6=tf.keras.Sequential([\n    tf.keras.layers.Conv2D(64,(3,3),activation='relu'),\n    tf.keras.layers.MaxPool2D((2,2)),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(150,activation='relu'),\n    tf.keras.layers.Dense(10,activation='softmax')\n])\n\nmodel_6.compile(loss=tf.keras.losses.CategoricalCrossentropy(),\n             optimizer=tf.keras.optimizers.Adam(),\n             metrics=['accuracy'])\n\nhistory_6=model_6.fit(norm_train_x,new_train_y,epochs=50,callbacks=[callbacks])\n","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:36:14.495665Z","iopub.execute_input":"2022-03-18T21:36:14.495934Z","iopub.status.idle":"2022-03-18T21:36:45.425346Z","shell.execute_reply.started":"2022-03-18T21:36:14.495905Z","shell.execute_reply":"2022-03-18T21:36:45.424619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_6.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:36:45.428000Z","iopub.execute_input":"2022-03-18T21:36:45.428539Z","iopub.status.idle":"2022-03-18T21:36:45.437197Z","shell.execute_reply.started":"2022-03-18T21:36:45.428510Z","shell.execute_reply":"2022-03-18T21:36:45.436431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model_6)","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:36:45.438442Z","iopub.execute_input":"2022-03-18T21:36:45.438720Z","iopub.status.idle":"2022-03-18T21:36:45.607153Z","shell.execute_reply.started":"2022-03-18T21:36:45.438684Z","shell.execute_reply":"2022-03-18T21:36:45.606324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history_6.history).plot()\nplt.ylabel(\"loss\")\nplt.xlabel(\"epochs\");","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:36:45.609576Z","iopub.execute_input":"2022-03-18T21:36:45.609876Z","iopub.status.idle":"2022-03-18T21:36:45.833271Z","shell.execute_reply.started":"2022-03-18T21:36:45.609837Z","shell.execute_reply":"2022-03-18T21:36:45.832596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p=tf.squeeze((model_6.predict(norm_test)))\n\np\n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:41:14.004076Z","iopub.execute_input":"2022-03-18T21:41:14.004336Z","iopub.status.idle":"2022-03-18T21:41:15.465466Z","shell.execute_reply.started":"2022-03-18T21:41:14.004307Z","shell.execute_reply":"2022-03-18T21:41:15.464734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l=[]\nfor i in p:\n    l.append(np.argmax(i))\n\nprediction=tf.constant(l)\nprediction\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-18T21:41:43.097579Z","iopub.execute_input":"2022-03-18T21:41:43.098299Z","iopub.status.idle":"2022-03-18T21:41:52.976998Z","shell.execute_reply.started":"2022-03-18T21:41:43.098263Z","shell.execute_reply":"2022-03-18T21:41:52.976304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': list(range(1,len(prediction)+1)), 'Label': prediction})\noutput.to_csv('submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{},"execution_count":null,"outputs":[]}]}