{"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":"import 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":"2023-07-31T18:20:48.068399Z","iopub.execute_input":"2023-07-31T18:20:48.068959Z","iopub.status.idle":"2023-07-31T18:20:48.077664Z","shell.execute_reply.started":"2023-07-31T18:20:48.068931Z","shell.execute_reply":"2023-07-31T18:20:48.076077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import Libriries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nimport keras\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.activations import linear, relu, sigmoid\nfrom sklearn.model_selection import  train_test_split\nfrom keras.datasets import mnist","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:20:48.079789Z","iopub.execute_input":"2023-07-31T18:20:48.080114Z","iopub.status.idle":"2023-07-31T18:20:58.755085Z","shell.execute_reply.started":"2023-07-31T18:20:48.080089Z","shell.execute_reply":"2023-07-31T18:20:58.754340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Explore data","metadata":{}},{"cell_type":"code","source":"# kaggle mnist data \ntrain_df =  pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(train_df.shape, test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:20:58.756388Z","iopub.execute_input":"2023-07-31T18:20:58.757376Z","iopub.status.idle":"2023-07-31T18:21:02.976517Z","shell.execute_reply.started":"2023-07-31T18:20:58.757331Z","shell.execute_reply":"2023-07-31T18:21:02.975246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# keras mnist data \n(x_train1, y_train1), (x_test1, y_test1) = mnist.load_data()\n\nx_train1 = np.concatenate([x_train1, x_test1], axis=0)\ny_train1 = np.concatenate([y_train1, y_test1], axis=0)\n\nprint(x_train1.shape, y_train1.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:02.979029Z","iopub.execute_input":"2023-07-31T18:21:02.979390Z","iopub.status.idle":"2023-07-31T18:21:04.754577Z","shell.execute_reply.started":"2023-07-31T18:21:02.979362Z","shell.execute_reply":"2023-07-31T18:21:04.753633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train2 = x_train1.reshape(70000, 784)\nprint(x_train2.shape, y_train1.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:04.755768Z","iopub.execute_input":"2023-07-31T18:21:04.757559Z","iopub.status.idle":"2023-07-31T18:21:04.763033Z","shell.execute_reply.started":"2023-07-31T18:21:04.757526Z","shell.execute_reply":"2023-07-31T18:21:04.761936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print ('The shape of X is: ' + str(train_df.shape))\nprint ('The shape of y is: ' + str(test_df.shape))","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:04.764308Z","iopub.execute_input":"2023-07-31T18:21:04.764635Z","iopub.status.idle":"2023-07-31T18:21:04.782131Z","shell.execute_reply.started":"2023-07-31T18:21:04.764608Z","shell.execute_reply":"2023-07-31T18:21:04.781097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:04.783366Z","iopub.execute_input":"2023-07-31T18:21:04.784100Z","iopub.status.idle":"2023-07-31T18:21:04.832460Z","shell.execute_reply.started":"2023-07-31T18:21:04.784076Z","shell.execute_reply":"2023-07-31T18:21:04.831353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:04.833690Z","iopub.execute_input":"2023-07-31T18:21:04.834475Z","iopub.status.idle":"2023-07-31T18:21:04.852726Z","shell.execute_reply.started":"2023-07-31T18:21:04.834446Z","shell.execute_reply":"2023-07-31T18:21:04.851004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = np.asarray(train_df['label'])\ny[:5]\nprint(y.shape, y_train1.shape)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:04.853969Z","iopub.execute_input":"2023-07-31T18:21:04.854258Z","iopub.status.idle":"2023-07-31T18:21:04.860508Z","shell.execute_reply.started":"2023-07-31T18:21:04.854234Z","shell.execute_reply":"2023-07-31T18:21:04.859556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.asarray(train_df)\nX = X[:,1:]\nnp.shape(X)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:04.863819Z","iopub.execute_input":"2023-07-31T18:21:04.864362Z","iopub.status.idle":"2023-07-31T18:21:04.875830Z","shell.execute_reply.started":"2023-07-31T18:21:04.864321Z","shell.execute_reply":"2023-07-31T18:21:04.874612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.concatenate((X, x_train2))\ny_train = np.concatenate((y, y_train1))","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:04.877386Z","iopub.execute_input":"2023-07-31T18:21:04.878173Z","iopub.status.idle":"2023-07-31T18:21:05.038513Z","shell.execute_reply.started":"2023-07-31T18:21:04.878144Z","shell.execute_reply":"2023-07-31T18:21:05.036712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, np.max(X_train), y_train.shape, np.max(y_train))","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:05.040264Z","iopub.execute_input":"2023-07-31T18:21:05.040619Z","iopub.status.idle":"2023-07-31T18:21:05.084323Z","shell.execute_reply.started":"2023-07-31T18:21:05.040591Z","shell.execute_reply":"2023-07-31T18:21:05.082993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape each row back into a 28x28 image\nimage_shape = (28, 28)\nfirst_5_images = X_train[:10].reshape(-1, *image_shape)\n\n# Create a 2x5 grid for displaying the images\nfig, axes = plt.subplots(1, 10, figsize=(10, 2))\n\n# Plot each image along with its label\nfor i, (image, label) in enumerate(zip(first_5_images, y_train[:10])):\n    ax = axes[i]\n    ax.imshow(image, cmap='gray')\n    ax.set_title(f\"{label}\")\n    ax.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:05.085730Z","iopub.execute_input":"2023-07-31T18:21:05.086029Z","iopub.status.idle":"2023-07-31T18:21:05.590807Z","shell.execute_reply.started":"2023-07-31T18:21:05.086004Z","shell.execute_reply":"2023-07-31T18:21:05.589667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocess the data","metadata":{}},{"cell_type":"code","source":"X_train = X_train/255.0\nprint(X_train.max())","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:05.592314Z","iopub.execute_input":"2023-07-31T18:21:05.592919Z","iopub.status.idle":"2023-07-31T18:21:05.857046Z","shell.execute_reply.started":"2023-07-31T18:21:05.592884Z","shell.execute_reply":"2023-07-31T18:21:05.856185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = tf.keras.utils.to_categorical(y_train, 10)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:05.858277Z","iopub.execute_input":"2023-07-31T18:21:05.858968Z","iopub.status.idle":"2023-07-31T18:21:05.867392Z","shell.execute_reply.started":"2023-07-31T18:21:05.858916Z","shell.execute_reply":"2023-07-31T18:21:05.866052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split the datasets\n(X_train, X_val, y_train, y_val) = train_test_split(X_train, y_train, test_size = 0.15)\nprint(X_train.shape, X_val.shape, y_train.shape, y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:05.869585Z","iopub.execute_input":"2023-07-31T18:21:05.870030Z","iopub.status.idle":"2023-07-31T18:21:06.061766Z","shell.execute_reply.started":"2023-07-31T18:21:05.869999Z","shell.execute_reply":"2023-07-31T18:21:06.060216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model\n","metadata":{}},{"cell_type":"code","source":"model = Sequential(\n    [               \n        tf.keras.Input(shape=(784,)),\n        Dense(784, activation='relu'),\n        Dense(512, activation='relu'),\n        Dense(512, activation='relu'),\n        Dense(512, activation='relu'),\n        Dense(512, activation='relu'),\n        Dense(10, activation='softmax')\n    ], name = \"my_model\" \n)\n\ncheckpoint_cb = keras.callbacks.ModelCheckpoint('best-model.h5', save_best_only=True)\nearly_stopping_cb = keras.callbacks.EarlyStopping(patience=5, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:06.062811Z","iopub.execute_input":"2023-07-31T18:21:06.063114Z","iopub.status.idle":"2023-07-31T18:21:06.303999Z","shell.execute_reply.started":"2023-07-31T18:21:06.063088Z","shell.execute_reply":"2023-07-31T18:21:06.302849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:21:06.305450Z","iopub.execute_input":"2023-07-31T18:21:06.306034Z","iopub.status.idle":"2023-07-31T18:21:06.333805Z","shell.execute_reply.started":"2023-07-31T18:21:06.306000Z","shell.execute_reply":"2023-07-31T18:21:06.332300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model with CategoricalCrossentropy loss and Adam optimizer\nmodel.compile(\n    loss=tf.keras.losses.CategoricalCrossentropy(from_logits=False),\n    optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005),\n    metrics=['accuracy']\n)\n\n# Fit the model to the training data\nhistory = model.fit(X_train, y_train, epochs=40, validation_data=(X_val, y_val),callbacks=[checkpoint_cb, early_stopping_cb])\n\nmodel.save('kaggle_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:35:57.045490Z","iopub.execute_input":"2023-07-31T18:35:57.045923Z","iopub.status.idle":"2023-07-31T18:41:00.337365Z","shell.execute_reply.started":"2023-07-31T18:35:57.045894Z","shell.execute_reply":"2023-07-31T18:41:00.335817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Access the training history from the `history` object\ntrain_loss = history.history['loss']\ntrain_accuracy = history.history['accuracy']\nval_loss = history.history['val_loss']\nval_accuracy = history.history['val_accuracy']\n\n# Plot the training and validation loss\nplt.figure(figsize=(6, 6))\nplt.plot(range(1, len(train_loss) + 1), train_loss, label='Training Loss')\nplt.plot(range(1, len(val_loss) + 1), val_loss, label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\nplt.grid()\nplt.show()\n\n# Plot the training and validation accuracy\nplt.figure(figsize=(6, 6))\nplt.plot(range(1, len(train_accuracy) + 1), train_accuracy, label='Training Accuracy')\nplt.plot(range(1, len(val_accuracy) + 1), val_accuracy, label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.legend()\nplt.grid()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:43.751422Z","iopub.execute_input":"2023-07-31T18:30:43.751683Z","iopub.status.idle":"2023-07-31T18:30:44.192267Z","shell.execute_reply.started":"2023-07-31T18:30:43.751662Z","shell.execute_reply":"2023-07-31T18:30:44.191203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Access the validation accuracy from the history\nval_accuracy = history.history['val_accuracy']\n\n# Find the index of the epoch with the highest validation accuracy\nbest_epoch_idx = np.argmax(val_accuracy)\n\n# Load the best model from that epoch\nbest_model = tf.keras.models.load_model('best-model.h5')\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:44.193785Z","iopub.execute_input":"2023-07-31T18:30:44.194152Z","iopub.status.idle":"2023-07-31T18:30:44.357961Z","shell.execute_reply.started":"2023-07-31T18:30:44.194122Z","shell.execute_reply":"2023-07-31T18:30:44.356465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test the model","metadata":{}},{"cell_type":"code","source":"test_df.shape\ntest_df.head()\nprint(test_df.iloc[0])","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:44.359773Z","iopub.execute_input":"2023-07-31T18:30:44.360071Z","iopub.status.idle":"2023-07-31T18:30:44.367298Z","shell.execute_reply.started":"2023-07-31T18:30:44.360047Z","shell.execute_reply":"2023-07-31T18:30:44.365606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = best_model.predict(np.asarray(test_df.iloc[10]).reshape(1,784))","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:44.369369Z","iopub.execute_input":"2023-07-31T18:30:44.370091Z","iopub.status.idle":"2023-07-31T18:30:44.529658Z","shell.execute_reply.started":"2023-07-31T18:30:44.370057Z","shell.execute_reply":"2023-07-31T18:30:44.528596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_value = np.max(prediction)\nmax_index = np.argmax(prediction)\n\nprint(\"Maximum value:\", max_value)\nprint(\"Index of the maximum value:\", max_index)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:44.530948Z","iopub.execute_input":"2023-07-31T18:30:44.531451Z","iopub.status.idle":"2023-07-31T18:30:44.537636Z","shell.execute_reply.started":"2023-07-31T18:30:44.531378Z","shell.execute_reply":"2023-07-31T18:30:44.536442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nimage_data = np.asarray(test_df.iloc[10]).reshape(image_shape)\n\n# Plot the image\nplt.imshow(image_data, cmap='gray')\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:44.538866Z","iopub.execute_input":"2023-07-31T18:30:44.539818Z","iopub.status.idle":"2023-07-31T18:30:44.610030Z","shell.execute_reply.started":"2023-07-31T18:30:44.539782Z","shell.execute_reply":"2023-07-31T18:30:44.608813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.asarray(test_df)\nnp.shape(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:44.611695Z","iopub.execute_input":"2023-07-31T18:30:44.612003Z","iopub.status.idle":"2023-07-31T18:30:44.618965Z","shell.execute_reply.started":"2023-07-31T18:30:44.611976Z","shell.execute_reply":"2023-07-31T18:30:44.617849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = best_model.predict(X_test, verbose=0)\npred = np.argmax(pred, axis=1)\npred = pd.Series(pred, name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:44.620223Z","iopub.execute_input":"2023-07-31T18:30:44.620568Z","iopub.status.idle":"2023-07-31T18:30:49.646226Z","shell.execute_reply.started":"2023-07-31T18:30:44.620541Z","shell.execute_reply":"2023-07-31T18:30:49.645483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmission['Label'] = pred\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T18:30:49.650908Z","iopub.execute_input":"2023-07-31T18:30:49.651419Z","iopub.status.idle":"2023-07-31T18:30:49.710959Z","shell.execute_reply.started":"2023-07-31T18:30:49.651376Z","shell.execute_reply":"2023-07-31T18:30:49.709423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}