{"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 numpy as np # linear algebra\nimport tensorflow as tf\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-14T12:59:53.769785Z","iopub.execute_input":"2022-01-14T12:59:53.770816Z","iopub.status.idle":"2022-01-14T12:59:53.775443Z","shell.execute_reply.started":"2022-01-14T12:59:53.770769Z","shell.execute_reply":"2022-01-14T12:59:53.774570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(filepath_or_buffer='../input/digit-recognizer/train.csv')\ntest_df = pd.read_csv(filepath_or_buffer='../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-14T12:59:53.777503Z","iopub.execute_input":"2022-01-14T12:59:53.777800Z","iopub.status.idle":"2022-01-14T12:59:57.177388Z","shell.execute_reply.started":"2022-01-14T12:59:53.777761Z","shell.execute_reply":"2022-01-14T12:59:57.176579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reshape_data(train_data, test_data):\n    train_np = train_data.to_numpy()\n    test_np = test_data.to_numpy()\n    \n    train_x_flat = train_np[:, 1:]\n    train_x = np.reshape(train_x_flat, (train_x_flat.shape[0], 28, 28))\n    train_y = train_np[:, 0]\n    \n    test_x = np.reshape(test_np, (test_np.shape[0], 28, 28))\n    \n    return (train_x, train_y), test_x\n\n(train_images, train_labels), test_images = reshape_data(train_df, test_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-14T12:59:57.178730Z","iopub.execute_input":"2022-01-14T12:59:57.179073Z","iopub.status.idle":"2022-01-14T12:59:57.186381Z","shell.execute_reply.started":"2022-01-14T12:59:57.179034Z","shell.execute_reply":"2022-01-14T12:59:57.185505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's demonstrate that the reshaping of the data worked\n\nplt.figure(figsize=(10, 10))\nfor i in range(16):\n    plt.subplot(4, 4, i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(train_images[40+i])\n    plt.xlabel(train_labels[40+i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-14T12:59:57.187540Z","iopub.execute_input":"2022-01-14T12:59:57.188375Z","iopub.status.idle":"2022-01-14T12:59:57.713398Z","shell.execute_reply.started":"2022-01-14T12:59:57.188333Z","shell.execute_reply":"2022-01-14T12:59:57.712635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The Model (CNN)","metadata":{}},{"cell_type":"code","source":"train_images = tf.expand_dims(train_images, axis=-1)\nprint(train_images.shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-14T12:59:57.715479Z","iopub.execute_input":"2022-01-14T12:59:57.715786Z","iopub.status.idle":"2022-01-14T12:59:58.093571Z","shell.execute_reply.started":"2022-01-14T12:59:57.715750Z","shell.execute_reply":"2022-01-14T12:59:58.092809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hyper parameters\nlearning_rate = 0.001\nepochs = 10\nbatch_size = 128\n\ncnn_model = tf.keras.Sequential([\n    tf.keras.layers.Rescaling(scale=1./255, input_shape=(28, 28, 1)),\n    tf.keras.layers.Conv2D(16, (3, 3), padding='same', activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2), padding='same'),\n    tf.keras.layers.Conv2D(32, (3, 3), padding='same', activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(64, (3, 3), padding='same', activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.MaxPooling2D((2, 2), padding='same'),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(1024, activation='relu'),\n    tf.keras.layers.Dense(10, activation='softmax')\n])\n\ncnn_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-14T12:59:58.094659Z","iopub.execute_input":"2022-01-14T12:59:58.094899Z","iopub.status.idle":"2022-01-14T12:59:58.191354Z","shell.execute_reply.started":"2022-01-14T12:59:58.094861Z","shell.execute_reply":"2022-01-14T12:59:58.190262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now train\n\ncnn_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate), loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])\n\ncnn_model.fit(train_images, train_labels, epochs=epochs, validation_split=0.2, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-14T12:59:58.192388Z","iopub.execute_input":"2022-01-14T12:59:58.192700Z","iopub.status.idle":"2022-01-14T13:01:20.990098Z","shell.execute_reply.started":"2022-01-14T12:59:58.192656Z","shell.execute_reply":"2022-01-14T13:01:20.989177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Now use the model to predict stuff","metadata":{}},{"cell_type":"code","source":"test_images = tf.expand_dims(test_images, axis=-1)\n\ntest_predictions = cnn_model(test_images)\nprint(test_predictions)","metadata":{"execution":{"iopub.status.busy":"2022-01-14T13:01:20.991584Z","iopub.execute_input":"2022-01-14T13:01:20.991929Z","iopub.status.idle":"2022-01-14T13:01:21.598694Z","shell.execute_reply.started":"2022-01-14T13:01:20.991884Z","shell.execute_reply":"2022-01-14T13:01:21.597960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Since the model is very sure of itself with its probabilities, we don't bother with random choices given by the model distribution, we just choose the digit\n# with the highest probability\n\ntest_probabilities = np.max(test_predictions, axis=1)\ntest_labels = np.argmax(test_predictions, axis=1)\ntest_imageIDs = np.arange(1, len(test_labels)+1, dtype=int)\n\nprint(test_labels.shape)\nprint(test_imageIDs.shape)\nprint(test_imageIDs[np.newaxis].T)","metadata":{"execution":{"iopub.status.busy":"2022-01-14T13:01:21.600072Z","iopub.execute_input":"2022-01-14T13:01:21.600560Z","iopub.status.idle":"2022-01-14T13:01:21.614100Z","shell.execute_reply.started":"2022-01-14T13:01:21.600522Z","shell.execute_reply":"2022-01-14T13:01:21.613408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(test_imageIDs[np.newaxis].T, columns=['ImageId'])\nsubmission = submission.join(pd.DataFrame(test_labels[np.newaxis].T, columns=['Label']))\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-14T13:01:21.618492Z","iopub.execute_input":"2022-01-14T13:01:21.619093Z","iopub.status.idle":"2022-01-14T13:01:21.639455Z","shell.execute_reply.started":"2022-01-14T13:01:21.619052Z","shell.execute_reply":"2022-01-14T13:01:21.637851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-14T13:01:21.641679Z","iopub.execute_input":"2022-01-14T13:01:21.642110Z","iopub.status.idle":"2022-01-14T13:01:21.697588Z","shell.execute_reply.started":"2022-01-14T13:01:21.642072Z","shell.execute_reply":"2022-01-14T13:01:21.696898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}