{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\"Open" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "U0bxC_CUcGzk", "outputId": "d143fe3a-a989-4da5-cff4-42fa60ccd330" }, "outputs": [], "source": [ "import tensorflow as tf\n", "import tensorflow_datasets as tfds\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from random import randint\n", "\n", "# In Google Colab, can change runtime to GPU if desired. If a TensorFlow \n", "# operation has both CPU and GPU implementations, by default the GPU device \n", "# is prioritized when the operation is assigned.\n", "device = tf.config.get_visible_devices()\n", "print(device)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "sV3gUT4lK4uO" }, "outputs": [], "source": [ "# Random seed for reproducibility\n", "seed = 42\n", "tf.keras.utils.set_random_seed(seed)\n", "\n", "# Save the model at the end?\n", "save_model = False\n", "\n", "# Batch sizes for training and testing\n", "batch_size = 64\n", "test_batch_size = 14\n", "\n", "# Training epochs\n", "n_epochs = 10\n", "\n", "# Learning rate\n", "learning_rate = 1.0\n", "\n", "# Decay rate for adjusting the learning rate\n", "gamma = 0.7\n", "\n", "# Number of target classes in the MNIST data\n", "num_classes = 10\n", "\n", "# Data input shape\n", "input_shape = (28, 28, 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ki3C27nDdb53", "outputId": "0b54a525-98f2-444b-9e7f-b6b3da22a157" }, "outputs": [], "source": [ "# Load the MNIST dataset\n", "mnist = tf.keras.datasets.mnist\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "# The scaled mean and standard deviation of the MNIST dataset (precalculated)\n", "data_mean = 0.1307\n", "data_std = 0.3081\n", "\n", "# Reshape the input data\n", "x_train = x_train.reshape(x_train.shape[0], \n", " x_train.shape[1], \n", " x_train.shape[2], 1)\n", "\n", "x_test = x_test.reshape(x_test.shape[0], \n", " x_test.shape[1], \n", " x_test.shape[2], 1)\n", "\n", "# Normalize the data\n", "x_train = (x_train/255.0 - data_mean) / data_std\n", "x_test = (x_test/255.0 - data_mean) / data_std\n", "\n", "# Convert labels to one-hot vectors\n", "y_train = tf.one_hot(y_train.astype(np.int32), depth=num_classes)\n", "y_test = tf.one_hot(y_test.astype(np.int32), depth=num_classes)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "tILjrHRFdb3x" }, "outputs": [], "source": [ "# Define the architecture of the neural network\n", "model = tf.keras.models.Sequential([\n", " tf.keras.layers.Conv2D(32, (3,3), strides=(1,1),\n", " padding='valid', \n", " activation='relu',\n", " input_shape=input_shape),\n", " tf.keras.layers.Conv2D(64, (3,3), strides=(1,1),\n", " padding='valid',\n", " activation='relu'),\n", " tf.keras.layers.MaxPool2D(),\n", " tf.keras.layers.Dropout(0.25),\n", " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(128, activation='relu'),\n", " tf.keras.layers.Dropout(0.5),\n", " tf.keras.layers.Dense(num_classes, activation='softmax')\n", "])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ERKGNdfxdb1s", "outputId": "ef8fbf8d-bca9-44d5-89c4-502e408b5b53" }, "outputs": [], "source": [ "# Decay the learning rate at a base rate of gamma roughly every epoch, which\n", "# is len(x_train) steps\n", "scheduler = tf.keras.optimizers.schedules.ExponentialDecay(\n", " learning_rate,\n", " decay_steps=len(x_train),\n", " decay_rate=gamma)\n", "\n", "# Define the optimizer to user for gradient descent\n", "optimizer = tf.keras.optimizers.Adadelta(scheduler)\n", "\n", "# Compile the model\n", "model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc'])\n", "\n", "# Display a model summary\n", "model.summary()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_PGJ8FXmMucW", "outputId": "7702889e-720e-41d0-d7e4-d2fd5e36b59f" }, "outputs": [], "source": [ "# Train the model\n", "model.fit(x_train, y_train,\n", " batch_size=batch_size,\n", " epochs=n_epochs,\n", " validation_data=(x_test, y_test),\n", " validation_batch_size=test_batch_size)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "PomeYxKcdbzo" }, "outputs": [], "source": [ "if save_model:\n", " model.save_weights(\"mnist_cnn_tf.ckpt\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def visualize_and_predict_tf(model, x_test, y_test, index=0):\n", " # Select a single image and its label by index\n", " img = x_test[index]\n", " label = y_test[index]\n", "\n", " # Visualize the image\n", " plt.imshow(img.squeeze(), cmap='gray') # Reshape for grayscale image\n", " plt.title(f'Actual Label: {np.argmax(label)}')\n", " plt.show()\n", "\n", " # Run inference\n", " img = np.expand_dims(img, 0) # Add batch dimension\n", " predictions = model.predict(img)\n", " pred_label = np.argmax(predictions[0])\n", "\n", " print(f'Predicted Label: {pred_label}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "visualize_and_predict_tf(model, x_test, y_test, index=randint(0, len(x_test)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "accelerator": "GPU", "colab": { "authorship_tag": "ABX9TyOILfYw6UXSzhAdYkLRoPJg", "include_colab_link": true, "name": "MNIST - Tensorflow.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.18" } }, "nbformat": 4, "nbformat_minor": 1 }