Ejemplo n.º 1
0
    def test_orgate(self):
        p = Perceptron([0.5, 0.5], -0.5)
        self.assertEqual(p.activate([0, 0]), 0)

        self.assertEqual(p.activate([1, 0]), 1)

        self.assertEqual(p.activate([0, 1]), 1)

        self.assertEqual(p.activate([1, 1]), 1)
Ejemplo n.º 2
0
 def test_activate(self):
     p = Perceptron(2)
     p.theta = 1.5
     p.w = [1, 1]
     self.assertEqual(p.activate([0, 0]), 0)
     self.assertEqual(p.activate([0, 1]), 0)
     self.assertEqual(p.activate([1, 0]), 0)
     self.assertEqual(p.activate([1, 1]), 1)
     self.assertEqual(p.activate([2, 0]), 1)
     self.assertEqual(p.activate([0, 2]), 1)
Ejemplo n.º 3
0
 def test_train(self):
     p = Perceptron(2)
     for i in range(100):
         p.train([1, 0], 0, 0.05)
         p.train([0, 1], 1, 0.05)
     self.assertAlmostEqual(p.activate([1, 0]), 0)
     self.assertAlmostEqual(p.activate([0, 1]), 1)
Ejemplo n.º 4
0
    def test_train(self):

        p = Perceptron(2)
        for i in range(100):
            p.train([1, 0], 0, 0.05)
            p.train([0, 1], 1, 0.05)
        self.assertAlmostEqual(p.activate([1, 0]), 0)
        self.assertAlmostEqual(p.activate([0, 1]), 1)
Ejemplo n.º 5
0
    def test_partypoort(self):
        p = Perceptron([0.6, 0.3, 0.2], -0.4)
        self.assertEqual(p.activate([0, 0, 0]), 0)

        self.assertEqual(p.activate([1, 0, 0]), 1)

        self.assertEqual(p.activate([0, 1, 0]), 0)

        self.assertEqual(p.activate([1, 1, 0]), 1)

        self.assertEqual(p.activate([0, 0, 1]), 0)

        self.assertEqual(p.activate([1, 0, 1]), 1)

        self.assertEqual(p.activate([0, 1, 1]), 1)

        self.assertEqual(p.activate([1, 1, 1]), 1)
Ejemplo n.º 6
0
    def test_norgate(self):
        p = Perceptron([-1, -1, -1], 0)
        self.assertEqual(p.activate([0, 0, 0]), 1)

        self.assertEqual(p.activate([1, 0, 0]), 0)

        self.assertEqual(p.activate([0, 1, 0]), 0)

        self.assertEqual(p.activate([1, 1, 0]), 0)

        self.assertEqual(p.activate([0, 0, 1]), 0)

        self.assertEqual(p.activate([1, 0, 1]), 0)

        self.assertEqual(p.activate([0, 1, 1]), 0)

        self.assertEqual(p.activate([1, 1, 1]), 0)
Ejemplo n.º 7
0
    def test_activate(self):
        p = Perceptron(2)
        p.w = [1, 1]
        p.theta = 1.5
        self.assertEqual(p.activate([0, 0]), 0)
        self.assertEqual(p.activate([1, 1]), 1)
        self.assertEqual(p.activate([0, 1]), 0)
        self.assertEqual(p.activate([1, 0]), 0)
        self.assertEqual(p.activate([2, 0]), 1)
        self.assertEqual(p.activate([0, 2]), 1)

        p.w = [0.1, -0.2]
        p.theta = -0.1
        self.assertEqual(p.activate([0, 0]), 1)
        self.assertEqual(p.activate([0, 2.5]), 0)
        self.assertEqual(p.activate([0.3, 0]), 1)
        self.assertEqual(p.activate([1.5, 0.5]), 1)
        self.assertEqual(p.activate([0.1, 1.5]), 0)

        p = Perceptron(5)
        p.w = [1, 2, 3, 4, 5]
        p.theta = 7
        self.assertEqual(p.activate([1, 1, 1, 0, 0]), 0)
        self.assertEqual(p.activate([0, 0, 0, 1, 1]), 1)
Ejemplo n.º 8
0
 def test_activate_on_perceptron_with_weights_equal_to_zero_returns_zero(self):
     perceptron = Perceptron(bias=0.0, weight_1=0.0, weight_2=0.0)
     activation = perceptron.activate(input_1=0.5, input_2=0.5)
     self.assertEqual(0.0, activation)
Ejemplo n.º 9
0
 def test_activate_works(self):
     perceptron = Perceptron(bias=0.5, weight_1=1.0, weight_2=0.8)
     activation = perceptron.activate(input_1=0.4, input_2=0.6)
     self.assertEqual(1.38, activation)
Ejemplo n.º 10
0
def test():
    p = Perceptron([0.1, 0.5, 0.1], 2)
    output = p.activate([0.0, 0.2, 0.1])
    print(output)
Ejemplo n.º 11
0
    def test_invert(self):
        p = Perceptron([-1], 0)
        self.assertEqual(p.activate([1]), 0)

        p = Perceptron([-1], 0)
        self.assertEqual(p.activate([0]), 1)
Ejemplo n.º 12
0
from perceptron import Perceptron

inputs = [-1,-1,1]
weightNum = len(inputs)
weights = 0
learningRate = 0.01
desired = 1
iterate = True
iterCount = 0

ptron = Perceptron()
weights = ptron.setWeights(weightNum)
while iterate:
    print "-"*70
    print "Initial Weights: ", weights
    sum = ptron.activate(weights, inputs)
    print "Sum: ", sum
    returned = ptron.updateWeights(learningRate, desired, sum)
    print "New Weights: ", returned['newWeights']
    if not returned['iterate']:
        print "Iterations: ", iterCount
        iterate = False
        print "-"*70
    else:
        weights = returned['newWeights']
        iterCount = iterCount + 1
Ejemplo n.º 13
0
class PerceptronTest(unittest.TestCase):
    def setUp(self):
        self.perceptron = Perceptron()

    def test_activate_func_returns_positive_one_if_input_is_greater_than_zero(
        self
    ):
        actual = self.perceptron.activate(5)

        expected = 1
        self.assertEqual(actual, expected)

    def test_activate_func_returns_negative_one_if_input_is_less_than_zero(
        self
    ):
        actual = self.perceptron.activate(-10)

        expected = -1
        self.assertEqual(actual, expected)

    def test_activate_func_returns_negative_one_if_input_is__zero(self):
        actual = self.perceptron.activate(0)

        expected = -1
        self.assertEqual(actual, expected)

    def test_get_weights_returns_weights_stored_in_perceptron(self):
        expected = [0.5, 0.5, 0.5]
        self.perceptron.weights = expected

        actual = self.perceptron.get_weights()

        self.assertEqual(actual, expected)

    @patch('perceptron.random.uniform')
    def test_give_weights_random_value_when_create_perceptron(
        self,
        mock_uniform
    ):
        expected = [0.1, 0.2, 0.3]
        mock_uniform.side_effect = expected

        perceptron = Perceptron()
        actual = perceptron.get_weights()

        self.assertEqual(actual, expected)

        self.assertEqual(mock_uniform.call_count, 3)

        expected_calls = [
            call(-1, 1),
            call(-1, 1),
            call(-1, 1)
        ]
        self.assertEqual(mock_uniform.mock_calls, expected_calls)

    @patch('perceptron.random.uniform')
    def test_feedforward_returns_positive_one_if_sum_is_greater_than_zero(
        self,
        mock_uniform
    ):
        expected = [0.1, 0.2, 0.3]
        mock_uniform.side_effect = expected

        inputs = [1, 1, 1]

        perceptron = Perceptron()
        actual = perceptron.feedforward(inputs)

        expected = 1
        self.assertEqual(actual, expected)

    @patch('perceptron.random.uniform')
    def test_feedforward_returns_negative_one_if_sum_is_less_than_zero(
        self,
        mock_uniform
    ):
        expected = [0.1, 0.2, 0.3]
        mock_uniform.side_effect = expected

        inputs = [1, -3, 1]

        perceptron = Perceptron()
        actual = perceptron.feedforward(inputs)

        expected = -1
        self.assertEqual(actual, expected)

    @patch('perceptron.random.uniform')
    def test_train_returns_new_adjusted_weights_in_positive_direction(
        self,
        mock_uniform
    ):
        expected = [0.1, 0.2, 0.3]
        mock_uniform.side_effect = expected

        inputs = [1, -3, 1]
        desired = 1

        perceptron = Perceptron()
        actual = perceptron.train(inputs, desired)

        expected = [0.12000000000000001, 0.14, 0.32]
        self.assertEqual(actual, expected)

    @patch('perceptron.random.uniform')
    def test_train_returns_new_adjusted_weights_in_negative_direction(
        self,
        mock_uniform
    ):
        expected = [0.2, 0.1, 0.3]
        mock_uniform.side_effect = expected

        inputs = [1, -3, 1]
        desired = -1

        perceptron = Perceptron()
        actual = perceptron.train(inputs, desired)

        expected = [0.18000000000000002, 0.16, 0.27999999999999997]
        self.assertEqual(actual, expected)