cmkl/fall-2024/aicore/aic-201/00030/MNIST Handwriting Classific...

1 line
110 KiB
Plaintext
Raw Normal View History

2024-11-29 23:54:08 +07:00
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"toc_visible":true,"gpuType":"T4"},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"cells":[{"cell_type":"markdown","metadata":{"id":"HPR_nNQ94KJ-"},"source":["# Prepare Environment"]},{"cell_type":"code","metadata":{"id":"m15_JQeGuaTX","executionInfo":{"status":"ok","timestamp":1730172105657,"user_tz":-420,"elapsed":1108,"user":{"displayName":"Akadej Udomchaiporn","userId":"12826764406638924459"}}},"source":["from __future__ import absolute_import\n","from __future__ import division\n","from __future__ import print_function\n","\n","from IPython.display import display\n","\n","import matplotlib\n","import matplotlib.pyplot as plt\n","plt.rcParams[\"axes.grid\"] = False\n","%matplotlib inline"],"execution_count":1,"outputs":[]},{"cell_type":"code","metadata":{"id":"z--Q9_0x_0GP","executionInfo":{"status":"ok","timestamp":1730172143413,"user_tz":-420,"elapsed":4989,"user":{"displayName":"Akadej Udomchaiporn","userId":"12826764406638924459"}}},"source":["import numpy as np\n","import keras\n","import tensorflow as tf"],"execution_count":2,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"lXmN9g0ayjnx"},"source":["# Load MNIST dataset\n","\n","The MNIST database (Modified National Institute of Standards and Technology database) is a large database of handwritten digits.\n","\n","Ref: http://yann.lecun.com/exdb/mnist/"]},{"cell_type":"code","metadata":{"id":"LLM9sJikvkFn","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1730172147636,"user_tz":-420,"elapsed":1468,"user":{"displayName":"Akadej Udomchaiporn","userId":"12826764406638924459"}},"outputId":"28b65b4d-c8f3-4e65-c916-191b0b454316"},"source":["from keras.datasets import mnist\n","\n","# Download MNIST dataset using `datasets` module in Keras\n","# Note: the data have already been split into training and test sets\n","(x_train, y_train), (x_test, y_test) = mnist.load_data()\n","\n","print(f'Training set: {x_train.shape}, {y_train.shape}')\n","print(f'Test set: {x_test.shape}, {y_test.shape}')"],"execution_count":3,"outputs":[{"output_type":"stream","name":"stdout","text":["Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n","\u001b[1m11490434/11490434\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 0us/step\n","Training set: (60000, 28, 28), (60000,)\n","Test set: (10000, 28, 28), (10000,)\n"]}]},{"cell_type":"markdown","metadata":{"id":"dsyiuNmM2PwN"},"source":["Let's look at some examples of the training and test sets."]},{"cell_type":"code","metadata":{"id":"xgrAQgknzX79","colab":{"base_uri":"https://localhost:8080/","height":352},"executionInfo":{"status":"ok","timestamp":1730172173520,"user_tz":-420,"elapsed":4126,"user":{"displayName":"Akadej Udomchaiporn","userId":"12826764406638924459"}},"outputId":"f8501c04-56b4-4625-a770-5bee584fa274"},"source":["def plot_mnist_data(data, label, n_images):\n"," img_w = 28\n"," img_h = 28\n"," image = np.reshape(data, (-1, img_h, img_w))\n","\n"," f, axs = plt.subplots(1, n_images)\n"," f.set_figheight(15)\n"," f.set_figwidth(15)\n"," for i in range(len(axs)):\n"," axs[i].imshow(image[i], cmap=\"gray\")\n"," axs[i].set_title(f\"Label: {label[i]}\", fontsize=20)\n"," axs[i].tick_params(\n"," axis='both',\n"," which='both',\n"," bottom=False, top=False,left=False, right=False,\n"," labelbottom=False, labeltop=False, labelleft=False, labelright=False)\n"," plt.show()\n"," plt.close(\"all\")\n","\n","print(\"Training set\")\n","plot_mnist_data(x_train, y_train, n_images=8)\n","\n","print(\"Test set\")\n","plot_mnist_data(x_test, y_test, n_images=8)"],"execution_count":4,"outputs":[{"output_type":"stream","name":"stdout","text":["Training set\n"]},{"output_type":"display_data","data":{"text/plain":["<Figure size 1500x1500 with 8 Axes>"],"image/png":"iVBORw0KGgoAAAANSUhEUgAA