def setUp(self): """Prepares a Halfadder Neuron Network""" # Layer 1 Neurons: n1 = Neuron([12,12], Sigmoid().activate, bias=-18) n2 = Neuron([-12,-12], Sigmoid().activate, bias=6) n3 = Neuron([12,12], Sigmoid().activate, bias=-18) # Layer 2 Neurons: n4 = Neuron([-12,-12,0], Sigmoid().activate, bias=6) n5 = Neuron([0,0,12], Sigmoid().activate, bias=-6) # Layers l1 = NeuronLayer([n1,n2,n3]) l2 = NeuronLayer([n4,n5]) self.ntwrk = NeuronNetwork([l1,l2])
class Neuron_INVERT(unittest.TestCase): """Tests the Neuron class by building a INVERT logic gate Neuron""" def setUp(self): """Prepares a INVERT-type Neuron.""" self.INVERT_Neuron = Neuron([-12], Sigmoid().activate, bias=6) def test_INVERT_high(self): """Tests a scenario in which a Neuron - designed to be an INVERT port - returns a output as close as possible to high (1)""" outcome = self.INVERT_Neuron.activate([0]) self.assertAlmostEqual(outcome, 1, 2) def test_INVERT_low(self): """Tests a scenario in which a Neuron - designed to be an INVERT port - returns a output as close as possible to low (0)""" outcome = self.INVERT_Neuron.activate([1]) self.assertAlmostEqual(outcome, 0, 2)
class Neuron_NOR(unittest.TestCase): """Tests the Neuron class by building a NOR logic gate Neuron""" def setUp(self): """Prepares a NOR-type Neuron""" self.NOR_Neuron = Neuron([-12, -12, -12], Sigmoid().activate, bias=6) def test_NOR_high(self): """Tests a scenario in which a Neuron - designed to be an NOR port - returns a output as close as possible to high (1)""" outcome = self.NOR_Neuron.activate([0, 0, 0]) self.assertAlmostEqual(outcome, 1, 2) def test_NOR_low(self): """Tests some scenarios in which a Neuron - designed to be an NOR port - returns a output as close as possible to low (0)""" outcome = self.NOR_Neuron.activate([1, 0, 0]) self.assertAlmostEqual(outcome, 0, 2) outcome = self.NOR_Neuron.activate([0, 1, 0]) self.assertAlmostEqual(outcome, 0, 2) outcome = self.NOR_Neuron.activate([1, 1, 0]) self.assertAlmostEqual(outcome, 0, 2) outcome = self.NOR_Neuron.activate([0, 0, 1]) self.assertAlmostEqual(outcome, 0, 2) outcome = self.NOR_Neuron.activate([1, 0, 1]) self.assertAlmostEqual(outcome, 0, 2) outcome = self.NOR_Neuron.activate([0, 1, 1]) self.assertAlmostEqual(outcome, 0, 2)
from classes.Neuron import Neuron from classes.NeuronLayer import NeuronLayer from classes.NeuronNetwork import NeuronNetwork from classes.Activation import Sigmoid # Layer 1 n1 = Neuron([0.2, -0.4], Sigmoid().activate) n2 = Neuron([0.7, 0.1], Sigmoid().activate) # Layer 2 n3 = Neuron([0.6, 0.9], Sigmoid().activate) l1 = NeuronLayer([n1, n2]) l2 = NeuronLayer([n3]) ntwrk = NeuronNetwork([l1, l2], 1) x = [[0, 0], [0, 1], [1, 0], [1, 1]] y = [[0], [1], [1], [0]] ntwrk.train(x, y, 40000, 0.001) print(ntwrk.__str__()) print("MSE network:") print(ntwrk.error(x, y)) print("Should be as close as possible to high (1)") print("[0,1] gives:") print(ntwrk.feed_forward([0, 1])) print("[1,0] gives:") print(ntwrk.feed_forward([1, 0])) print("Should be as close as possible to low (0)")
from classes.Neuron import Neuron from classes.NeuronLayer import NeuronLayer from classes.NeuronNetwork import NeuronNetwork from classes.Activation import Sigmoid n1 = Neuron([0.0,0.1],Sigmoid().activate) n2 = Neuron([0.2,0.3],Sigmoid().activate) n3 = Neuron([0.4,0.5],Sigmoid().activate) n4 = Neuron([0.6,0.7,0.8],Sigmoid().activate) n5 = Neuron([0.9,1.0,1.1],Sigmoid().activate) l1 = NeuronLayer([n1,n2,n3]) l2 = NeuronLayer([n4,n5]) ntwrk = NeuronNetwork([l1,l2],0.5) x = [[0,0],[1,0],[0,1],[1,1]] y = [[0,0],[1,0],[1,0],[0,1]] ntwrk.train(x,y,80000,0.001) print(ntwrk.__str__()) print("MSE network:") print(ntwrk.error(x,y)) print("Output should be close to [0,0]") print(ntwrk.feed_forward([0,0])) print("Output should be close to [1,0]") print(ntwrk.feed_forward([1,0])) print(ntwrk.feed_forward([0,1]))
def setUp(self): """Prepares a OR-type Neuron""" self.OR_Neuron = Neuron([12, 12], Sigmoid().activate, bias=-6)
def setUp(self): """Prepares a AND-type Neuron""" self.AND_Neuron = Neuron([12, 12], Sigmoid().activate, bias=-18)
def setUp(self): """Prepares a NOR-type Neuron""" self.NOR_Neuron = Neuron([-12, -12, -12], Sigmoid().activate, bias=6)
from classes.Activation import Sigmoid from classes.Neuron import Neuron AND_Neuron_wrong = Neuron([0.5, 0.5], Sigmoid().activate, bias= -1) print("Should be high (1):") outcome = AND_Neuron_wrong.activate([1, 1]) print("Input [1,1] gives {}".format(outcome)) print("") print("Should be low (0):") outcome = AND_Neuron_wrong.activate([1, 0]) print("Input [1,0] gives {}".format(outcome)) outcome = AND_Neuron_wrong.activate([0, 1]) print("Input [0,1] gives {}".format(outcome)) outcome = AND_Neuron_wrong.activate([0, 0]) print("Input [0,0] gives {}".format(outcome))
from classes.Activation import Sigmoid from classes.Neuron import Neuron INVERTER_Neuron_wrong = Neuron([-1],Sigmoid().activate) print("Should be high (1):") outcome = INVERTER_Neuron_wrong.activate([0]) print("Input [0] gives {}".format(outcome)) print("") print("Should be low (0):") outcome = INVERTER_Neuron_wrong.activate([1]) print("Input [1] gives {}".format(outcome))
def setUp(self): """Prepares a INVERT-type Neuron.""" self.INVERT_Neuron = Neuron([-12], Sigmoid().activate, bias=6)