{"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":"markdown","source":"<a href=\"https://colab.research.google.com/github/denis-spe/Digit-Recognization/blob/v1/digit_recognization_tf.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>","metadata":{"id":"view-in-github"}},{"cell_type":"markdown","source":"<h1>Digit Recognization With Tensorflow</h1>","metadata":{"id":"dfHF-xOQqNfD"}},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Import Necessary Libraries and packages</h2>","metadata":{"id":"8CgtQ9TgqNfI"}},{"cell_type":"code","source":"import os\nimport datetime\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow import keras\nfrom tensorflow.keras import callbacks\nfrom tensorflow.keras import layers\nprint(\"tensorflow version: \", tf.__version__)","metadata":{"id":"tHfHwR-OqNfK","outputId":"14433e8f-6dca-4647-f9ff-c78e4eb83d21","execution":{"iopub.status.busy":"2022-04-15T00:21:53.511219Z","iopub.execute_input":"2022-04-15T00:21:53.511921Z","iopub.status.idle":"2022-04-15T00:21:53.518154Z","shell.execute_reply.started":"2022-04-15T00:21:53.511885Z","shell.execute_reply":"2022-04-15T00:21:53.517512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Setting The Notebook</h2>","metadata":{"id":"mb6P9C3KqNfN"}},{"cell_type":"code","source":"# Setting the graph style\nplt.rc('figure', autolayout=True)\nplt.rc(\n    'axes', titleweight='bold', \n    titlesize=20, labelweight=700,\n    labelsize=13\n    )\nplt.rc('font', size=15)\n\ndef set_seeds(seed: int=0):\n    \"\"\"Sets the Seed into order to get the same result on every code run\"\"\"\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    tf.random.set_seed(seed)\n    np.random.seed(seed)\n    os.environ[\"TF_DETERMINISTIC\"] = str(seed)\n    os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"-1\"\n\n# Call the set_seeds with default parameter\nset_seeds()\n\ntry:\n    tfu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n    stratagy = tf.distribute.TPUStrategy(tfu)\nexcept ValueError:\n    stratagy = tf.distribute.get_strategy()\nprint(f\"Number of Accelerator: {stratagy.num_replicas_in_sync}\")","metadata":{"id":"AEjq8QjsqNfN","outputId":"739764a2-c9bc-4c0c-8b6c-be667da1fb2b","execution":{"iopub.status.busy":"2022-04-15T00:21:53.537162Z","iopub.execute_input":"2022-04-15T00:21:53.537945Z","iopub.status.idle":"2022-04-15T00:21:53.547614Z","shell.execute_reply.started":"2022-04-15T00:21:53.537906Z","shell.execute_reply":"2022-04-15T00:21:53.546644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Above Dataset</h2>\n<p style=\"font-size: 15px;\">\nThe data files train.csv and test.csv contain gray-scale images of hand-drawn digits, from zero through nine.\n\nEach image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total. Each pixel has a single pixel-value associated with it, indicating the lightness or darkness of that pixel, with higher numbers meaning darker. This pixel-value is an integer between 0 and 255, inclusive.\n\nThe training data set, (train.csv), has 785 columns. The first column, called \"label\", is the digit that was drawn by the user. The rest of the columns contain the pixel-values of the associated image.\n\nEach pixel column in the training set has a name like pixelx, where x is an integer between 0 and 783, inclusive. To locate this pixel on the image, suppose that we have decomposed x as x = i * 28 + j, where i and j are integers between 0 and 27, inclusive. Then pixelx is located on row i and column j of a 28 x 28 matrix, (indexing by zero).\n</p>","metadata":{"id":"PisWUlUmqNfO"}},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Load Csv File Into Pandas DataFrame</h2>","metadata":{"id":"zWpujn2MqNfP"}},{"cell_type":"code","source":"try:\n    from google.colab import drive\n    # Mount google files\n    drive.mount('/gdrive')\n\n    # list the files from drive\n    folder_path = '../gdrive/MyDrive/Datasets/MNIST_data'\n    \nexcept ImportError:\n    folder_path = '../input/digit-recognizer'\n\nos.listdir(folder_path)","metadata":{"id":"gjagb9UJqWKc","outputId":"dc250860-8a29-4a96-dbf0-a78828232e45","execution":{"iopub.status.busy":"2022-04-15T00:21:53.566330Z","iopub.execute_input":"2022-04-15T00:21:53.566602Z","iopub.status.idle":"2022-04-15T00:21:53.580325Z","shell.execute_reply.started":"2022-04-15T00:21:53.566569Z","shell.execute_reply":"2022-04-15T00:21:53.579495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Path\ntrain_data_path = folder_path + '/train.csv'\ntest_data_path = folder_path + '/test.csv'\n\n# Loading dataset into pandas \ntrain_df = pd.read_csv(train_data_path)\ntest_df = pd.read_csv(test_data_path)","metadata":{"id":"qnfQo_yxqNfP","execution":{"iopub.status.busy":"2022-04-15T00:21:53.581609Z","iopub.execute_input":"2022-04-15T00:21:53.582216Z","iopub.status.idle":"2022-04-15T00:21:59.396355Z","shell.execute_reply.started":"2022-04-15T00:21:53.582157Z","shell.execute_reply":"2022-04-15T00:21:59.395400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">View The Dataframe</h2>","metadata":{"id":"bfkkDqGdqNfQ"}},{"cell_type":"code","source":"# First Rows in the train data\nprint(train_df.shape)\ntrain_df.head()","metadata":{"id":"ieRawEREqNfR","outputId":"c1d6fdc8-fcef-4f7a-87fa-dc882f37a85d","execution":{"iopub.status.busy":"2022-04-15T00:21:59.397741Z","iopub.execute_input":"2022-04-15T00:21:59.397945Z","iopub.status.idle":"2022-04-15T00:21:59.424506Z","shell.execute_reply.started":"2022-04-15T00:21:59.397918Z","shell.execute_reply":"2022-04-15T00:21:59.423716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# First Rows in the test data\nprint(test_df.shape)\ntest_df.head()","metadata":{"id":"4IRzv3OnqNfR","outputId":"81ff070c-c48c-46cf-bfd3-a20b07d1b405","execution":{"iopub.status.busy":"2022-04-15T00:21:59.425702Z","iopub.execute_input":"2022-04-15T00:21:59.426081Z","iopub.status.idle":"2022-04-15T00:21:59.445982Z","shell.execute_reply.started":"2022-04-15T00:21:59.426042Z","shell.execute_reply":"2022-04-15T00:21:59.445060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Train Data Split Into Train and Validation data</h2>","metadata":{"id":"1iKh19Uu2OEQ"}},{"cell_type":"code","source":"mask = np.random.randn(len(train_df)) > 0.8\ntrain_data = train_df[mask]\nvalid_data = train_df[~mask]\nprint(f\"Train_data shape: {train_data.shape}\\nValid_data: {valid_data.shape}\")","metadata":{"id":"GqEJl7EA2dV5","outputId":"25f4b6ca-d469-49fe-a746-b57e93cef42d","execution":{"iopub.status.busy":"2022-04-15T00:21:59.447909Z","iopub.execute_input":"2022-04-15T00:21:59.448141Z","iopub.status.idle":"2022-04-15T00:21:59.623452Z","shell.execute_reply.started":"2022-04-15T00:21:59.448114Z","shell.execute_reply":"2022-04-15T00:21:59.622539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Create Dataset Generator</h2>","metadata":{"id":"WQofPy3lqNfS"}},{"cell_type":"code","source":"class DatasetGenerator:\n    def __init__(self, batch_size: int = 64, shuffle: int = False, epoch: int = 10) -> None:\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        self.epoch = epoch\n\n    def __call__(self, df: pd.DataFrame) -> any:\n        if self.shuffle:\n            df_ = tf.constant([\n                row[1:].values / 255\n                for _, row in df.iterrows()\n                ])\n            df_ = tf.reshape(df_, [len(df), 28, 28])\n            labels = df.iloc[:, 0]\n            data = tf.data.Dataset.from_tensor_slices((df_, labels)).batch(self.batch_size).shuffle(1000)\n        else:\n            df_ = tf.constant([\n                row.values.reshape(28, 28) /255\n                for _, row in df.iterrows()\n                ])\n            df_ = tf.reshape(df_, [len(df), 28, 28])\n            data = tf.data.Dataset.from_tensor_slices((df_)).batch(self.batch_size)\n        return data","metadata":{"id":"6RMv7k1zqNfT","execution":{"iopub.status.busy":"2022-04-15T00:21:59.624606Z","iopub.execute_input":"2022-04-15T00:21:59.624849Z","iopub.status.idle":"2022-04-15T00:21:59.636756Z","shell.execute_reply.started":"2022-04-15T00:21:59.624809Z","shell.execute_reply":"2022-04-15T00:21:59.635813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Convert The Data To tensor</h2>","metadata":{"id":"rQZNmVfVqNfV"}},{"cell_type":"code","source":"# Change the dataframes to generator \ntrain_data = DatasetGenerator(shuffle=True)(train_data)\nvalidation_data = DatasetGenerator(shuffle=True)(valid_data)\ntest_dataset = DatasetGenerator()(test_df)","metadata":{"id":"LD4Zd62NqNfW","execution":{"iopub.status.busy":"2022-04-15T00:21:59.638004Z","iopub.execute_input":"2022-04-15T00:21:59.638248Z","iopub.status.idle":"2022-04-15T00:22:13.884592Z","shell.execute_reply.started":"2022-04-15T00:21:59.638216Z","shell.execute_reply":"2022-04-15T00:22:13.883662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Exploring Datasets</h2>","metadata":{"id":"v0DWrfItqNfW"}},{"cell_type":"code","source":"# Exploring One of the image in the train_dataset generator\nimg, label = next(iter(train_data))","metadata":{"id":"ADVM-AAcqNfX","execution":{"iopub.status.busy":"2022-04-15T00:22:13.885940Z","iopub.execute_input":"2022-04-15T00:22:13.886281Z","iopub.status.idle":"2022-04-15T00:22:14.008085Z","shell.execute_reply.started":"2022-04-15T00:22:13.886239Z","shell.execute_reply":"2022-04-15T00:22:14.007413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"image Datatype: \", type(img[0]))\nprint(\"label data type: \", type(label[1]))\nprint(\"label: \", set(label.numpy()))","metadata":{"id":"7uup4sof4w7S","outputId":"a996d2eb-d6d2-4f25-9dc4-d272c4e11684","execution":{"iopub.status.busy":"2022-04-15T00:22:14.009557Z","iopub.execute_input":"2022-04-15T00:22:14.009866Z","iopub.status.idle":"2022-04-15T00:22:14.027801Z","shell.execute_reply.started":"2022-04-15T00:22:14.009823Z","shell.execute_reply":"2022-04-15T00:22:14.026879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For exploratory purpose change the image to numpy array\nnp_img = np.array(img)\nIMG_SHAPE = np_img.shape\nprint(\"Image shape: \", IMG_SHAPE)","metadata":{"id":"vjPGLIZzqNfX","outputId":"b6c93022-f2df-43e7-e399-17b3a6b87be7","execution":{"iopub.status.busy":"2022-04-15T00:22:14.029142Z","iopub.execute_input":"2022-04-15T00:22:14.029675Z","iopub.status.idle":"2022-04-15T00:22:14.041219Z","shell.execute_reply.started":"2022-04-15T00:22:14.029627Z","shell.execute_reply":"2022-04-15T00:22:14.040246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Digits Visualization</h2>","metadata":{"id":"fbfPxc_gqNfY"}},{"cell_type":"code","source":"# Visualizing of the one image in the test_dataset\nimg = next(iter(test_dataset))\ncmap = plt.get_cmap('magma')\nplt.imshow(tf.squeeze(img[0]), cmap=cmap)\nplt.axis('off')\nplt.show()\n","metadata":{"id":"gCTp8TCuqNfZ","outputId":"2b907878-9a41-45a3-bc9b-ef97f987f4f4","execution":{"iopub.status.busy":"2022-04-15T00:22:14.044316Z","iopub.execute_input":"2022-04-15T00:22:14.045070Z","iopub.status.idle":"2022-04-15T00:22:14.324058Z","shell.execute_reply.started":"2022-04-15T00:22:14.045023Z","shell.execute_reply":"2022-04-15T00:22:14.323437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create Subplot instance\nfig, ax = plt.subplots(figsize=(8, 8))\n\n# Number of columns and rows\nNROW = 3\nNCOL = 3\n\n# loop over the range of NROW * NCOL starting from 1\nfor i in range(1, NROW * NROW + 1):\n    fig.add_subplot(NROW, NCOL, i)\n\n    # Get the image from the generated index\n    img, label = next(iter(train_data))\n\n    # Plot it\n    plt.imshow(tf.squeeze(img[0]), \n        cmap=cmap\n        )\n    plt.axis('off')\n    plt.title(label[0].numpy())\n    ax.set_xticklabels([])\n    ax.set_yticklabels([])\n    ax.set_axis_off()","metadata":{"id":"Qy9WTFQIqNfZ","outputId":"87649122-ae5e-4685-a12e-8644bdfaadf8","execution":{"iopub.status.busy":"2022-04-15T00:22:14.325790Z","iopub.execute_input":"2022-04-15T00:22:14.326411Z","iopub.status.idle":"2022-04-15T00:22:15.333623Z","shell.execute_reply.started":"2022-04-15T00:22:14.326363Z","shell.execute_reply":"2022-04-15T00:22:15.332748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Modeling</h2>","metadata":{"id":"GHCjAPFDqNfb"}},{"cell_type":"code","source":"train_nrow = np.array(list(train_data.as_numpy_iterator())).shape[0]\ntest_nrow = np.array(list(validation_data.as_numpy_iterator())).shape[0]\ntraining_size = train_nrow - test_nrow\nBATCH_SIZE_PER_REPLICA = 32\nbatch_size = BATCH_SIZE_PER_REPLICA * stratagy.num_replicas_in_sync\nsteps_per_epoch = training_size // batch_size\nvalidation_steps = test_nrow // batch_size","metadata":{"id":"PQ7MdFL18A51","outputId":"67f499db-277f-46b6-90a6-86747d50321c","execution":{"iopub.status.busy":"2022-04-15T00:22:15.335389Z","iopub.execute_input":"2022-04-15T00:22:15.335957Z","iopub.status.idle":"2022-04-15T00:22:15.620828Z","shell.execute_reply.started":"2022-04-15T00:22:15.335911Z","shell.execute_reply":"2022-04-15T00:22:15.619415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef build_model(input_shape, eager: bool = False):\n    \"\"\"Create a Keras Sequential and complie it\"\"\"\n    model = keras.Sequential()\n    model.add(layers.Flatten(input_shape=input_shape))\n\n    model.add(layers.Dense(units=256, activation='relu'))\n    # model.add(layers.BatchNormalization())\n    model.add(layers.Dropout(rate=0.5))\n\n    model.add(layers.Dense(units=256, activation='relu'))\n    # model.add(layers.BatchNormalization())\n    model.add(layers.Dropout(rate=0.5))\n\n    model.add(layers.Dense(units=256, activation='relu'))\n    # model.add(layers.BatchNormalization())\n    model.add(layers.Dropout(rate=0.2))\n\n\n\n    model.add(layers.Dense(units=10, activation='softmax'))\n\n    if eager:\n        model.compile(\n            optimizer='adam',\n            loss='SparseCategoricalCrossentropy',\n            metrics=['acc'],\n            run_eagerly=True\n\n        )\n    else:\n        model.compile(\n            optimizer='adam',\n            loss='SparseCategoricalCrossentropy',\n            metrics=['acc'],\n        )\n    return model\n\n# Model instance\nmodel = build_model(IMG_SHAPE[1:])\n\n# Plot the model\ndot_image_file = '/tmp/model_1.png'\nkeras.utils.plot_model(model, to_file=dot_image_file, show_shapes=True)","metadata":{"id":"Wr9hSvLOqNfb","outputId":"7840dfd3-2005-4d44-87ca-97ad45c94580","execution":{"iopub.status.busy":"2022-04-15T00:22:15.624778Z","iopub.execute_input":"2022-04-15T00:22:15.625065Z","iopub.status.idle":"2022-04-15T00:22:16.713128Z","shell.execute_reply.started":"2022-04-15T00:22:15.625030Z","shell.execute_reply":"2022-04-15T00:22:16.712380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize the EarlyStopping class: early_stopping\nearly_stopping = callbacks.EarlyStopping(\n    patience=1,\n    min_delta=0.01,\n    restore_best_weights=True\n    \n)","metadata":{"id":"Qv4bS0qpmOLZ","execution":{"iopub.status.busy":"2022-04-15T00:22:16.714504Z","iopub.execute_input":"2022-04-15T00:22:16.714847Z","iopub.status.idle":"2022-04-15T00:22:16.719272Z","shell.execute_reply.started":"2022-04-15T00:22:16.714816Z","shell.execute_reply":"2022-04-15T00:22:16.718561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Callback to save the Keras model or model weights at some frequency","metadata":{"id":"zA-8lkezX6W4"}},{"cell_type":"code","source":"checkpoint_filepath = \"/tmp/checkpoints\"\n\nmodel_checkpoint_callback = callbacks.ModelCheckpoint(\n    filepath=checkpoint_filepath,\n    save_weights_only=True,\n    monitor='val_accuracy',\n    mode='max',\n    save_best_only=True)","metadata":{"id":"MosPsaLOXykx","execution":{"iopub.status.busy":"2022-04-15T00:22:16.720189Z","iopub.execute_input":"2022-04-15T00:22:16.720609Z","iopub.status.idle":"2022-04-15T00:22:16.732882Z","shell.execute_reply.started":"2022-04-15T00:22:16.720576Z","shell.execute_reply":"2022-04-15T00:22:16.732193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reduce learning rate when a metric has stopped improving","metadata":{"id":"ogk2FU7mhF0q"}},{"cell_type":"code","source":"reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2,\n                              patience=0, min_lr=0.001, mode='auto', verbose=1)","metadata":{"id":"4Q0XR6xohD7J","execution":{"iopub.status.busy":"2022-04-15T00:22:16.734093Z","iopub.execute_input":"2022-04-15T00:22:16.734541Z","iopub.status.idle":"2022-04-15T00:22:16.747684Z","shell.execute_reply.started":"2022-04-15T00:22:16.734510Z","shell.execute_reply":"2022-04-15T00:22:16.746800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Fit the model\nwith stratagy.scope():\n    model = build_model(IMG_SHAPE[1:])\n\nhistory = model.fit(\n    train_data, \n    validation_data=validation_data, \n    epochs=15,\n    callbacks=[early_stopping, reduce_lr],\n    batch_size=batch_size,\n    validation_steps=validation_steps \n    )","metadata":{"id":"DKo5JrQLqNfc","outputId":"9ac273bf-eda8-4ed7-d375-85dd0df4f678","execution":{"iopub.status.busy":"2022-04-15T00:22:16.748949Z","iopub.execute_input":"2022-04-15T00:22:16.749307Z","iopub.status.idle":"2022-04-15T00:22:24.526688Z","shell.execute_reply.started":"2022-04-15T00:22:16.749276Z","shell.execute_reply":"2022-04-15T00:22:24.526000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training without early_stopping callback\n# Fit the model\nwith stratagy.scope():\n    model = build_model(IMG_SHAPE[1:])\n\nhistory = model.fit(\n    train_data, \n    validation_data=validation_data,\n    epochs=20,\n    callbacks=[model_checkpoint_callback, reduce_lr],\n    # Made same improve with these two code\n    batch_size=batch_size,\n    validation_steps=validation_steps\n    )","metadata":{"id":"_uEVPYLdHaE8","outputId":"37e29189-84ff-45e1-f24c-69933aa498c2","execution":{"iopub.status.busy":"2022-04-15T00:22:24.528118Z","iopub.execute_input":"2022-04-15T00:22:24.528651Z","iopub.status.idle":"2022-04-15T00:22:45.211056Z","shell.execute_reply.started":"2022-04-15T00:22:24.528603Z","shell.execute_reply":"2022-04-15T00:22:45.210047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Construct a dataframe with loss and accuracy score\nhistory_df = pd.DataFrame(history.history)\nhistory_df.head()","metadata":{"id":"1b6TZUFwX0_2","outputId":"7686dd9e-c1e3-4bef-a5d6-be0ec3f56bf6","execution":{"iopub.status.busy":"2022-04-15T00:22:45.212386Z","iopub.execute_input":"2022-04-15T00:22:45.212632Z","iopub.status.idle":"2022-04-15T00:22:45.227396Z","shell.execute_reply.started":"2022-04-15T00:22:45.212603Z","shell.execute_reply":"2022-04-15T00:22:45.226734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Visualizing Train And Validation Loss And Accuracy</h2>","metadata":{"id":"vdMJXsCyYBcN"}},{"cell_type":"code","source":"def metric_vis(train: str, val: str, ax: plt.axis) -> None:\n\n    fig1 = history_df[train].plot(c='pink', ax=ax, marker=\"x\")\n    fig2 = history_df[val].plot(c='skyblue', ax=ax, marker=\"o\")\n    ax.set_ylabel(f\"{train} Score\")\n    ax.set_xlabel(\"Epoch/Iteration\")\n    ax.set_title(f\"Train And Validation {train}\".title())\n    # Hide the right and top spines\n    ax.spines['right'].set_visible(False)\n    ax.spines['top'].set_visible(False)\n    ax.legend([f'{train}', f\"{val}\"])","metadata":{"id":"oeN4ZDcMc-kz","execution":{"iopub.status.busy":"2022-04-15T00:22:45.228551Z","iopub.execute_input":"2022-04-15T00:22:45.230378Z","iopub.status.idle":"2022-04-15T00:22:45.238627Z","shell.execute_reply.started":"2022-04-15T00:22:45.230326Z","shell.execute_reply":"2022-04-15T00:22:45.237867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(10, 5))\nmetric_vis(\"loss\", 'val_loss', ax=ax[0])\nmetric_vis(\"acc\", 'val_acc', ax=ax[1])","metadata":{"id":"V7cm6wKhY8iP","outputId":"31bfaaa0-5ee3-4331-d43a-ac2c798f7f44","execution":{"iopub.status.busy":"2022-04-15T00:22:45.240048Z","iopub.execute_input":"2022-04-15T00:22:45.240670Z","iopub.status.idle":"2022-04-15T00:22:45.803145Z","shell.execute_reply.started":"2022-04-15T00:22:45.240628Z","shell.execute_reply":"2022-04-15T00:22:45.802262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style=\"font-size: 20px;\">Submission</h2>","metadata":{"id":"0ivZTEHFWwsh"}},{"cell_type":"code","source":"# Make predictions\nprob  = model.predict(test_dataset)\n# Get the highest value from probability\npred = tf.argmax(prob, 1)","metadata":{"id":"AdhSM8kMqNfd","execution":{"iopub.status.busy":"2022-04-15T00:22:45.804276Z","iopub.execute_input":"2022-04-15T00:22:45.804510Z","iopub.status.idle":"2022-04-15T00:22:47.407797Z","shell.execute_reply.started":"2022-04-15T00:22:45.804480Z","shell.execute_reply":"2022-04-15T00:22:47.406885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.read_csv(folder_path + '/sample_submission.csv')\nsubmission['Label']=pred\nsubmission.to_csv('prediction14.csv', index=False)","metadata":{"id":"8WE9oJ-iPWVD","execution":{"iopub.status.busy":"2022-04-15T00:22:47.411158Z","iopub.execute_input":"2022-04-15T00:22:47.411536Z","iopub.status.idle":"2022-04-15T00:22:47.485469Z","shell.execute_reply.started":"2022-04-15T00:22:47.411488Z","shell.execute_reply":"2022-04-15T00:22:47.484467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"bQpflI6ZhSG1"},"execution_count":null,"outputs":[]}]}