{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns \n\nimport tensorflow  as tf\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Flatten , Input , Dense , Conv2D , MaxPooling2D , Dropout\n\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.layers import BatchNormalization","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-03T12:42:33.702710Z","iopub.execute_input":"2024-05-03T12:42:33.703034Z","iopub.status.idle":"2024-05-03T12:42:49.980258Z","shell.execute_reply.started":"2024-05-03T12:42:33.703007Z","shell.execute_reply":"2024-05-03T12:42:49.979281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_ds = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:42:49.982089Z","iopub.execute_input":"2024-05-03T12:42:49.982692Z","iopub.status.idle":"2024-05-03T12:42:56.108784Z","shell.execute_reply.started":"2024-05-03T12:42:49.982661Z","shell.execute_reply":"2024-05-03T12:42:56.107793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = train_ds.shape[0]\ntest_size = test_ds.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:42:56.109925Z","iopub.execute_input":"2024-05-03T12:42:56.111060Z","iopub.status.idle":"2024-05-03T12:42:56.116048Z","shell.execute_reply.started":"2024-05-03T12:42:56.111033Z","shell.execute_reply":"2024-05-03T12:42:56.115131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_ds.drop(\"label\" , axis = 1)\ny = train_ds[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:42:56.119229Z","iopub.execute_input":"2024-05-03T12:42:56.119604Z","iopub.status.idle":"2024-05-03T12:42:56.259547Z","shell.execute_reply.started":"2024-05-03T12:42:56.119572Z","shell.execute_reply":"2024-05-03T12:42:56.258350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(image , label):\n    image = tf.cast(image ,tf.float32)\n    image = tf.reshape(image , (28 ,28 , 1) )\n    \n    image = image/255.0 \n    label = to_categorical(label , num_classes=10)\n    return image , label\n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:42:56.261280Z","iopub.execute_input":"2024-05-03T12:42:56.262123Z","iopub.status.idle":"2024-05-03T12:42:56.268638Z","shell.execute_reply.started":"2024-05-03T12:42:56.262091Z","shell.execute_reply":"2024-05-03T12:42:56.267508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef prepare_dataset( images , labels , valid_ratio = 0.2, batch_size = 32 ):\n    dataset = tf.data.Dataset.from_tensor_slices((images,labels))\n    dataset = dataset.map(preprocess_image)\n    \n    valid_ = int(len(images) * valid_ratio)\n    valid_dataset = dataset.take(valid_)\n    train_dataset = dataset.skip(valid_)\n    \n    return train_dataset , valid_dataset\n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:42:56.270010Z","iopub.execute_input":"2024-05-03T12:42:56.270361Z","iopub.status.idle":"2024-05-03T12:42:56.279193Z","shell.execute_reply.started":"2024-05-03T12:42:56.270284Z","shell.execute_reply":"2024-05-03T12:42:56.278072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset , valid_dataset = prepare_dataset(X ,y)\ntrain_dataset = train_dataset.batch(32)\ntrain_dataset = train_dataset.shuffle(512 , reshuffle_each_iteration=True ).prefetch(1)\nvalid_dataset = valid_dataset.batch(32)\n    \n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:42:56.280378Z","iopub.execute_input":"2024-05-03T12:42:56.280663Z","iopub.status.idle":"2024-05-03T12:42:57.869222Z","shell.execute_reply.started":"2024-05-03T12:42:56.280639Z","shell.execute_reply":"2024-05-03T12:42:57.868192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nmodel = Sequential([\n    Input(shape = (28 , 28 , 1)) , \n    Conv2D(32 , (3 , 3 ) , activation = \"relu\" ) , \n    MaxPooling2D((2, 2)) , \n    \n    Flatten() , \n    Dense(256 , activation = \"relu\" ) ,\n    Dropout(0.2) ,\n    Dense(10 , activation =\"softmax\")\n     \n])\n\nmodel.compile(loss = \"categorical_crossentropy\" , metrics =[\"accuracy\"] , \n             optimizer = tf.keras.optimizers.RMSprop(learning_rate = 1e-3 , \n                                                    momentum = 0.9))\n\nmodel.fit(train_dataset , epochs = 20, \n         validation_data = valid_dataset)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:15:25.497939Z","iopub.execute_input":"2024-05-03T12:15:25.498246Z","iopub.status.idle":"2024-05-03T12:16:35.115170Z","shell.execute_reply.started":"2024-05-03T12:15:25.498220Z","shell.execute_reply":"2024-05-03T12:16:35.114360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss , acc = model.evaluate(valid_dataset)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T01:11:30.395396Z","iopub.execute_input":"2024-05-03T01:11:30.395752Z","iopub.status.idle":"2024-05-03T01:11:30.883761Z","shell.execute_reply.started":"2024-05-03T01:11:30.395724Z","shell.execute_reply":"2024-05-03T01:11:30.882820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(64 ,(3,3) , activation = \"relu\" ) ,\n    MaxPooling2D((2,2)) , \n    BatchNormalization() , \n    \n    Conv2D(64 ,(3,3) , activation = \"relu\" ) ,\n    MaxPooling2D((2,2)) , \n    BatchNormalization() , \n    \n    Flatten() ,\n    Dense(128 , activation = \"relu\") , \n    Dense(10 , activation=\"softmax\")\n    \n    \n])\n\nmodel.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001) ,\n             loss =\"categorical_crossentropy\" , \n             metrics= [\"accuracy\"])\n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:42:57.870369Z","iopub.execute_input":"2024-05-03T12:42:57.870648Z","iopub.status.idle":"2024-05-03T12:42:57.905172Z","shell.execute_reply.started":"2024-05-03T12:42:57.870624Z","shell.execute_reply":"2024-05-03T12:42:57.904481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset , epochs = 50 , validation_data = (valid_dataset))","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:42:57.906234Z","iopub.execute_input":"2024-05-03T12:42:57.906516Z","iopub.status.idle":"2024-05-03T12:46:25.223907Z","shell.execute_reply.started":"2024-05-03T12:42:57.906492Z","shell.execute_reply":"2024-05-03T12:46:25.223025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_on = model.predict(test_ds.values.reshape((-1 ,28 ,28,1)))\n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:46:25.227818Z","iopub.execute_input":"2024-05-03T12:46:25.228132Z","iopub.status.idle":"2024-05-03T12:46:27.924723Z","shell.execute_reply.started":"2024-05-03T12:46:25.228107Z","shell.execute_reply":"2024-05-03T12:46:27.923671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(prediction_on , axis  =1)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:46:31.852334Z","iopub.execute_input":"2024-05-03T12:46:31.852669Z","iopub.status.idle":"2024-05-03T12:46:31.857407Z","shell.execute_reply.started":"2024-05-03T12:46:31.852642Z","shell.execute_reply":"2024-05-03T12:46:31.856360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.Series(predictions , name = \"Label\")\nsubmission = pd.concat([pd.Series(range(1 , 28001) , name =\"ImageId\") ,results ] ,axis = 1)\nsubmission.to_csv(\"submission.csv\" , index = False)","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:46:33.427208Z","iopub.execute_input":"2024-05-03T12:46:33.427584Z","iopub.status.idle":"2024-05-03T12:46:33.471230Z","shell.execute_reply.started":"2024-05-03T12:46:33.427555Z","shell.execute_reply":"2024-05-03T12:46:33.470505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:46:35.811516Z","iopub.execute_input":"2024-05-03T12:46:35.811977Z","iopub.status.idle":"2024-05-03T12:46:37.219975Z","shell.execute_reply.started":"2024-05-03T12:46:35.811937Z","shell.execute_reply":"2024-05-03T12:46:37.218748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-05-03T12:18:43.088353Z","iopub.execute_input":"2024-05-03T12:18:43.088801Z","iopub.status.idle":"2024-05-03T12:18:43.097743Z","shell.execute_reply.started":"2024-05-03T12:18:43.088769Z","shell.execute_reply":"2024-05-03T12:18:43.096758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-05-03T02:43:34.372258Z","iopub.execute_input":"2024-05-03T02:43:34.372926Z","iopub.status.idle":"2024-05-03T02:43:34.392472Z","shell.execute_reply.started":"2024-05-03T02:43:34.372894Z","shell.execute_reply":"2024-05-03T02:43:34.391144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-05-03T02:07:42.772999Z","iopub.execute_input":"2024-05-03T02:07:42.773423Z","iopub.status.idle":"2024-05-03T02:07:42.779671Z","shell.execute_reply.started":"2024-05-03T02:07:42.773391Z","shell.execute_reply":"2024-05-03T02:07:42.778667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}