コード例 #1
0
ファイル: test_XOR.py プロジェクト: SwagLag/Perceptrons
    def setUp(self):
        # Layer 1 Perceptrons:
        n1 = Perceptron([0.5, 0.5], Step(0).activate, "n1", bias=-1)
        n2 = Perceptron([-0.1, -0.1], Step(0).activate, "n2", bias=0)
        # Layer 2 Perceptrons:
        n3 = Perceptron([-1, -1], Step(0).activate, "n3", bias=0)

        l1 = PerceptronLayer(1, [n1, n2])
        l2 = PerceptronLayer(1, [n3])

        self.ntwrk = PerceptronNetwork([l1, l2])
コード例 #2
0
ファイル: test_halfadder.py プロジェクト: SwagLag/Perceptrons
    def setUp(self):
        n1 = Perceptron([0.5,0.5],Step(0).activate,"n1",bias=-1)
        n2 = Perceptron([-1,-1],Step(0).activate,"n2",bias=0)
        n3 = Perceptron([0.5,0.5],Step(0).activate,"n3",bias=-1)
        # Layer 2 Perceptrons:
        n4 = Perceptron([-1,-1,0],Step(0).activate,"n4",bias=0)
        n5 = Perceptron([0,0,1],Step(0).activate,"n5",bias=-1)

        l1 = PerceptronLayer(1,[n1,n2,n3])
        l2 = PerceptronLayer(2,[n4,n5])

        self.ntwrk = PerceptronNetwork([l1,l2])
コード例 #3
0
ファイル: test_OR.py プロジェクト: SwagLag/Perceptrons
class Perceptron_OR(unittest.TestCase):
    """Tests the Perceptron class by building a OR logic gate Perceptron"""

    def setUp(self):
        """Prepares a OR-type Perceptron"""
        self.OR_Perceptron = Perceptron([0.5, 0.5], Step(0).activate, "OR",bias=-0.5)

    def test_OR_high(self):
        """Tests some scenarios in which a Perceptron - designed to be an OR port - returns a high output (1)"""
        outcome = self.OR_Perceptron.activate([1, 1])
        self.assertEqual(outcome, 1)

        outcome = self.OR_Perceptron.activate([1, 0])
        self.assertEqual(outcome, 1)

        outcome = self.OR_Perceptron.activate([0, 1])
        self.assertEqual(outcome, 1)

    def test_OR_low(self):
        """Tests some scenarios in which a Perceptron - designed to be an OR port - returns a low output (0)"""
        outcome = self.OR_Perceptron.activate([0, 0])
        self.assertEqual(outcome, 0)
コード例 #4
0
class Perceptron_AND(unittest.TestCase):
    """Tests the Perceptron class by building a AND logic gate Perceptron"""
    def setUp(self):
        """Prepares a AND-type Perceptron"""
        self.AND_Perceptron = Perceptron([0.5, 0.5],
                                         Step(0).activate,
                                         "AND",
                                         bias=-1)

    def test_AND_high(self):
        """Tests a scenario in which a Perceptron - designed to be an AND port - returns a high output (1)"""
        outcome = self.AND_Perceptron.activate([1, 1])
        self.assertEqual(outcome, 1)

    def test_AND_low(self):
        """Tests some scenarios in which a Perceptron - designed to be an AND port - returns a low output (0)"""
        outcome = self.AND_Perceptron.activate([0, 0])
        self.assertEqual(outcome, 0)

        outcome = self.AND_Perceptron.activate([0, 1])
        self.assertEqual(outcome, 0)

        outcome = self.AND_Perceptron.activate([1, 0])
        self.assertEqual(outcome, 0)
コード例 #5
0
ファイル: test_OR.py プロジェクト: SwagLag/Perceptrons
 def setUp(self):
     """Prepares a OR-type Perceptron"""
     self.OR_Perceptron = Perceptron([0.5, 0.5], Step(0).activate, "OR",bias=-0.5)
コード例 #6
0
def listfilter(wantedclasses: List[int], x: List[Union[int, float]], y: List[int]):
    """Gets the trainingset that only involves the wanted classes."""
    finalx = []
    finaly = []
    for index in range(len(y)):
        if y[index] in wantedclasses:
            finalx.append(list(x[index]))
            finaly.append(y[index])
    return finalx, finaly

firstx, firsty = listfilter([0,1],x,y)

# Train dan het eerste perceptron! We hebben hier te maken met twee trainingssets,
# dus het probleem zou wel lineair op te lossen moeten zijn.

percep = Perceptron([random.random() for i in range(4)], Step().activate)
percep.update(firstx,firsty)

# Uncomment voor uitgebreide informatie
# print(percep.__str__())

print("End weights:")
print(percep.getweights())
print("End bias:")
print(percep.getbias())

# Train dan nu het tweede perceptron! Deze zou iets lastiger kunnen zijn, omdat we hier te maken
# hebben met drie klasses; we werken immers met Step() die alleen maar 0 of 1 kan geven.... Bovendien
# werken we in deze opdracht alleen maar met een enkele perceptron die getraind wordt. Als we een netwerk
# zouden maken zou dit geen probleem geweest zijn; dan hadden we voor elke klasse een node in de output-layer
# kunnen maken.
コード例 #7
0
 def setUp(self):
     """Prepares a AND-type Perceptron"""
     self.AND_Perceptron = Perceptron([0.5, 0.5],
                                      Step(0).activate,
                                      "AND",
                                      bias=-1)
コード例 #8
0
# Since the XOR gate solves a non-linear problem, we estimate that this perceptron will fail to initialise properly.
# In order to demonstrate this, we do **not** use the unittest module, as that would result in a lack of feedback
# as to why it went wrong. Instead, we will print the outcomes and show that the XOR port is not working as it should.

from classes.Perceptron import Perceptron
from classes.Activation import Step

import random
random.seed(1756708)

test1 = Perceptron([random.random() for x in range(2)], Step(0).activate)
test1.update([[0, 0], [0, 1], [1, 0], [1, 1]], [0, 1, 1, 0])

# Uncomment voor uitgebreide informatie
# print(test1.__str__())

print("End weights:")
print(test1.getweights())
print("End bias:")
print(test1.getbias())

print("Should be high (1):")
print("Input [1,0] gives:")
print(test1.activate([1, 0]))
print("Input [0,1] gives:")
print(test1.activate([0, 1]))
print("")
print("Should be low (0):")
print("Input [0,0] gives:")
print(test1.activate([0, 0]))
print("Input [1,1] gives:")