{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30636,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-01-21T11:37:08.945918Z","iopub.execute_input":"2024-01-21T11:37:08.946184Z","iopub.status.idle":"2024-01-21T11:37:09.898131Z","shell.execute_reply.started":"2024-01-21T11:37:08.946161Z","shell.execute_reply":"2024-01-21T11:37:09.897234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:37:40.848890Z","iopub.execute_input":"2024-01-21T11:37:40.849417Z","iopub.status.idle":"2024-01-21T11:37:44.421817Z","shell.execute_reply.started":"2024-01-21T11:37:40.849362Z","shell.execute_reply":"2024-01-21T11:37:44.420813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:37:58.064759Z","iopub.execute_input":"2024-01-21T11:37:58.065137Z","iopub.status.idle":"2024-01-21T11:38:00.106908Z","shell.execute_reply.started":"2024-01-21T11:37:58.065106Z","shell.execute_reply":"2024-01-21T11:38:00.106034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsample.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:38:19.851108Z","iopub.execute_input":"2024-01-21T11:38:19.851477Z","iopub.status.idle":"2024-01-21T11:38:19.874350Z","shell.execute_reply.started":"2024-01-21T11:38:19.851449Z","shell.execute_reply":"2024-01-21T11:38:19.873412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label=train[\"label\"]\ntrain_label","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:38:30.944364Z","iopub.execute_input":"2024-01-21T11:38:30.944774Z","iopub.status.idle":"2024-01-21T11:38:30.952323Z","shell.execute_reply.started":"2024-01-21T11:38:30.944746Z","shell.execute_reply":"2024-01-21T11:38:30.951437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=train.drop(columns='label')\ntrain.columns","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:39:25.409205Z","iopub.execute_input":"2024-01-21T11:39:25.410071Z","iopub.status.idle":"2024-01-21T11:39:25.501771Z","shell.execute_reply.started":"2024-01-21T11:39:25.410038Z","shell.execute_reply":"2024-01-21T11:39:25.500672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=train.values.reshape(train.shape[0],28,28,1)/255.0\nY_train=test.values.reshape(test.shape[0],28,28,1)/255.0","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:39:40.708975Z","iopub.execute_input":"2024-01-21T11:39:40.709715Z","iopub.status.idle":"2024-01-21T11:39:40.883684Z","shell.execute_reply.started":"2024-01-21T11:39:40.709683Z","shell.execute_reply":"2024-01-21T11:39:40.882837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers,models\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import Input,GlobalAveragePooling2D\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Flatten,Dense","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:39:56.396218Z","iopub.execute_input":"2024-01-21T11:39:56.397086Z","iopub.status.idle":"2024-01-21T11:40:07.961825Z","shell.execute_reply.started":"2024-01-21T11:39:56.397053Z","shell.execute_reply":"2024-01-21T11:40:07.960976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=models.Sequential()\nmodel.add(Conv2D(filters=64,kernel_size=3,padding='same',activation='relu',input_shape=(28,28,1)))\nmodel.add(Conv2D(filters=64,kernel_size=3,padding='same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=128,kernel_size=3,padding='same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=128,kernel_size=3,padding='same',activation='relu'))\nmodel.add(Conv2D(filters=192,kernel_size=3,padding='same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=256,kernel_size=3,padding='same',activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2,padding='same'))\nmodel.add(Flatten())\nmodel.add(Dense(units=256,activation='relu'))\nmodel.add(Dense(units=10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:40:19.711987Z","iopub.execute_input":"2024-01-21T11:40:19.712886Z","iopub.status.idle":"2024-01-21T11:40:20.752925Z","shell.execute_reply.started":"2024-01-21T11:40:19.712851Z","shell.execute_reply":"2024-01-21T11:40:20.752081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:40:28.244293Z","iopub.execute_input":"2024-01-21T11:40:28.245217Z","iopub.status.idle":"2024-01-21T11:40:28.284186Z","shell.execute_reply.started":"2024-01-21T11:40:28.245181Z","shell.execute_reply":"2024-01-21T11:40:28.283292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_encoded=to_categorical(train_label,num_classes=10)\nmodel.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])\nmodel.fit(X_train,y_train_encoded,epochs=40,validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:40:39.916541Z","iopub.execute_input":"2024-01-21T11:40:39.917263Z","iopub.status.idle":"2024-01-21T11:45:24.585721Z","shell.execute_reply.started":"2024-01-21T11:40:39.917233Z","shell.execute_reply":"2024-01-21T11:45:24.584814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=model.predict(Y_train)\npredicted_labels=np.argmax(predictions,axis=1)\nsubmission=pd.DataFrame({\"ImageId\":range(1,len(predicted_labels)+1),\"Label\":predicted_labels})\nsubmission.to_csv(\"submission.csv\",index=False)\nsubmission=pd.read_csv(\"submission.csv\")\nprint(submission)","metadata":{"execution":{"iopub.status.busy":"2024-01-21T11:48:11.666729Z","iopub.execute_input":"2024-01-21T11:48:11.667542Z","iopub.status.idle":"2024-01-21T11:48:14.186986Z","shell.execute_reply.started":"2024-01-21T11:48:11.667509Z","shell.execute_reply":"2024-01-21T11:48:14.186043Z"},"trusted":true},"execution_count":null,"outputs":[]}]}