from layer import Layer
from network import Network
from termcolor import colored
from typing import List
class Tests:
def __init__(self):
"""
Initializes modules required for the tests
"""
[Link] = Layer(2, 3)
[Link] = Network([2, 3, 2])
def test_layer_output_calculation(self):
"""
Test for the calculate outputs function for the individual layers.
Currently configured for Sigmoid Squishification activation function
"""
# Setting layer weights and biases to predetermined values
[Link] = [
[1.0, 0.5],
[0.75, 1.5],
[0.0, 2.0]
]
[Link] = [0.5, 1.0, 1.5]
# Setting the inputs of the layer
inputs = [2.0, 0.5]
# Calculating what the module thinks the outputs are
outputs = [Link].calculate_outputs(inputs)
# Rounding outputs to 5 decimal places
for i in range(len(outputs)):
outputs[i] = round(outputs[i], 5)
# Defining precomputed, correct outputs
expected_outputs = [0.93991, 0.96267, 0.92414]
# Checking if the output of the function in question and precomputed,
correct outputs match
self.print_test_results("LAYER CALCULATE OUTPUTS", outputs,
expected_outputs)
def test_network_feed_inputs(self):
"""
Test for feeding the inputs to the network.
"""
# Defining inputs to be fed into the network
inputs = [2.0, 1.0]
# Feeding inputs into the network
[Link].feed_inputs(inputs)
# Checking if the inputs have been fed to the network successfully
self.print_test_results("NETWORK FEED INPUTS", [Link], inputs)
def test_network_run_calculations(self):
"""
Test for the entire neural network. Currently configured for sigmoid
squishification activation function
"""
# Setting Network weights and biases to predetermined values
[Link][0].weights = [
[1.0, 0.5],
[0.75, 1.5],
[0.0, 2.0]
]
[Link][0].biases = [0.5, 1.0, 1.5]
[Link][1].weights = [
[0.5, 1.0, 2.5],
[0.0, 0.5, -1.5],
]
[Link][1].biases = [0.0, -2.0]
# Defining inputs to be fed to the network
inputs = [2.0, 0.5]
# Defining precomputed, correct values for what the function being tested
should produce
expected_outputs = [0.97686, 0.05191]
# Feeding the inputs into the network
[Link].feed_inputs(inputs)
# Storing the function being tested's values
outputs = [Link].run_calculations()
# Rounding values to 5 decimal places
for i in range(len(outputs)):
outputs[i] = round(outputs[i], 5)
# Checking if the output of the function in question, and precomputed,
correct outputs match
self.print_test_results("NETWORK RUN CALCULATIONS", outputs,
expected_outputs)
def test_activation_function(self):
"""
Test for the activation function. Currently configured to test sigmoid
squishification
"""
# Defining the various inputs that the activation function will be tested
for
x = [10.0, 0.0, 5.0, 3.2]
# Calculating the outputs of the activation function and rounding them to 5
decimal places
for i in range(len(x)):
x[i] = round([Link].activation_function(x[i]), 5)
# Defining precomputed and correct values for function
expectation = [0.99995, 0.5, 0.99331, 0.96083]
# Checking if the output of the function in question and precomputed,
correct outputs match
self.print_test_results("LAYER ACTIVATION FUNCTION", x, expectation)
def print_test_results(self, name: str, result, expected_result):
"""
Generic function for printing the test results of a test. Parameters in
order:
1. name : Specifies the name of the test, will be printed at the
top
2. result : The result that the functionality being tested gave
3. expected_result : The result that the functionality being tested should
give for the test to pass
If the result equals the expected result this function will print that it
passed, otherwise,
this function will print that the test failed failed.
"""
print("---------------------------------------")
if (result == expected_result):
print(colored(f"TEST : {name} : PASSED", "cyan", attrs=['bold']))
else:
print(colored(f"TEST : {name} : FAILED", "red", attrs=['bold']))
print("---------------------------------------")
print(F"EXPECTED OUTPUT : {expected_result}")
print(f"GIVEN OUTPUT : {result}")
print("---------------------------------------\n")