{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true, "id": "-OzzKm9QFurU" }, "source": [ "# CSC 578 Spring 2024 HW\\#5\n", "## 578hw5_Check1.ipynb -- Check application code\n", "### You can run this code, the whole file, _as is_, with NN578_network2.ipynb, to ensure your system is set up correctly. \n", "Check your output with what's shown in the file." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "912aoxK4GslG", "outputId": "b313e8fa-f9fb-4430-99dd-a0c4343d8162" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Mounted at /content/drive\n" ] } ], "source": [ "## nt: Code piece to mount my Google Drive\n", "from google.colab import drive\n", "drive.mount(\"/content/drive\") # my Google Drive root directory will be mapped here" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9eHuf_ORGz8-", "outputId": "7e27ef87-c399-402c-c41e-ad3ca3048df9" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "/content/drive/My Drive/CSC578_Spring2024/HW#5\n" ] } ], "source": [ "# nt: Change the working directory to the work directory (where the code file is).\n", "import os\n", "thisdir = '/content/drive/My Drive/CSC578_Spring2024/HW#5'\n", "os.chdir(thisdir)\n", "\n", "# Ensure the files are there (in the folder)\n", "!pwd" ] }, { "cell_type": "markdown", "metadata": { "id": "tVNhW15zFurc" }, "source": [ "## (1) Tiny experiment with One-training-One-test Iris data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "collapsed": true, "id": "fJtGrbl-Fure", "outputId": "347a7c35-c412-4c21-afbe-cc9b06821f35" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Collecting import-ipynb\n", " Downloading import_ipynb-0.1.4-py3-none-any.whl (4.1 kB)\n", "Requirement already satisfied: IPython in /usr/local/lib/python3.10/dist-packages (from import-ipynb) (7.34.0)\n", "Requirement already satisfied: nbformat in /usr/local/lib/python3.10/dist-packages (from import-ipynb) (5.10.4)\n", "Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (67.7.2)\n", "Collecting jedi>=0.16 (from IPython->import-ipynb)\n", " Downloading jedi-0.19.1-py2.py3-none-any.whl (1.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (4.4.2)\n", "Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (0.7.5)\n", "Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (5.7.1)\n", "Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (3.0.43)\n", "Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (2.16.1)\n", "Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (0.2.0)\n", "Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (0.1.7)\n", "Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (4.9.0)\n", "Requirement already satisfied: fastjsonschema>=2.15 in /usr/local/lib/python3.10/dist-packages (from nbformat->import-ipynb) (2.19.1)\n", "Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.10/dist-packages (from nbformat->import-ipynb) (4.19.2)\n", "Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /usr/local/lib/python3.10/dist-packages (from nbformat->import-ipynb) (5.7.2)\n", "Requirement already satisfied: parso<0.9.0,>=0.8.3 in /usr/local/lib/python3.10/dist-packages (from jedi>=0.16->IPython->import-ipynb) (0.8.4)\n", "Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->import-ipynb) (23.2.0)\n", "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->import-ipynb) (2023.12.1)\n", "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->import-ipynb) (0.35.0)\n", "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=2.6->nbformat->import-ipynb) (0.18.0)\n", "Requirement already satisfied: platformdirs>=2.5 in /usr/local/lib/python3.10/dist-packages (from jupyter-core!=5.0.*,>=4.12->nbformat->import-ipynb) (4.2.1)\n", "Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->IPython->import-ipynb) (0.7.0)\n", "Requirement already satisfied: wcwidth in /usr/local/lib/python3.10/dist-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->IPython->import-ipynb) (0.2.13)\n", "Installing collected packages: jedi, import-ipynb\n", "Successfully installed import-ipynb-0.1.4 jedi-0.19.1\n" ] } ], "source": [ "# First install this library so that we can import code from other Notebooks\n", "## https://newbedev.com/how-to-import-functions-of-a-jupyter-notebook-into-another-jupyter-notebook-in-google-colab#:~:text=How%20to%20import%20functions%20of%20a%20jupyter%20notebook,mount%20your%20google%20drive%20to%20access%20your%20xxx.ipynb\n", "!pip install import-ipynb\n", "import import_ipynb" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bmnCFtaiFurk", "outputId": "edf88c5e-5091-49d7-d622-d456737b293f" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "importing Jupyter notebook from NN578_network2.ipynb\n" ] } ], "source": [ "# import the class Network from \"NN578_network.ipynb\"\n", "import NN578_network2 as network2\n", "import numpy as np\n", "\n", "# Load the data (using the custom function defined in the class Notebook).\n", "iris_data = network2.my_load_csv('iris.csv', 4, 3)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vEMGPaqPFurl", "outputId": "b4965441-0482-453d-e3ee-2c78a5abfaac" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Training 2 epochs complete.\n", "\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "([0.26673128660052947, 0.2107866577006649],\n", " [1, 1],\n", " [0.3244002758397572, 0.37647122809828154],\n", " [0, 0])" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "# Test with one-data Iris data\n", "inst1 = (np.array([5.7, 3, 4.2, 1.2]), np.array([0., 1., 0.]))\n", "x1 = np.reshape(inst1[0], (4, 1))\n", "y1 = np.reshape(inst1[1], (3, 1))\n", "sample1 = [(x1, y1)]\n", "inst2 = (np.array([4.8, 3.4, 1.6, 0.2]), np.array([1., 0., 0.]))\n", "x2 = np.reshape(inst2[0], (4, 1))\n", "y2 = np.reshape(inst2[1], (3, 1))\n", "sample2 = [(x2, y2)]\n", "\n", "# Create a network from the saved network\n", "net4 = network2.Network.load_network(\"iris-423.dat\")\n", "net4.set_model_parameters(cost=network2.QuadraticCost,\n", " act_hidden=network2.Sigmoid,\n", " act_output=network2.Sigmoid)\n", "\n", "net4.SGD(sample1, 2, 1, 1.0, evaluation_data=sample2)" ] }, { "cell_type": "markdown", "metadata": { "id": "bdiqbgW-Furp" }, "source": [ "### Load the iris_train, iris_test datasets" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "KuuSA1BXFurq" }, "outputs": [], "source": [ "# Load the iris train-test (separate) data files\n", "iris_train = network2.my_load_csv('iris-train-2.csv', 4, 3)\n", "iris_test = network2.my_load_csv('iris-test-2.csv', 4, 3)\n", "# Dataset lengths as golbal variables...\n", "train_size = len(iris_train)\n", "test_size = len(iris_test)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "OhN0YgO_oRdU" }, "outputs": [], "source": [ "# Function to display results in a pandas dataframe\n", "import pandas as pd\n", "\n", "def display_results(train_cost, train_acc, test_cost, test_acc):\n", " # Convert a corrent_count to an accuracy percentage\n", " train_acc = [acc / train_size for acc in train_acc]\n", " if len(test_cost) > 0:\n", " # Convert a corrent_count to an accuracy percentage\n", " test_acc = [acc / test_size for acc in test_acc]\n", " df = pd.DataFrame(list(zip(train_cost, train_acc, test_cost, test_acc)),\n", " columns = ['train_cost', 'train_acc', 'test_cost', 'test_acc'])\n", " else:\n", " df = pd.DataFrame(list(zip(train_cost, train_acc)),\n", " columns = ['train_cost', 'train_acc'])\n", "\n", " print(df)" ] }, { "cell_type": "markdown", "metadata": { "id": "XTXwAe0zFurr" }, "source": [ "## (0) Sigmoid + Sigmoid + QuadraticCost" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "C4fDxbpPFurr", "outputId": "378edc98-0eba-4424-d7d4-ebcf91c606fa" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Training 30 epochs complete.\n", "\n", " train_cost train_acc test_cost test_acc\n", "0 0.344393 0.333333 0.344382 0.333333\n", "1 0.340949 0.333333 0.341009 0.333333\n", "2 0.298419 0.333333 0.298796 0.333333\n", "3 0.258159 0.666667 0.258992 0.666667\n", "4 0.234100 0.666667 0.235112 0.666667\n", "5 0.218808 0.666667 0.219791 0.666667\n", "6 0.208522 0.666667 0.209426 0.666667\n", "7 0.200634 0.666667 0.201501 0.666667\n", "8 0.193802 0.666667 0.194732 0.666667\n", "9 0.189709 0.666667 0.190617 0.666667\n", "10 0.187111 0.666667 0.187939 0.666667\n", "11 0.185153 0.666667 0.185898 0.666667\n", "12 0.183615 0.666667 0.184288 0.666667\n", "13 0.182369 0.666667 0.182988 0.666667\n", "14 0.181323 0.666667 0.181915 0.666667\n", "15 0.180418 0.666667 0.181029 0.666667\n", "16 0.179626 0.666667 0.180337 0.666667\n", "17 0.178966 0.666667 0.179901 0.666667\n", "18 0.178490 0.666667 0.179790 0.666667\n", "19 0.178215 0.666667 0.179951 0.666667\n", "20 0.178240 0.657143 0.180432 0.644444\n", "21 0.178861 0.657143 0.181479 0.644444\n", "22 0.173678 0.666667 0.174211 0.666667\n", "23 0.173800 0.666667 0.172278 0.666667\n", "24 0.173686 0.666667 0.172145 0.666667\n", "25 0.175193 0.666667 0.174264 0.666667\n", "26 0.174433 0.666667 0.173232 0.666667\n", "27 0.175653 0.666667 0.175029 0.666667\n", "28 0.172012 0.666667 0.169896 0.666667\n", "29 0.177043 0.666667 0.177078 0.666667\n" ] } ], "source": [ "# Load/reset the network (from the saved network)\n", "net = network2.Network.load_network(\"iris-423.dat\")\n", "\n", "# Set the model hyperparameters (where all models parameters are explicit)\n", "net.set_model_parameters(cost=network2.QuadraticCost,\n", " act_hidden=network2.Sigmoid,\n", " act_output=network2.Sigmoid)\n", "\n", "# Train the network (where all compilation parameters are taking DEFAULT values)\n", "train_cost, train_acc, test_cost, test_acc = \\\n", " net.SGD(iris_train, 30, 8, 1.5, evaluation_data=iris_test)\n", "\n", "display_results(train_cost, train_acc, test_cost, test_acc)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "GA2j8ZNiFurt" }, "outputs": [], "source": [] } ], "metadata": { "colab": { "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.12" } }, "nbformat": 4, "nbformat_minor": 0 }