{"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":30527,"isInternetEnabled":false,"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)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.activations import relu,sigmoid,linear\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-13T14:36:37.158607Z","iopub.execute_input":"2024-01-13T14:36:37.159578Z","iopub.status.idle":"2024-01-13T14:36:37.172018Z","shell.execute_reply.started":"2024-01-13T14:36:37.159542Z","shell.execute_reply":"2024-01-13T14:36:37.171058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndata_test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint(data_train.shape)\nprint(data_test.shape)\ndata_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:36:37.173799Z","iopub.execute_input":"2024-01-13T14:36:37.174129Z","iopub.status.idle":"2024-01-13T14:36:42.057596Z","shell.execute_reply.started":"2024-01-13T14:36:37.174093Z","shell.execute_reply":"2024-01-13T14:36:42.056537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = data_train.drop(\"label\" ,axis=1)\ny_train = data_train[\"label\"]\nprint(X_train.shape)\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:36:42.059182Z","iopub.execute_input":"2024-01-13T14:36:42.059470Z","iopub.status.idle":"2024-01-13T14:36:42.144729Z","shell.execute_reply.started":"2024-01-13T14:36:42.059445Z","shell.execute_reply":"2024-01-13T14:36:42.143666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m,n=X_train.shape\n\nfig,axes=plt.subplots(10,10,figsize=(7,7))\nfig.tight_layout(pad=0.13,rect=[0, 0.03, 1, 0.91])\nfor i,ax in enumerate(axes.flat):\n    idx=np.random.randint(m)\n    X_reshaped=X_train.values[idx].reshape(28,28)\n    ax.imshow(X_reshaped,cmap=\"gray\")\n    ax.set_title(y_train[idx])\n    ax.set_axis_off()\n    fig.suptitle(\"Label, Image\")\n","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:36:42.146106Z","iopub.execute_input":"2024-01-13T14:36:42.146408Z","iopub.status.idle":"2024-01-13T14:36:51.155486Z","shell.execute_reply.started":"2024-01-13T14:36:42.146383Z","shell.execute_reply":"2024-01-13T14:36:51.154632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.to_numpy().reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:36:51.157710Z","iopub.execute_input":"2024-01-13T14:36:51.158002Z","iopub.status.idle":"2024-01-13T14:36:51.162460Z","shell.execute_reply.started":"2024-01-13T14:36:51.157977Z","shell.execute_reply":"2024-01-13T14:36:51.161539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:36:51.163504Z","iopub.execute_input":"2024-01-13T14:36:51.163829Z","iopub.status.idle":"2024-01-13T14:36:51.174509Z","shell.execute_reply.started":"2024-01-13T14:36:51.163805Z","shell.execute_reply":"2024-01-13T14:36:51.173493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.random.set_seed(1234)\ninput_shape = (28,28,1)\nmodel = Sequential(\n    [\n        Conv2D(64, (3,3), strides=1, padding='valid', activation='relu', input_shape=input_shape),\n        BatchNormalization(),\n        MaxPooling2D((2,2), strides=1, padding='valid'),\n        Conv2D(128, (3,3), strides=1, padding='valid', activation='relu'),\n        BatchNormalization(),\n        MaxPooling2D((2,2), strides=1, padding='valid'),\n        Conv2D(256, (3,3), strides=1, padding='valid', activation='relu'),\n        BatchNormalization(),\n        MaxPooling2D((2,2), strides=1, padding='valid'),\n        Dropout(0.2),\n        Dense(256, activation='relu'),\n        Flatten(),\n        Dense(10, activation='softmax')\n    ] , name=\"model1\"\n)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:36:51.175862Z","iopub.execute_input":"2024-01-13T14:36:51.176297Z","iopub.status.idle":"2024-01-13T14:36:51.397221Z","shell.execute_reply.started":"2024-01-13T14:36:51.176264Z","shell.execute_reply":"2024-01-13T14:36:51.395214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss= tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer= 'adam',\n    metrics= ['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:36:51.398395Z","iopub.execute_input":"2024-01-13T14:36:51.398689Z","iopub.status.idle":"2024-01-13T14:36:51.414941Z","shell.execute_reply.started":"2024-01-13T14:36:51.398658Z","shell.execute_reply":"2024-01-13T14:36:51.414046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(\n    X_train, y_train, epochs=30\n)","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:36:51.416268Z","iopub.execute_input":"2024-01-13T14:36:51.416547Z","iopub.status.idle":"2024-01-13T14:44:15.182298Z","shell.execute_reply.started":"2024-01-13T14:36:51.416523Z","shell.execute_reply":"2024-01-13T14:44:15.181378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nplt.plot(history.history[\"loss\"],label=\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:44:15.183728Z","iopub.execute_input":"2024-01-13T14:44:15.184027Z","iopub.status.idle":"2024-01-13T14:44:15.390208Z","shell.execute_reply.started":"2024-01-13T14:44:15.184001Z","shell.execute_reply":"2024-01-13T14:44:15.389298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = data_test.to_numpy().reshape(-1,28,28,1)\ny_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:44:15.392854Z","iopub.execute_input":"2024-01-13T14:44:15.393165Z","iopub.status.idle":"2024-01-13T14:44:21.036860Z","shell.execute_reply.started":"2024-01-13T14:44:15.393138Z","shell.execute_reply":"2024-01-13T14:44:21.036005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission['Label'] = y_pred.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:44:21.038166Z","iopub.execute_input":"2024-01-13T14:44:21.038534Z","iopub.status.idle":"2024-01-13T14:44:21.066273Z","shell.execute_reply.started":"2024-01-13T14:44:21.038500Z","shell.execute_reply":"2024-01-13T14:44:21.065169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-13T14:44:21.067499Z","iopub.execute_input":"2024-01-13T14:44:21.067849Z","iopub.status.idle":"2024-01-13T14:44:21.127653Z","shell.execute_reply.started":"2024-01-13T14:44:21.067820Z","shell.execute_reply":"2024-01-13T14:44:21.126860Z"},"trusted":true},"execution_count":null,"outputs":[]}]}