CSC 578 Spring 2024 HW#5

578hw5_Check1.ipynb -- Check application code

You can run this code, the whole file, as is, with NN578_network2.ipynb, to ensure your system is set up correctly.

Check your output with what's shown in the file.

In [1]:
## nt: Code piece to mount my Google Drive
from google.colab import drive
drive.mount("/content/drive") # my Google Drive root directory will be mapped here
Mounted at /content/drive
In [3]:
# nt: Change the working directory to the work directory (where the code file is).
import os
thisdir = '/content/drive/My Drive/CSC578_Spring2024/HW#5'
os.chdir(thisdir)

# Ensure the files are there (in the folder)
!pwd
/content/drive/My Drive/CSC578_Spring2024/HW#5

(1) Tiny experiment with One-training-One-test Iris data

In [4]:
# First install this library so that we can import code from other Notebooks
## 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
!pip install import-ipynb
import import_ipynb
Collecting import-ipynb
  Downloading import_ipynb-0.1.4-py3-none-any.whl (4.1 kB)
Requirement already satisfied: IPython in /usr/local/lib/python3.10/dist-packages (from import-ipynb) (7.34.0)
Requirement already satisfied: nbformat in /usr/local/lib/python3.10/dist-packages (from import-ipynb) (5.10.4)
Requirement already satisfied: setuptools>=18.5 in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (67.7.2)
Collecting jedi>=0.16 (from IPython->import-ipynb)
  Downloading jedi-0.19.1-py2.py3-none-any.whl (1.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 7.0 MB/s eta 0:00:00
Requirement already satisfied: decorator in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (4.4.2)
Requirement already satisfied: pickleshare in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (0.7.5)
Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (5.7.1)
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)
Requirement already satisfied: pygments in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (2.16.1)
Requirement already satisfied: backcall in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (0.2.0)
Requirement already satisfied: matplotlib-inline in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (0.1.7)
Requirement already satisfied: pexpect>4.3 in /usr/local/lib/python3.10/dist-packages (from IPython->import-ipynb) (4.9.0)
Requirement already satisfied: fastjsonschema>=2.15 in /usr/local/lib/python3.10/dist-packages (from nbformat->import-ipynb) (2.19.1)
Requirement already satisfied: jsonschema>=2.6 in /usr/local/lib/python3.10/dist-packages (from nbformat->import-ipynb) (4.19.2)
Requirement already satisfied: jupyter-core!=5.0.*,>=4.12 in /usr/local/lib/python3.10/dist-packages (from nbformat->import-ipynb) (5.7.2)
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)
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)
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)
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)
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)
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)
Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.10/dist-packages (from pexpect>4.3->IPython->import-ipynb) (0.7.0)
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)
Installing collected packages: jedi, import-ipynb
Successfully installed import-ipynb-0.1.4 jedi-0.19.1
In [5]:
# import the class Network from "NN578_network.ipynb"
import NN578_network2 as network2
import numpy as np

# Load the data (using the custom function defined in the class Notebook).
iris_data = network2.my_load_csv('iris.csv', 4, 3)
importing Jupyter notebook from NN578_network2.ipynb
In [6]:
# Test with one-data Iris data
inst1 = (np.array([5.7, 3, 4.2, 1.2]), np.array([0., 1., 0.]))
x1 = np.reshape(inst1[0], (4, 1))
y1 = np.reshape(inst1[1], (3, 1))
sample1 = [(x1, y1)]
inst2 = (np.array([4.8, 3.4, 1.6, 0.2]), np.array([1., 0., 0.]))
x2 = np.reshape(inst2[0], (4, 1))
y2 = np.reshape(inst2[1], (3, 1))
sample2 = [(x2, y2)]

# Create a network from the saved network
net4 = network2.Network.load_network("iris-423.dat")
net4.set_model_parameters(cost=network2.QuadraticCost,
                          act_hidden=network2.Sigmoid,
                          act_output=network2.Sigmoid)

net4.SGD(sample1, 2, 1, 1.0, evaluation_data=sample2)
Training 2 epochs complete.

Out[6]:
([0.26673128660052947, 0.2107866577006649],
 [1, 1],
 [0.3244002758397572, 0.37647122809828154],
 [0, 0])

Load the iris_train, iris_test datasets

In [7]:
# Load the iris train-test (separate) data files
iris_train = network2.my_load_csv('iris-train-2.csv', 4, 3)
iris_test = network2.my_load_csv('iris-test-2.csv', 4, 3)
# Dataset lengths as golbal variables...
train_size = len(iris_train)
test_size = len(iris_test)
In [8]:
# Function to display results in a pandas dataframe
import pandas as pd

def display_results(train_cost, train_acc, test_cost, test_acc):
    # Convert a corrent_count to an accuracy percentage
    train_acc = [acc / train_size for acc in train_acc]
    if len(test_cost) > 0:
        # Convert a corrent_count to an accuracy percentage
        test_acc = [acc / test_size for acc in test_acc]
        df = pd.DataFrame(list(zip(train_cost, train_acc, test_cost, test_acc)),
                         columns = ['train_cost', 'train_acc', 'test_cost', 'test_acc'])
    else:
        df = pd.DataFrame(list(zip(train_cost, train_acc)),
                         columns = ['train_cost', 'train_acc'])

    print(df)

(0) Sigmoid + Sigmoid + QuadraticCost

In [9]:
# Load/reset the network (from the saved network)
net = network2.Network.load_network("iris-423.dat")

# Set the model hyperparameters (where all models parameters are explicit)
net.set_model_parameters(cost=network2.QuadraticCost,
                         act_hidden=network2.Sigmoid,
                         act_output=network2.Sigmoid)

# Train the network (where all compilation parameters are taking DEFAULT values)
train_cost, train_acc, test_cost, test_acc = \
    net.SGD(iris_train, 30, 8, 1.5, evaluation_data=iris_test)

display_results(train_cost, train_acc, test_cost, test_acc)
Training 30 epochs complete.

    train_cost  train_acc  test_cost  test_acc
0     0.344393   0.333333   0.344382  0.333333
1     0.340949   0.333333   0.341009  0.333333
2     0.298419   0.333333   0.298796  0.333333
3     0.258159   0.666667   0.258992  0.666667
4     0.234100   0.666667   0.235112  0.666667
5     0.218808   0.666667   0.219791  0.666667
6     0.208522   0.666667   0.209426  0.666667
7     0.200634   0.666667   0.201501  0.666667
8     0.193802   0.666667   0.194732  0.666667
9     0.189709   0.666667   0.190617  0.666667
10    0.187111   0.666667   0.187939  0.666667
11    0.185153   0.666667   0.185898  0.666667
12    0.183615   0.666667   0.184288  0.666667
13    0.182369   0.666667   0.182988  0.666667
14    0.181323   0.666667   0.181915  0.666667
15    0.180418   0.666667   0.181029  0.666667
16    0.179626   0.666667   0.180337  0.666667
17    0.178966   0.666667   0.179901  0.666667
18    0.178490   0.666667   0.179790  0.666667
19    0.178215   0.666667   0.179951  0.666667
20    0.178240   0.657143   0.180432  0.644444
21    0.178861   0.657143   0.181479  0.644444
22    0.173678   0.666667   0.174211  0.666667
23    0.173800   0.666667   0.172278  0.666667
24    0.173686   0.666667   0.172145  0.666667
25    0.175193   0.666667   0.174264  0.666667
26    0.174433   0.666667   0.173232  0.666667
27    0.175653   0.666667   0.175029  0.666667
28    0.172012   0.666667   0.169896  0.666667
29    0.177043   0.666667   0.177078  0.666667
In [ ]: