{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30747,"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-07-17T15:34:17.770233Z","iopub.execute_input":"2024-07-17T15:34:17.770585Z","iopub.status.idle":"2024-07-17T15:34:17.779164Z","shell.execute_reply.started":"2024-07-17T15:34:17.770557Z","shell.execute_reply":"2024-07-17T15:34:17.778112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:34:22.493644Z","iopub.execute_input":"2024-07-17T15:34:22.494451Z","iopub.status.idle":"2024-07-17T15:34:22.498290Z","shell.execute_reply.started":"2024-07-17T15:34:22.494419Z","shell.execute_reply":"2024-07-17T15:34:22.497365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:34:22.937199Z","iopub.execute_input":"2024-07-17T15:34:22.937527Z","iopub.status.idle":"2024-07-17T15:34:25.471046Z","shell.execute_reply.started":"2024-07-17T15:34:22.937500Z","shell.execute_reply":"2024-07-17T15:34:25.469947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=train_data","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:34:25.472864Z","iopub.execute_input":"2024-07-17T15:34:25.473192Z","iopub.status.idle":"2024-07-17T15:34:25.478020Z","shell.execute_reply.started":"2024-07-17T15:34:25.473166Z","shell.execute_reply":"2024-07-17T15:34:25.476974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop('label', axis=1).values\ny = data['label'].values\n\n# Normalize the pixel values\nX = X / 255.0\nX = X.reshape(-1, 28, 28, 1)\n\n# Split the data","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:35:55.655174Z","iopub.execute_input":"2024-07-17T15:35:55.655554Z","iopub.status.idle":"2024-07-17T15:35:55.834296Z","shell.execute_reply.started":"2024-07-17T15:35:55.655520Z","shell.execute_reply":"2024-07-17T15:35:55.833212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom kerastuner import HyperModel\n\nclass CNNHyperModel(HyperModel):\n    def build(self, hp):\n        model = Sequential()\n        model.add(tf.keras.layers.Conv2D(\n            filters=hp.Int('conv_1_filters', min_value=16, max_value=128, step=16),\n            kernel_size=(3, 3),\n            input_shape=(28, 28, 1),\n            activation='relu'\n        ))\n        model.add(tf.keras.layers.MaxPooling2D((2, 2)))\n        model.add(tf.keras.layers.Conv2D(\n            filters=hp.Int('conv_2_filters', min_value=16, max_value=128, step=16),\n            kernel_size=(3, 3),\n            activation='relu'\n        ))\n        model.add(tf.keras.layers.MaxPooling2D((2, 2)))\n        model.add(tf.keras.layers.Flatten())\n        model.add(tf.keras.layers.Dense(\n            units=hp.Int('dense_1_units', min_value=64, max_value=256, step=64),\n            activation='relu'\n        ))\n        model.add(tf.keras.layers.Dropout(hp.Float('dropout_1', 0.2, 0.5, step=0.1)))\n        model.add(tf.keras.layers.Dense(\n            units=hp.Int('dense_2_units', min_value=32, max_value=128, step=32),\n            activation='relu'\n        ))\n        model.add(tf.keras.layers.Dense(10, activation='softmax'))\n        \n        model.compile(optimizer='adam',\n                      loss='sparse_categorical_crossentropy',\n                      metrics=['accuracy'])\n        return model\n","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:35:56.250314Z","iopub.execute_input":"2024-07-17T15:35:56.250968Z","iopub.status.idle":"2024-07-17T15:35:56.261652Z","shell.execute_reply.started":"2024-07-17T15:35:56.250934Z","shell.execute_reply":"2024-07-17T15:35:56.260741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kerastuner.tuners import RandomSearch\n\ntuner = RandomSearch(\n    CNNHyperModel(),\n    objective='val_accuracy',\n    max_trials=10,  # Number of different hyperparameter combinations to try\n    executions_per_trial=1,  # Number of models to train with each combination\n    directory='my_dir',  # Directory to save the search results\n    project_name='cnn_tuning'  # Name of the project\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:35:57.579264Z","iopub.execute_input":"2024-07-17T15:35:57.579618Z","iopub.status.idle":"2024-07-17T15:35:57.594995Z","shell.execute_reply.started":"2024-07-17T15:35:57.579592Z","shell.execute_reply":"2024-07-17T15:35:57.594140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tuner.search(X,y,epochs=20,validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:36:08.737077Z","iopub.execute_input":"2024-07-17T15:36:08.737445Z","iopub.status.idle":"2024-07-17T15:41:55.011658Z","shell.execute_reply.started":"2024-07-17T15:36:08.737416Z","shell.execute_reply":"2024-07-17T15:41:55.010788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = tuner.get_best_models(num_models=1)[0]\nbest_model.save('best_model.h5')\n","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:42:21.834938Z","iopub.execute_input":"2024-07-17T15:42:21.835970Z","iopub.status.idle":"2024-07-17T15:42:22.917289Z","shell.execute_reply.started":"2024-07-17T15:42:21.835923Z","shell.execute_reply":"2024-07-17T15:42:22.916364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-07-17T13:27:09.663827Z","iopub.execute_input":"2024-07-17T13:27:09.664670Z","iopub.status.idle":"2024-07-17T13:27:09.672822Z","shell.execute_reply.started":"2024-07-17T13:27:09.664635Z","shell.execute_reply":"2024-07-17T13:27:09.672071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.fit(X, y, epochs=20, validation_split=(0.2))","metadata":{"execution":{"iopub.status.busy":"2024-07-17T13:29:49.690210Z","iopub.execute_input":"2024-07-17T13:29:49.690593Z","iopub.status.idle":"2024-07-17T13:30:45.334535Z","shell.execute_reply.started":"2024-07-17T13:29:49.690562Z","shell.execute_reply":"2024-07-17T13:30:45.333605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m=tf.keras.models.load_model('/kaggle/working/best_model.h5')\nxtest=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Preprocess test data\nxtest = xtest.values\nxtest = xtest.reshape(-1, 28, 28, 1)\nxtest = xtest / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:42:25.337849Z","iopub.execute_input":"2024-07-17T15:42:25.338337Z","iopub.status.idle":"2024-07-17T15:42:27.184509Z","shell.execute_reply.started":"2024-07-17T15:42:25.338302Z","shell.execute_reply":"2024-07-17T15:42:27.183632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = m.predict(xtest)\npredicted_labels = predictions.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:42:28.283966Z","iopub.execute_input":"2024-07-17T15:42:28.284351Z","iopub.status.idle":"2024-07-17T15:42:30.460017Z","shell.execute_reply.started":"2024-07-17T15:42:28.284323Z","shell.execute_reply":"2024-07-17T15:42:30.458921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a DataFrame for submission\nsubmission = pd.DataFrame({\n   'ImageId': range(1, len(predicted_labels) + 1),\n   'Label': predicted_labels\n})\n\n# Save to CSV file\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T15:42:31.339194Z","iopub.execute_input":"2024-07-17T15:42:31.339888Z","iopub.status.idle":"2024-07-17T15:42:31.380957Z","shell.execute_reply.started":"2024-07-17T15:42:31.339856Z","shell.execute_reply":"2024-07-17T15:42:31.380131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.save('Mnist.keras')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T13:29:25.460977Z","iopub.execute_input":"2024-07-17T13:29:25.461750Z","iopub.status.idle":"2024-07-17T13:29:25.507255Z","shell.execute_reply.started":"2024-07-17T13:29:25.461714Z","shell.execute_reply":"2024-07-17T13:29:25.506426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}