{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"In this notebook we train a Convolutional Neural Network (CNN) in order to predict what digit is in a photograph of 28x28 pixels using the MNIST data set which contains photographs of hand written digits. In the first cell below, we read in the data, and split our data into a train and cross validation set in order to evaluate the effectiveness of our model and tweak the hyperparameters for optimal performance. \n\nThe flattened data must be reshaped into a 28x28 array for each image as this allows convolution techniques to be applied.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, models\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.preprocessing import StandardScaler\nimport matplotlib.pyplot as plt\n\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\ny = train_data['label']\nX = train_data.drop(columns = ['label'])\nX_train, X_cv, y_train, y_cv = train_test_split(X, y, test_size=0.2, random_state=42)\n\ny_train_one_hot = to_categorical(y_train)\ny_cv_one_hot = to_categorical(y_cv)\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_cv = scaler.transform(X_cv)\n\nimages_X_train = np.array([image.reshape((28, 28)) for image in X_train])\nimages_X_cv = np.array([image.reshape((28, 28)) for image in X_cv])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Build the model\n\n# Define the CNN model\nmodel = models.Sequential()\n\n# Convolutional layers\nmodel.add(layers.Conv2D(16, (2, 2), activation='relu',padding = 'same', input_shape=(28, 28,1)))\nmodel.add(BatchNormalization())\nmodel.add(layers.Conv2D(16, (2, 2), activation='relu', padding = 'same'))\nmodel.add(BatchNormalization())\n\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.5))\n\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu',padding = 'same'))\nmodel.add(BatchNormalization())\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu',padding = 'same'))\nmodel.add(BatchNormalization())\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.5))\n\n# Flatten the output for the fully connected layers\nmodel.add(layers.Flatten())\n\n# Fully connected layers\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(10, activation='softmax'))  \n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Display the model summary\nmodel.summary()\n# Train the model\nmodel.fit(images_X_train, y_train_one_hot, epochs = 1, batch_size=100, validation_data=(images_X_cv, y_cv_one_hot))\n\n# Evaluate the model\ntest_loss, test_acc = model.evaluate(images_X_cv, y_cv_one_hot)\nprint(f\"Test accuracy: {test_acc}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we train the model on the entire data set.","metadata":{}},{"cell_type":"code","source":"X = scaler.fit_transform(X)\ny_one_hot = to_categorical(y)\nimages_X = np.array([image.reshape((28, 28)) for image in X])\n\nmodel.fit(images_X, y_one_hot, epochs = 19, batch_size=100)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = scaler.transform(test_data)\ntest_data = np.array([image.reshape((28, 28)) for image in test_data])\n\npredictions = np.argmax(model.predict(test_data), axis=1)\nids = [i+1 for i in range(len(predictions))]\n\ndf = pd.DataFrame({\n    'Imageid': ids,\n    'Label': predictions\n})\n    \ndf.to_csv('submission.csv', index=False)\n\nprint(\"Your submission was successfully saved!\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}