{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "mnist-rnn.ipynb", "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "4kyB8MUKn7Vh" }, "source": [ "# MNIST with Recurrent Neural Networks" ] }, { "cell_type": "markdown", "metadata": { "id": "YyFM4icCoAaX" }, "source": [ "## Prerequisites\n", "\n", "Install the following packages" ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "u63awVMPoD7Y", "outputId": "ca3297ad-339c-4483-afdc-bff00b9dea1b" }, "source": [ "! pip3 install cloudmesh-installer\n", "! pip3 install cloudmesh-common" ], "execution_count": 2, "outputs": [ { "output_type": "stream", "text": [ "Requirement already satisfied: cloudmesh-installer in /usr/local/lib/python3.6/dist-packages (4.4.19)\n", "Requirement already satisfied: docopt in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (0.6.2)\n", "Requirement already satisfied: bump2version==1.0.0 in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (1.0.0)\n", "Requirement already satisfied: coverage in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (3.7.1)\n", "Requirement already satisfied: oyaml in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (1.0)\n", "Requirement already satisfied: humanize in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (0.5.1)\n", "Requirement already satisfied: pip-tools in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (4.5.1)\n", "Requirement already satisfied: pytest-cov in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (2.11.1)\n", "Requirement already satisfied: colorama in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (0.4.4)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (2.23.0)\n", "Requirement already satisfied: wheel in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (0.36.2)\n", "Requirement already satisfied: pytest in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (3.6.4)\n", "Requirement already satisfied: pipdeptree in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (2.0.0)\n", "Requirement already satisfied: tabulate in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (0.8.7)\n", "Requirement already satisfied: python-hostlist in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (1.21)\n", "Requirement already satisfied: cloudmesh-common in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (4.3.43)\n", "Requirement already satisfied: psutil in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (5.4.8)\n", "Requirement already satisfied: flake8 in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (3.8.4)\n", "Requirement already satisfied: ordered-set in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (4.0.2)\n", "Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from oyaml->cloudmesh-installer) (3.13)\n", "Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from pip-tools->cloudmesh-installer) (1.15.0)\n", "Requirement already satisfied: click>=7 in /usr/local/lib/python3.6/dist-packages (from pip-tools->cloudmesh-installer) (7.1.2)\n", "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->cloudmesh-installer) (2.10)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->cloudmesh-installer) (3.0.4)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->cloudmesh-installer) (1.24.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->cloudmesh-installer) (2020.12.5)\n", "Requirement already satisfied: pluggy<0.8,>=0.5 in /usr/local/lib/python3.6/dist-packages (from pytest->cloudmesh-installer) (0.7.1)\n", "Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from pytest->cloudmesh-installer) (53.0.0)\n", "Requirement already satisfied: atomicwrites>=1.0 in /usr/local/lib/python3.6/dist-packages (from pytest->cloudmesh-installer) (1.4.0)\n", "Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.6/dist-packages (from pytest->cloudmesh-installer) (20.3.0)\n", "Requirement already satisfied: py>=1.5.0 in /usr/local/lib/python3.6/dist-packages (from pytest->cloudmesh-installer) (1.10.0)\n", "Requirement already satisfied: more-itertools>=4.0.0 in /usr/local/lib/python3.6/dist-packages (from pytest->cloudmesh-installer) (8.7.0)\n", "Requirement already satisfied: pip>=6.0.0 in /usr/local/lib/python3.6/dist-packages (from pipdeptree->cloudmesh-installer) (19.3.1)\n", "Requirement already satisfied: simplejson in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common->cloudmesh-installer) (3.17.2)\n", "Requirement already satisfied: pyfiglet in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common->cloudmesh-installer) (0.8.post1)\n", "Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common->cloudmesh-installer) (2018.9)\n", "Requirement already satisfied: python-dateutil in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common->cloudmesh-installer) (2.8.1)\n", "Requirement already satisfied: pathlib in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common->cloudmesh-installer) (1.0.1)\n", "Requirement already satisfied: importlib-metadata; python_version < \"3.8\" in /usr/local/lib/python3.6/dist-packages (from flake8->cloudmesh-installer) (3.4.0)\n", "Requirement already satisfied: pycodestyle<2.7.0,>=2.6.0a1 in /usr/local/lib/python3.6/dist-packages (from flake8->cloudmesh-installer) (2.6.0)\n", "Requirement already satisfied: pyflakes<2.3.0,>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from flake8->cloudmesh-installer) (2.2.0)\n", "Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from flake8->cloudmesh-installer) (0.6.1)\n", "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/dist-packages (from importlib-metadata; python_version < \"3.8\"->flake8->cloudmesh-installer) (3.4.0)\n", "Requirement already satisfied: typing-extensions>=3.6.4; python_version < \"3.8\" in /usr/local/lib/python3.6/dist-packages (from importlib-metadata; python_version < \"3.8\"->flake8->cloudmesh-installer) (3.7.4.3)\n", "Requirement already satisfied: cloudmesh-common in /usr/local/lib/python3.6/dist-packages (4.3.43)\n", "Requirement already satisfied: humanize in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (0.5.1)\n", "Requirement already satisfied: tabulate in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (0.8.7)\n", "Requirement already satisfied: python-dateutil in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (2.8.1)\n", "Requirement already satisfied: pyfiglet in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (0.8.post1)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (2.23.0)\n", "Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (2018.9)\n", "Requirement already satisfied: colorama in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (0.4.4)\n", "Requirement already satisfied: simplejson in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (3.17.2)\n", "Requirement already satisfied: python-hostlist in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (1.21)\n", "Requirement already satisfied: psutil in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (5.4.8)\n", "Requirement already satisfied: oyaml in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (1.0)\n", "Requirement already satisfied: pathlib in /usr/local/lib/python3.6/dist-packages (from cloudmesh-common) (1.0.1)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil->cloudmesh-common) (1.15.0)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->cloudmesh-common) (3.0.4)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->cloudmesh-common) (1.24.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->cloudmesh-common) (2020.12.5)\n", "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->cloudmesh-common) (2.10)\n", "Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from oyaml->cloudmesh-common) (3.13)\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "RlBJ-XuWopPh" }, "source": [ "## Import Libraries" ] }, { "cell_type": "code", "metadata": { "id": "RylyDeO2oC4m" }, "source": [ "from __future__ import absolute_import\n", "from __future__ import division\n", "from __future__ import print_function\n", "\n", "import numpy as np\n", "import tensorflow as tf\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense, Activation, SimpleRNN\n", "from tensorflow.keras.utils import to_categorical, plot_model\n", "from tensorflow.keras.datasets import mnist\n", "from cloudmesh.common.StopWatch import StopWatch" ], "execution_count": 3, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "e2lUNBJxore4" }, "source": [ "## Download Data and Pre-Process" ] }, { "cell_type": "code", "metadata": { "id": "3X9sLwfUocZ6" }, "source": [ "StopWatch.start(\"data-load\")\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "StopWatch.stop(\"data-load\")\n", "\n", "\n", "StopWatch.start(\"data-pre-process\")\n", "num_labels = len(np.unique(y_train))\n", "\n", "\n", "y_train = to_categorical(y_train)\n", "y_test = to_categorical(y_test)\n", "\n", "\n", "image_size = x_train.shape[1]\n", "x_train = np.reshape(x_train,[-1, image_size, image_size])\n", "x_test = np.reshape(x_test,[-1, image_size, image_size])\n", "x_train = x_train.astype('float32') / 255\n", "x_test = x_test.astype('float32') / 255\n", "StopWatch.stop(\"data-pre-process\")\n", "\n", "input_shape = (image_size, image_size)\n", "batch_size = 128\n", "units = 256\n", "dropout = 0.2" ], "execution_count": 4, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "O8DwuhcaowYQ" }, "source": [ "## Define Model" ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "O4tidxRFofEj", "outputId": "8abfcea0-3cc5-44f5-d462-3cf37f09d888" }, "source": [ "StopWatch.start(\"compile\")\n", "model = Sequential()\n", "model.add(SimpleRNN(units=units,\n", " dropout=dropout,\n", " input_shape=input_shape, return_sequences=True))\n", "model.add(SimpleRNN(units=units,\n", " dropout=dropout,\n", " return_sequences=True))\n", "model.add(SimpleRNN(units=units,\n", " dropout=dropout,\n", " return_sequences=False))\n", "model.add(Dense(num_labels))\n", "model.add(Activation('softmax'))\n", "model.summary()\n", "plot_model(model, to_file='rnn-mnist.png', show_shapes=True)\n", "\n", "\n", "model.compile(loss='categorical_crossentropy',\n", " optimizer='sgd',\n", " metrics=['accuracy'])\n", "StopWatch.stop(\"compile\")" ], "execution_count": 5, "outputs": [ { "output_type": "stream", "text": [ "Model: \"sequential\"\n", "_________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "=================================================================\n", "simple_rnn (SimpleRNN) (None, 28, 256) 72960 \n", "_________________________________________________________________\n", "simple_rnn_1 (SimpleRNN) (None, 28, 256) 131328 \n", "_________________________________________________________________\n", "simple_rnn_2 (SimpleRNN) (None, 256) 131328 \n", "_________________________________________________________________\n", "dense (Dense) (None, 10) 2570 \n", "_________________________________________________________________\n", "activation (Activation) (None, 10) 0 \n", "=================================================================\n", "Total params: 338,186\n", "Trainable params: 338,186\n", "Non-trainable params: 0\n", "_________________________________________________________________\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "ZxW3sG8WoyYC" }, "source": [ "## Train" ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "SjT5wffFoiEg", "outputId": "9474f86a-21c8-4cb0-8299-1743ae48b075" }, "source": [ "StopWatch.start(\"train\")\n", "model.fit(x_train, y_train, epochs=1, batch_size=batch_size)\n", "StopWatch.stop(\"train\")" ], "execution_count": 6, "outputs": [ { "output_type": "stream", "text": [ "469/469 [==============================] - 127s 265ms/step - loss: 0.9463 - accuracy: 0.6935\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "E3jjqPJqoljt" }, "source": [ "## Test" ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2B1-bFcvokdd", "outputId": "6aff2c20-5e3c-4b1b-a052-a215d7efe356" }, "source": [ "StopWatch.start(\"evaluate\")\n", "loss, acc = model.evaluate(x_test, y_test, batch_size=batch_size)\n", "print(\"\\nTest accuracy: %.1f%%\" % (100.0 * acc))\n", "StopWatch.stop(\"evaluate\")\n", "\n", "StopWatch.benchmark()" ], "execution_count": 7, "outputs": [ { "output_type": "stream", "text": [ "79/79 [==============================] - 7s 80ms/step - loss: 0.2581 - accuracy: 0.9181\n", "\n", "Test accuracy: 91.8%\n", "\n", "+---------------------+------------------------------------------------------------------+\n", "| Attribute | Value |\n", "|---------------------+------------------------------------------------------------------|\n", "| BUG_REPORT_URL | \"https://bugs.launchpad.net/ubuntu/\" |\n", "| DISTRIB_CODENAME | bionic |\n", "| DISTRIB_DESCRIPTION | \"Ubuntu 18.04.5 LTS\" |\n", "| DISTRIB_ID | Ubuntu |\n", "| DISTRIB_RELEASE | 18.04 |\n", "| HOME_URL | \"https://www.ubuntu.com/\" |\n", "| ID | ubuntu |\n", "| ID_LIKE | debian |\n", "| NAME | \"Ubuntu\" |\n", "| PRETTY_NAME | \"Ubuntu 18.04.5 LTS\" |\n", "| PRIVACY_POLICY_URL | \"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy\" |\n", "| SUPPORT_URL | \"https://help.ubuntu.com/\" |\n", "| UBUNTU_CODENAME | bionic |\n", "| VERSION | \"18.04.5 LTS (Bionic Beaver)\" |\n", "| VERSION_CODENAME | bionic |\n", "| VERSION_ID | \"18.04\" |\n", "| cpu_count | 2 |\n", "| mem.active | 1.3 GiB |\n", "| mem.available | 11.6 GiB |\n", "| mem.free | 9.7 GiB |\n", "| mem.inactive | 1.5 GiB |\n", "| mem.percent | 8.5 % |\n", "| mem.total | 12.7 GiB |\n", "| mem.used | 978.6 MiB |\n", "| platform.version | #1 SMP Thu Jul 23 08:00:38 PDT 2020 |\n", "| python | 3.6.9 (default, Oct 8 2020, 12:12:24) |\n", "| | [GCC 8.4.0] |\n", "| python.pip | 19.3.1 |\n", "| python.version | 3.6.9 |\n", "| sys.platform | linux |\n", "| uname.machine | x86_64 |\n", "| uname.node | 8f16b3b1f784 |\n", "| uname.processor | x86_64 |\n", "| uname.release | 4.19.112+ |\n", "| uname.system | Linux |\n", "| uname.version | #1 SMP Thu Jul 23 08:00:38 PDT 2020 |\n", "| user | collab |\n", "+---------------------+------------------------------------------------------------------+\n", "\n", "+------------------+----------+---------+---------+---------------------+-------+--------------+--------+-------+-------------------------------------+\n", "| Name | Status | Time | Sum | Start | tag | Node | User | OS | Version |\n", "|------------------+----------+---------+---------+---------------------+-------+--------------+--------+-------+-------------------------------------|\n", "| data-load | failed | 0.36 | 0.36 | 2021-02-18 15:16:12 | | 8f16b3b1f784 | collab | Linux | #1 SMP Thu Jul 23 08:00:38 PDT 2020 |\n", "| data-pre-process | failed | 0.086 | 0.086 | 2021-02-18 15:16:12 | | 8f16b3b1f784 | collab | Linux | #1 SMP Thu Jul 23 08:00:38 PDT 2020 |\n", "| compile | failed | 0.51 | 0.51 | 2021-02-18 15:16:12 | | 8f16b3b1f784 | collab | Linux | #1 SMP Thu Jul 23 08:00:38 PDT 2020 |\n", "| train | failed | 126.612 | 126.612 | 2021-02-18 15:16:13 | | 8f16b3b1f784 | collab | Linux | #1 SMP Thu Jul 23 08:00:38 PDT 2020 |\n", "| evaluate | failed | 6.798 | 6.798 | 2021-02-18 15:18:19 | | 8f16b3b1f784 | collab | Linux | #1 SMP Thu Jul 23 08:00:38 PDT 2020 |\n", "+------------------+----------+---------+---------+---------------------+-------+--------------+--------+-------+-------------------------------------+\n", "\n", "# csv,timer,status,time,sum,start,tag,uname.node,user,uname.system,platform.version\n", "# csv,data-load,failed,0.36,0.36,2021-02-18 15:16:12,,8f16b3b1f784,collab,Linux,#1 SMP Thu Jul 23 08:00:38 PDT 2020\n", "# csv,data-pre-process,failed,0.086,0.086,2021-02-18 15:16:12,,8f16b3b1f784,collab,Linux,#1 SMP Thu Jul 23 08:00:38 PDT 2020\n", "# csv,compile,failed,0.51,0.51,2021-02-18 15:16:12,,8f16b3b1f784,collab,Linux,#1 SMP Thu Jul 23 08:00:38 PDT 2020\n", "# csv,train,failed,126.612,126.612,2021-02-18 15:16:13,,8f16b3b1f784,collab,Linux,#1 SMP Thu Jul 23 08:00:38 PDT 2020\n", "# csv,evaluate,failed,6.798,6.798,2021-02-18 15:18:19,,8f16b3b1f784,collab,Linux,#1 SMP Thu Jul 23 08:00:38 PDT 2020\n", "\n" ], "name": "stdout" } ] } ] }