{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "uUecO5XzYw_i" }, "source": [ "## 578hw3_Check1_CoLab.ipynb (Spring 2024)\n", "### **NOTE**: This is a version for Google CoLab.\n", "#### Run all cells in this file with the original \"NN578_network.ipynb\" to ensure the code runs on your platform." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wnHiEX6pZu_G", "outputId": "72c5cdce-615c-4f41-d77f-e2d47d267b0e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "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": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "FEv7AacjZwqx", "outputId": "5fc3e890-9073-42fa-ad08-be9748ed9367" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/content/drive/My Drive/CSC578_Fall2023/HW#3\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_Fall2024/HW#3'\n", "os.chdir(thisdir)\n", "\n", "# Ensure the files are there (in the folder)\n", "!pwd" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lGaWYSexYw_t", "outputId": "f9fff734-9005-4fc4-df8b-6c5207314a38" }, "outputs": [ { "name": "stdout", "output_type": "stream", "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[31m13.9 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.34.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.0)\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", "!pip install import-ipynb\n", "import import_ipynb" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "X73HQ6nzYw_x", "outputId": "fea24f13-4d88-4363-b790-7b47b478c6c1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "importing Jupyter notebook from NN578_network.ipynb\n" ] } ], "source": [ "# import the class Network from \"NN578_network_2023fall.ipynb\"\n", "#from ipynb.fs.defs.NN578_network_2023fall import Network as network\n", "import NN578_network as network_nb\n", "import numpy as np\n", "\n", "# Load the data (using the custom function defined in the class Notebook).\n", "iris_data = network_nb.my_load_csv('iris-3.csv', 4, 3)\n", "iris_train = iris_data[:105]\n", "iris_test = iris_data[105:]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "T7GyVDNfYw_y", "outputId": "76b1c3c7-e031-4dd4-e7bd-7f40d4ff2942" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 0: train acc 0.3333\n", "Epoch 1: train acc 0.6667\n", "Epoch 2: train acc 0.6667\n", "\n", "****************************************************\n", "!! CHECK THIS: Initial activations shape -> [(4, 1)]\n", "****************************************************\n" ] } ], "source": [ "# Create a network from the saved network\n", "net1 = network_nb.Network.load_network(\"iris-423.dat\")\n", "\n", "# Train the network for 3 epochs, with minibatch size 8, eta=2.0 and no testset.\n", "net1.SGD(iris_data, 3, 8, 2.5)\n", "print ('\\n****************************************************')\n", "print ('!! CHECK THIS: Initial activations shape -> {}'.format(net1.init_acts_shape))\n", "print ('****************************************************')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6AwnD4dKYw_0", "outputId": "5e4464d0-0237-42cc-fa3b-358f887af2eb" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 0: train acc 0.3429, valid acc 0.3111\n", "Epoch 1: train acc 0.3429, valid acc 0.3111\n", "Epoch 2: train acc 0.6762, valid acc 0.6444\n" ] } ], "source": [ "# Re-load the saved network and run it again. This time using the test set.\n", "net2 = network_nb.Network.load_network(\"iris-423.dat\")\n", "net2.SGD(iris_train, 3, 8, 2.5, iris_test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "MQ3F2E99dGXf" }, "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.13" } }, "nbformat": 4, "nbformat_minor": 1 }